I have a very simple Pong game that I've built in Java. The code is quite long so I've decided to focus this question on the collision that occurs with the ball and the bat and also the effects in the game. I'm using ACM Graphics package to learn Java so most of the methods are from that package. I want to know how I can improve this checking process and if the way I revert the speed and direction is efficient. I'm also open to any suggestions for the game.
//Setting up variables
static final int WAIT = 50;
static final int MV_AMT = 20;
static final int BATWIDTH = 120;
static final int BATHEIGHT = 20;
static final int WINDOWX = 400;
static final int WINDOWY = 400;
static final int BALLRADIUS = 10;
private int batX = 150, batY = 400; //Starting positions
private int ballX = 160, ballY = 370;
private int ballSpeedX = 2; //the ball speed on the X axis
private int ballSpeedY = -9; //the ball speed on the Y axis
public void run(){
//... Stuff that runs before the game, ie. draw the sceen etc.
int currentTime = 0;
//Do all our stuff here
while(continueGame){
//Pause dat loop
pause(WAIT);
currentTime = currentTime + WAIT;
//Up the speed every 5 seconds
if (currentTime % 5000 == 0) {
if(ballSpeedY>0)
ballSpeedY += 2;
else
ballSpeedY -= 2;
if(ballSpeedX>0)
ballSpeedX += 2;
else
ballSpeedX -= 2;
}
//Move the ball
ballX=ballX+ballSpeedX;
ballY=ballY+ballSpeedY;
ball.setLocation(ballX, ballY);
//Check
checkCollisions();
}
//... Stuff that gets done after game over
}
public void checkCollisions(){
//This method is quite long so I won't be posting it all
//Just the part the calls the collision method
//Get the bounds
GRectangle batBounds = bat.getBounds();
GRectangle ballBounds = ball.getBounds();
//Where is the ball?
ballX = (int)ball.getX();
ballY = (int)ball.getY();
//Where is the bat?
batX = (int)bat.getX();
batY = (int)bat.getY();
//Did the bat touch the ball?
if(batBounds.intersects(ballBounds)){
batCollision();
}
}
public void batCollision(){
if( ballX+BALLRADIUS > batX+(BATWIDTH/2) ){ //Which side of the bat?
//Which direction is the ball traveling when it hits?
if(ballSpeedX >> 31 !=0){
ballSpeedX = ballSpeedX * -1;
} else {
ballSpeedX = ballSpeedX;
}
} else {
if(ballSpeedX >> 31 !=0){
ballSpeedX = ballSpeedX;
} else {
ballSpeedX = -ballSpeedX;
}
}
ballSpeedY = -ballSpeedY; //Adjust Y speed
}
1 Answer 1
In the original Pong® brand video game, there were two bats which were confined to move vertically. A collision would be detected if ball circuit was triggered at the same time as one of the bat circuits, and if the ball was not already moving in the proper direction for that bat. The ball's vertical speed would be set to an odd number in the range -15 to +15 based upon the number of scan lines of bat that were displayed before the collision was detected (basically the difference between the ball's Y position and the bat's position). What exact bounce behavior are you looking for in your game?
-
\$\begingroup\$ So I've made sort of a hybrid between Pong and Breakout. What I'm looking for is to determine the side of the bat that was hit and in which direction the incoming ball was moving to set the return speed accordingly. \$\endgroup\$Jonny Sooter– Jonny Sooter2013年06月07日 00:29:48 +00:00Commented Jun 7, 2013 at 0:29
private
? Also, why aren'tWAIT
andMV_AMT
static
? \$\endgroup\$