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

2017/4/13

Making a pong game, need to increase ball speed on bounce.

VinRio VinRio

2017/4/13

#
I am trying to make a pong game and I need to make it so if the ball hits the paddle it speed up, and/or when it hits the wall the angle is not predictable. I just need to be pointed in the right direction because I just cant seem to find any information about how this would be done. I can provide any code from my program that might help. My ball is controlled by two variables that control the X, and Y speed to make it move, is it as simple as adding a random number multiplier to it ?
Nosson1459 Nosson1459

2017/4/13

#
Do you want to know how to make the ball "bounce" or you just want to make it go faster, or both. To make the ball bounce off of the left and right walls you can do:
1
2
3
4
5
6
7
8
9
10
11
12
if (getX() <= getImage().getWidth() / 2
        || getX() >= getWorld().getWidth() - getImage().getWidth() / 2) {
    xSpeed = -xSpeed; // reverses the x direction,
    // not for sure name of your variable
}
 
// to make the ball bounce off the top wall (or bottom)
if (getY() <= getImage().getHeight() / 2
        || getY() >= getWorld().getHeight() - getImage().getHeight() / 2) {
    ySpeed = -ySpeed; // reverses the y direction,
    // not for sure name of your variable
}
If you want to "make it so if the ball hits the paddle it speed up" then just add to the x speed in the code you have for bouncing off the paddle. If you don't have any code for collision detecting with the paddle then things will be getting complicated, so I'll only give code for that if it's necessary. do you want it that when you bounce off the outer edge of the paddle it speeds up but when it hits off the middle it stays the same?
VinRio VinRio

2017/4/14

#
I have all the collision detection, and short of this feature it is mostly a finished game. I just need to figure out a good method of increasing the speed variably when it touches a paddle. I am using *-1, in order to make my ball bounce off the walls, so I am not sure what to add to the method to make it increase the speed if the numbers are swapping from negative to positive .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void hitPaddles() //this is in my ball class
    {
        Actor mover =getOneIntersectingObject(Mover.class);
         
        if(mover !=null)
        {
            xSpeed =xSpeed *-1;
            ySpeed =ySpeed;
        }
    }public void bounceOffWalls()
    {
        int height =getWorld().getHeight();
         
        int halfMyHeight =getImage().getHeight() /2;
         
        //bottom side of world
        if (getY() > height -halfMyHeight)
        {
            ySpeed =ySpeed *-1;
        }
         
        //top side of world
        if (getY() < 0 +halfMyHeight)
        {
            ySpeed =ySpeed *-1;
        }
    }
Yehuda Yehuda

2017/4/14

#
Yehuda wrote...
Used to be 1/2 of Nosson1459
What does assigning a variable to itself accomplish? If you want to negate the xSpeed and make it a value further away from zero then you can do:
1
xSpeed = -xSpeed + xSpeed < 0 ? -2 : 2;
This will add 2 to xSpeed (or -2 if it's below 0), just change the 2s if you want to add a different amount. If I understood correctly this should be line 7.
danpost danpost

2017/4/14

#
Yehuda wrote...
If you want to negate the xSpeed and make it a value further away from zero then you can do:
1
xSpeed = -xSpeed + xSpeed < 0 ? -2 : 2;
This will add 2 to xSpeed (or -2 if it's below 0), just change the 2s if you want to add a different amount. If I understood correctly this should be line 7.
The '+' needs to be a '-' for it to work properly. @Yahuda, the third 'xSpeed' in the line of code is the non-reversed speed, hence my change.
VinRio VinRio

2017/4/14

#
Is that using the semi colon for an if/else statement? I have not learned about that yet, any reference you can link me to? and I'm not sure what you mean by assigning variables to itself, where have I done that. Thanks for the help.
VinRio VinRio

2017/4/14

#
How would I make the ball start by heading straight towards a random paddle and then gain an angle based on hitting a wall or paddle. I have seen it done where the paddle has to be moving and it can put velocity onto the ball, but I can not seem to replicate anything like that.
danpost danpost

2017/4/14

#
VinRio wrote...
Is that using the semi colon for an if/else statement? I have not learned about that yet, any reference you can link me to?
Actually that is using a question mark and a colon (not a semi-colon). Refer to the section on Conditional Operators on this page of the Java tutorials.
I'm not sure what you mean by assigning variables to itself, where have I done that
See line 8 in your code provided above.
Yehuda Yehuda

2017/4/14

#
danpost wrote...
The '+' needs to be a '-' for it to work properly. @Yahuda, the third 'xSpeed' in the line of code is the non-reversed speed, hence my change.
I didn't notice that I was checking the value of xSpeed before it was negated, and then using it on the negated one. You could technically leave the + and change the < to a > or move the '-' from the first 2 to the second, these changes do the same thing -- no difference in size of the code or the results.
danpost danpost

2017/4/14

#
VinRio wrote...
How would I make the ball start by heading straight towards a random paddle and then gain an angle based on hitting a wall or paddle. I have seen it done where the paddle has to be moving and it can put velocity onto the ball, but I can not seem to replicate anything like that.
It would be quite difficult to replicate that behavior when using 'setLocation', 'xSpeed' and 'ySpeed'. It would be much easier using 'turn' and 'move' commands. Also, tracking the speed and location coordinates in a more accurate manner would go a long way to improving the movement. That is, instead of using the location coordinates of the actor in the world, which are integer values, you can track the location in more precise terms either with double values or by smaller units of measure. The SmoothMover class uses 'double' values for the location of your actors (greenfoot provides this class as an importable class using menubar Edit>Import class...); my QActor class uses smaller units of measure for location as well as speed(s) and rotation. It can be found in my downloadable Asteroids w/improved QActor SuperClass scenario.
You need to login to post a reply.