Results 1 to 9 of 9
  1. #1
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default Pinball program problem

    Dear All,
    I am writing a sort of a pinball program that will see a ball bounce around a table, and when it hits any of the four sides it changes colour in the sequence BLUE to RED to YELLOW - and then back to BLUE again. For the colour changes I am using a nested IF statement as follows, but the problem I am having is that the ball stays YELLOW for two bounces before it changes back to BLUE.
    For the life of me I can't see why this would be. Can anyone please throw any light on the probably obvious tweak that needs to be made?
    I have just written out the code in plain English as it almost works - I think it's just a structural issue. Many thanks!

    if (the ball touches any of the sides)
    {
    if (the ball is BLUE)
    {
    make the ball RED
    }

    else
    if (the ball is RED)
    {
    make the ball YELLOW
    }

    else
    if (the ball is YELLOW)
    {
    make the ball BLUE
    }
    }

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    While plain English is great, sometimes things show up in code that can't be seen. If you post the code as well, someone may be able to help you. Certain statements just don't make sense when written in English.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you're gona have to post the code.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by angryboy View Post
    you're gona have to post the code.
    didn't I just say that? :p
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    we posted at the same time. big deal
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    OK sorry here's the full code

    public void ricochet (int xInc, int yInc, int noOfHits)
    {
    int hitCount;
    hitCount = 0;
    while (hitCount < noOfHits)
    {
    this.ball.setXPos(getBallXPos() + xInc);
    this.ball.setYPos(getBallYPos() + yInc);
    this.delay(30);

    if (this.isInCorner())
    {
    xInc = - xInc;
    yInc = - yInc;
    hitCount = hitCount + 1;
    Toolkit.getDefaultToolkit().beep();
    }
    else
    {
    if (this.isTouchingLeftOrRight())
    {
    xInc = - xInc;
    hitCount = hitCount + 1;
    Toolkit.getDefaultToolkit().beep();
    }
    else
    {
    if (this.isTouchingTopOrBottom())
    {
    yInc = - yInc;
    hitCount = hitCount + 1;
    Toolkit.getDefaultToolkit().beep();
    }
    }
    if (this.isInCorner() || this.isTouchingLeftOrRight() || this.isTouchingTopOrBottom())
    {
    if (this.ball.getColour() == BLUE)
    {
    this.ball.setColour(RED);
    }
    else

    if (this.ball.getColour() == RED)
    {
    this.ball.setColour(YELLOW);
    }
    else

    if (this.ball.getColour() == YELLOW)
    {
    this.ball.setColour(BLUE);
    }
    }
    }
    }
    }

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    does the behavior recur for all input? your conditional statements don't seem organized too well. if a corner is hit, the color won't change. better fix that one.

  8. #8
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    does the behavior recur for all input? your conditional statements don't seem organized too well. if a corner is hit, the color won't change. better fix that one.

    yes the ball changes color in the corners no problem but when the ball is yellow it takes two hits against any side or corner to turn back to blue and re-start the sequence.

  9. #9
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    does the behavior recur for all input? your conditional statements don't seem organized too well. if a corner is hit, the color won't change. better fix that one.
    Hey dude, you are completely right - I had one pair of braces too many enclosing an else statement so the corner hits were being excluded.

    I bow to your superior judgement. Many thanks!

Similar Threads

  1. Program problem
    By arindamchkrbrty in forum New To Java
    Replies: 8
    Last Post: 03-10-2009, 04:58 AM
  2. Help! problem with program (using JFrame)
    By linux1man in forum New To Java
    Replies: 5
    Last Post: 01-21-2009, 08:46 PM
  3. [SOLVED] Problem with my program
    By Mika in forum New To Java
    Replies: 9
    Last Post: 12-17-2008, 04:38 AM
  4. DaysAndMonth program problem Help!!
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-04-2008, 06:11 PM
  5. program problem
    By amith in forum AWT / Swing
    Replies: 12
    Last Post: 05-16-2008, 08:07 PM

Posting Permissions

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