Results 1 to 17 of 17
  1. #1
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Java Score Not Working

    Hello, I am working on a school project, this is my first semester doing Java. I have managed to do everything without a problem except for the score of my game. Whenever the bullet hits the disc the score is supposed to go up 1, but it has been going up by 40 each time the bullet hits. Any help would be appreciated here is my code:
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Cannon_Finished extends Applet implements Runnable
    {
    // Variables for application
    int appwidth = 700;
    int appheight = 400;
    int skyheight = 300;
    
    // Variables for gun
    int width = 6;
    int height = 50;
    int top_x = 350;
    int top_y = 280;
    int speed = 13;
    
    // gun base
    int base_width = 36;
    int base_height = 50;
    int base_x = top_x - 15;
    int base_y = top_y + (height / 2);
    
    // bullet
    boolean bullet = false;
    int bullet_x = top_x;
    int bullet_y = top_y;
    int bullet_speed = 6;
    int bullet_height = 2;
    
    // disc
    boolean disc = false;
    int disc_height = 15;
    int disc_width = 50;
    int disc_speed = 4;
    int disc_x = 15;
    int disc_y = 30;
    
    //disc 2
    boolean disc2 = false;
    int disc2_height = 15;
    int disc2_width = 50;
    int disc2_speed = 3;
    int disc2_x = appwidth - 15;
    int disc2_y = 50;
    
    boolean disc3 = false;
    int disc3_height = 15;
    int disc3_width = 50;
    int disc3_speed = 3;
    int disc3_x = appwidth - 15;
    int disc3_y = 50;
    
    int lives = 15;
    int score = 0;
    AudioClip bang;
    
    public void init()
    {
    setSize(appwidth, appheight);
    
    bang = getAudioClip(getCodeBase(), "explosion.au");
    }
    
    public void start()
    {
    Thread th = new Thread(this);
    th.start();
    }
    
    
    public void run()
    {
    
    Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
    
    
    while(true)
    {
    if(disc)
    disc_x = disc_x +(int)(Math.random()*3);
    if (disc_x>=appwidth){
    disc_x = disc_x -1;
    lives = lives-1;
    disc = false;
    
    }
    if (bullet_y <= disc_y && bullet_x > disc_x && bullet_x < disc_x + disc_width) {
    disc = false;
    score = score+1;
    }
    
    if(disc2){
    disc2_x = disc2_x -2;
    if (disc2_x <=appwidth-appwidth){
    lives = lives-1;
    disc2 = false;
    }
    
    }
    if (bullet_y <= disc2_y && bullet_x > disc2_x && bullet_x < disc2_x + disc2_width) {
    disc2 = false;
    score = score+1;
    }
    
    
    if(disc3){
    disc3_x = disc3_x -1;
    if (disc3_x <=appwidth-appwidth){
    lives = lives-1;
    disc3 = false;
    }
    }
    if (bullet_y <= disc3_y && bullet_x > disc3_x && bullet_x < disc3_x + disc3_width) {
    disc3 = false;
    score = score+1;
    }
    
    draw_bullet();
    repaint();
    try
    {
    Thread.sleep(5);
    }
    catch(InterruptedException x)
    {
    
    }
    
    Thread.currentThread().setPriority(Thread.MAX_PRIO RITY);
    
    }
    }
    
    
    
    
    private void draw_bullet()
    {
    if(bullet)
    {
    bullet_y = bullet_y - bullet_speed;
    bullet_x = top_x;
    if(bullet_y < 0)
    {
    bullet_y = top_y;
    bullet = false;
    
    }
    }
    }
    
    public void paint(Graphics g)
    {
    
    // Draw sky
    g.setColor(Color.CYAN);
    g.fillRect(0, 0, appwidth, skyheight);
    
    // Draw grass
    g.setColor(Color.GREEN);
    g.fillRect(0, skyheight, appwidth, appheight-skyheight);
    
    // Draw gun
    g.setColor(new Color(126, 34, 23));
    g.fillRect(top_x, top_y, width, height);
    
    // gun base
    g.fillArc(base_x, base_y, base_width, base_height, 0, 180);
    
    // Draw bullet
    if(bullet)
    {
    g.setColor(Color.BLACK);
    g.fillOval(bullet_x, bullet_y, width, width);
    
    
    }
    //Draw score
    g.drawString("Score " +(int) score, appwidth - 70, top_y - 20);
    g.drawString("Lives " + (int) lives, 10, top_y - 20);
    
    // Draw disc
    if(disc)
    {
    g.setColor(Color.RED);
    g.fillRect(disc_x, disc_y, disc_width, disc_height);
    }
    
    //draw disc2
    if(disc2)
    {
    g.setColor(Color.BLACK);
    g.fillRect(disc2_x, disc2_y, disc2_width, disc2_height);
    }
    
    //draw disc3
    if(disc3)
    {
    g.setColor(Color.GRAY);
    g.fillRect(disc3_x, disc3_y, disc3_width, disc3_height);
    }
    
    if(lives <=0){
    g.setColor(Color.RED);
    g.fillRect(0, 0, appwidth, appheight);
    g.setColor(Color.BLACK);
    g.drawString("You Lose!!", 150, 150);
    }
    }
    
    
    // Keyboard
    // ************************ KeyDown *************************************
    public boolean keyDown(Event evt, int key) {
    if (key == Event.LEFT) {
    top_x = top_x - speed;
    
    if(top_x <= 0)
    top_x = 0;
    }
    
    if (key == Event.RIGHT) {
    top_x = top_x + speed;
    
    if(top_x >= appwidth - width)
    top_x = appwidth - width;
    }
    
    if(key == Event.UP)
    top_x = 0;
    
    if(key == Event.DOWN)
    top_x = appwidth - width;
    
    if (key == Event.ESCAPE) {
    disc = true;
    disc2 = true;
    disc3 = true;
    disc_x = 0;
    disc_y = (int)(Math.random()*200);
    disc2_y = (int)(Math.random()*200);
    disc2_x = appwidth;
    disc3_x = appwidth;
    disc3_y = (int)(Math.random()*200);
    }
    
    
    // base position
    base_x = top_x - 15;
    base_y = top_y + (height / 2);
    
    
    if(key == 32)
    {
    bullet = true;
    bang.play();
    }
    
    return true;
    }
    
    // ************************ KeyUp *************************************
    public boolean keyUp(Event evt, int key) {
    return true;
    }
    
    
    
    // Double buffering
    private Image dbImage;
    private Graphics dbg;
    
    /** Update - Method, implements double buffering */
    public void update(Graphics g) {
    // initialize buffer
    if (dbImage == null) {
    dbImage = createImage(this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics();
    
    }
    
    // clear screen in background
    dbg.setColor(getBackground());
    dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
    
    
    // draw elements in background
    dbg.setColor(getForeground());
    paint(dbg);
    
    // draw image on the screen
    g.drawImage(dbImage, 0, 0, this);
    
    }
    }
    Last edited by Norm; 12-16-2011 at 02:56 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Score Not Working

    it has been going up by 40 each time the bullet hits.
    Try debugging your code by adding printlns every where the score is changed. Print out the new value.
    The print out should help you figure out what is wrong with your code.

  3. #3
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Score Not Working

    I realized its not exactly 40 each time its like a random number between 10-70. I'm very new to java I'm sorry I'm not even sure how to add printlns, if you could tell me how to do that I would. Sorry for my idiocy once again, and thank for your help sir.

  4. #4
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Score Not Working

    I have found that the statement:
    if (bullet_y <= disc_y && bullet_x > disc_x && bullet_x < disc_x + disc_width)
    has made the bullet go through it multiple times and the score would therefore increase by more than 1.
    Does anyone know how to change this so the score will only increase once when the bullet and disc hit?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Score Not Working

    Does anyone know how to change this so the score will only increase once when the bullet and disc hit?
    What are the conditions that controls whether you increase the score? How many times can the bullet continue to hit the target? Is the first time enough?
    Then remove/destroy the bullet after its first contact with the target.

  6. #6
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Score Not Working

    I typed:
    if (bullet_y <= disc_y && bullet_x > disc_x && bullet_x < disc_x + disc_width) {
    bullet = false;
    score = score+1;
    disc = false;
    This made the score go up almost infinitely, and then the cannon stopped shooting the bullets. Am I doing something more wrong?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Score Not Working

    Am I doing something more wrong?
    You need to look at the program's logic to see what it is doing.
    Remember the computer will do EXACTLY what you tell it to do. So check your code to make sure you are telling it what you want it to do. It is not very good are reading minds, so you must be very specific with your instructions to tell it what to do.

  8. #8
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Score Not Working

    This is the logic I put in, (or at least think i did):
    When esc is pressed all the discs become true, and begin going across the screen, some from left to right, others from left to right, this works.
    When the bullet hits any disc, the disc disappears. (Works)
    When the bullet reached the top of the applet, the bullet = false. (Works)
    Now, from what I understand when the bullet hits the disc, the bullet = false, and should go away.
    When this happens, I say the score should increase 1.
    That is what I think I did.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Score Not Working

    What makes the score continue to increase after you set bullet = false?

  10. #10
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Score Not Working

    I'm not sure, I set it so when I press esc the bullet=true again, (line 254)
    but the bullet does not shoot after the first time the bullet hits the disc.
    Could it be that although the bullet and disc does not show up on the screen anymore they are still interacting with each other?
    This is the one thing I cannot figure out, I did everything without really running into any issues.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Score Not Working

    What makes the score continue to increase?
    You have an if test to control when the score will increase.
    When will that if test not be true?

  12. #12
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Score Not Working

    Hmm...yeah I'm sorry I feel like a complete fool but I'm not quite sure I follow what you're saying, I need to make a test to see when the score won't increase?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Score Not Working

    Your code has a problem. It continues to increase the value of score.
    WHY????
    You need to change something so that the program does not execute the: score = score+1; statement.
    That statement's execution is controlled by an if statement.
    Look at the conditions the if statement tests and see why it becomes true when you do NOT want it to be true.

  14. #14
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Score Not Working

    It is because this statement...
    if (bullet_y <= disc3_y && bullet_x > disc3_x && bullet_x < disc3_x + disc3_width) {
    happens multiple times, so each time this statement happens the score increases by 1, so is there any way I can change this statement to only occur once?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Score Not Working

    Add the test of a boolean variable to the if condition.
    Set the boolean false when the if statement is true.

    Somewhere else you will have to set it true to for that if condition to ever be true again.

  16. #16
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Score Not Working

    First of all I want to thank you for all the time you've spent helping me it means a lot.
    Secondly, my java idiocy shines brightly once again, I'm really new and don't quite understand how to do this.
    Could you go into a little more detail?
    Sorry once again.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Score Not Working

    Java Code:
    boolean aFlag = true; // define this somewhere
    ...
    
    if(aFlag && ....) { 
      aFlag = false; // don't come into this if again until aFlag is set true someplace
     ...
    }
    
    ....
    aFlag = true; // Now OK for the if to be true
    Last post for me tonight.

Similar Threads

  1. Java Score Not Working
    By emukherji in forum Java Applets
    Replies: 2
    Last Post: 12-16-2011, 03:02 AM
  2. Score Counter In Java GUI.
    By UnAccomplishedJavaPerson in forum New To Java
    Replies: 9
    Last Post: 11-18-2011, 02:47 PM
  3. Replies: 4
    Last Post: 02-16-2011, 12:05 AM
  4. Replies: 8
    Last Post: 05-28-2008, 08:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •