This site requires JavaScript, please enable it in your browser!
Greenfoot back
Icup
Icup wrote ...

2021/6/21

Hi I need help coming up with an if statement

Icup Icup

2021/6/21

#
I want to make it so once my ball hits the wall I get a point ( the game is pong) it I know it will be something like if ball hits left side of screen score1++; and then if ball hits right side of screen score2++; but I’m not sure how exactly to write the if statement
RcCookie RcCookie

2021/6/21

#
In the act method of the Ball class, write
if(getX() < getImage().getWidth() / 2) {
    score1++;
    endGame();
}
else if(getX() >= getWorld().getWidth() - getImage().getWidth() / 2) {
    score2++;
    endGame();
}
The endGame() method may be something you want to implement, but if you don’t need it just remove it.
Icup Icup

2021/6/21

#
Also do you know how I’d get everything to stay in the world and not go beyond the point of seeing stuff
danpost danpost

2021/6/21

#
Icup wrote...
do you know how I’d get everything to stay in the world and not go beyond the point of seeing stuff
Please clarify issue with details.
RcCookie RcCookie

2021/6/21

#
Behind the two if statements from above, add
else {
    // Any movement code comes here
}
Icup Icup

2021/6/21

#
What I mean is like my 2 pong pieces keep going off the screen when I move them like I can go beyond what I can see and make them disappear and I don’t want that to happen
Icup Icup

2021/6/21

#
Also I did the code you told me and it only works for one side
Icup Icup

2021/6/21

#
As well as the code doesn’t add one point it adds like 12 points
danpost danpost

2021/6/21

#
Icup wrote...
What I mean is like my 2 pong pieces keep going off the screen when I move them like I can go beyond what I can see and make them disappear and I don’t want that to happen
Show class codes for pong pieces. Also, show first line in your world's constructor (it would start with "super("...)
You need to login to post a reply.