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

2017/1/10

Bouncing Ball

banana95 banana95

2017/1/10

#
Hi, I'm trying to make a pong game but I'm having trouble with the ball movement. I have got the ball to bounce off the walls and the paddles using the code below: private int xSpeed = 3, ySpeed = 3; public void act() { movement(); Actor paddle_1 = getOneIntersectingObject(paddle1.class); if (paddle_1!=null) { xSpeed = -xSpeed; } Actor paddle_2 = getOneIntersectingObject(paddle2.class); if (paddle_2!=null) { xSpeed = -xSpeed; } playerScore(); } public void movement() { setLocation(getX() + xSpeed, getY() + ySpeed); if(getY()<5 || getY() > getWorld().getHeight()-5) { ySpeed = -ySpeed; } } The problem I'm having is that I want the ball to start off bouncing between two paddles continuously, until a third paddle comes into contact with the ball. When the ball hits the third paddle I need it to bounce off realistically and then go to the opposite side to score a point like in the game 'Pong'. I hope this makes sense. I'd really appreciate it if someone could point me in the right direction. Thanks
danpost danpost

2017/1/10

#
banana95 wrote...
The problem I'm having is that I want the ball to start off bouncing between two paddles continuously, until a third paddle comes into contact with the ball. When the ball hits the third paddle I need it to bounce off realistically and then go to the opposite side to score a point like in the game 'Pong'.
That would be the same as collision with 'paddle_1' and 'paddle_2' -- would it not?
banana95 banana95

2017/1/10

#
I don't think I've explained very well. I need to have 4 paddles. 2 which remain static and 2 that move vertically to intercept the ball. I need the ball to start off bouncing between the first two paddles continuously and nothing else and then when one of the other paddles intercepts the ball then it will bounce off the paddle towards either the top or bottom walls like in a game of 'Pong'. for example Actor paddle_1 = getOneIntersectingObject(paddle1.class); if (paddle_1!=null) { move(5); turn(180); } Actor paddle_2 = getOneIntersectingObject(paddle2.class); if (paddle_2!=null) { move(5); turn(180); } The trouble I have is that I'm using these (private int xSpeed = 3, ySpeed = 3) to move the ball e.g. setLocation(getX() + xSpeed, getY() + ySpeed); This means that the ball starts off travelling towards the bottom wall instead of going towards the second static paddle. I need a way to control the movement of the ball so that it starts off just bouncing in a straight line and then only changing direction when either paddle 3 or four is introduced. I'm very new to this so what I've said probably doesn't make a lot of sense. Maybe this will make more sense: if (ball hits paddle1) { turn(180); move(5); } if (ball hits paddle2) { turn(180); move(5); } if (ball hits paddle3) { change direction randomly (but only in front of the paddle) } if (ball hits paddle4) { change direction randomly (but only in front of the paddle) } thanks
banana95 banana95

2017/1/10

#
This is what I've got just now: public class ball1 extends Actor { public void act() { move(5); edgeBounce(); playerScore(); Actor paddle_3 = getOneIntersectingObject(paddle3.class); if (paddle_3!=null) { turn(getRandomNumber(160, 240)); } Actor paddle_4 = getOneIntersectingObject(paddle4.class); if (paddle_4!=null) { turn(getRandomNumber(180, 260)); } Actor paddle_1 = getOneIntersectingObject(paddle1.class); if (paddle_1!=null) { turn(180); move(5); } Actor paddle_2 = getOneIntersectingObject(paddle2.class); if (paddle_2!=null) { turn(180); move(5); } } public void edgeBounce() { if(getY()<5 || getY() > getWorld().getHeight()-5) { setRotation(360-getRotation()); move(5); } } public int getRandomNumber(int start,int end) { int normal = Greenfoot.getRandomNumber(end-start-1); return normal+start; } }
danpost danpost

2017/1/10

#
Do the same as for the other paddles, except add some small amount of randomness to the turn. The following will absolutely change the direction of bounce by plus or minus four degrees:
turn(180+((Greenfoot.getRandomNumber(2)*2-1)*4));
banana95 banana95

2017/1/11

#
Thank you this helped.
You need to login to post a reply.