Results 1 to 9 of 9
Thread: Pinball program problem
- 03-20-2009, 08:13 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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
}
}
- 03-20-2009, 08:40 PM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 03-20-2009, 08:40 PM #3
- 03-20-2009, 08:42 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
- 03-20-2009, 08:54 PM #5
- 03-21-2009, 12:38 AM #6
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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);
}
}
}
}
}
- 03-21-2009, 01:16 AM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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.
- 03-21-2009, 01:29 AM #8
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
- 03-21-2009, 02:01 AM #9
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
Program problem
By arindamchkrbrty in forum New To JavaReplies: 8Last Post: 03-10-2009, 04:58 AM -
Help! problem with program (using JFrame)
By linux1man in forum New To JavaReplies: 5Last Post: 01-21-2009, 08:46 PM -
[SOLVED] Problem with my program
By Mika in forum New To JavaReplies: 9Last Post: 12-17-2008, 04:38 AM -
DaysAndMonth program problem Help!!
By kris09 in forum New To JavaReplies: 1Last Post: 08-04-2008, 06:11 PM -
program problem
By amith in forum AWT / SwingReplies: 12Last Post: 05-16-2008, 08:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks