Java Pong game paddles bouncing problem when it touch up end of the game

Multi tool use
I am trying to create a Pong game in java. The game is very basic and I need to make a paddles that should bounce the ball when it touch;
The problem is it is working but both are paddles when touch up line (X=0)
then suddenly paddles are not working (not bouncing the ball just passing)
Here is the a video that you can see:
https://streamable.com/uu5xg
Here is one of the paddle part:
public void paddle2_update(double dt){
//P1_Hit_X = PP1_width + ball_Radius;
//P1_Hit_Y = P1_Y + PP1_height;
//P2_Hit_X = P2_X - ball_Radius;
//P2_Hit_Y = P2_Y + PP2_height;
// Paddle 2 Up with Up Key
if(PP2_UP){ //Define a bool for Up key
P2_Y -= PP_Velocity * dt;
P2_Hit_Y -= PP_Velocity *dt;
// Check paddle inside between the walls
if((P2_Y < 0) ) {
P2_Y = 0;
P2_Hit_Y = 0;
}
}
// Paddle 2 Down with Down Key
if(PP2_DOWN){
P2_Y += PP_Velocity * dt;
P2_Hit_Y += PP_Velocity *dt;
// Check paddle between inside the walls
if(P2_Y > game_height-PP2_height){
P2_Y = game_height-PP2_height;
P2_Hit_Y = game_height;
}
}
}
Also this part is update the ball;
When the ball is between the height of the paddle and X axis over the paddles then should bounce!
public void ball_update(double dt){
ball_X += ball_Velocity_X * dt;
ball_Y += ball_Velocity_Y * dt;
//P1_Hit_X = PP1_width + ball_Radius;
//P1_Hit_Y = P1_Y + PP1_height;
//P2_Hit_X = P2_X - ball_Radius;
//P2_Hit_Y = P2_Y + PP2_height;
// Collision up and below edge of game
if(ball_Y < ball_Radius) {
ball_Velocity_Y *= -1;
}else if (ball_Y > (game_height - ball_Radius)){
ball_Velocity_Y *= -1;
}
// If the ball between height of the paddle
if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
//If the ball close enough for Collision
if(ball_X < P1_Hit_X){
ball_Velocity_X *= -1;
ball_X = P1_Hit_X;
}
}
//If the ball between height of the paddle
if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
//If the ball close enough for Collision
if(ball_X > P2_Hit_X){
ball_Velocity_X *= -1;
ball_X = P2_Hit_X;
}
}
}
The second question is;
I have three stages in the game;
- (0) Introduction (press "p" to play)
- (1) Play
- (2) Result
so when I got the result I have a option to start a new game (press "p" to change stage to 2 to 1
) but when the new game open I can see already started game.
I am not sure for this but every stage I am creating a new background but I do not know how when I in stage-2
still game playing in stage-1
Can you see my mistakes?
Here is whole code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class PongGame extends GameEngine{
public static void main(String args) {
// Warning - don't edit this function.
// Create a new game.
createGame(new PongGame());
}
int game_status;
int game_width, game_height;
double PP1_width, PP1_height, PP2_width, PP2_height;
double P1_X, P1_Y, P2_X, P2_Y;
double PP_Velocity;
boolean PP1_UP, PP1_DOWN, PP2_UP, PP2_DOWN;
double ball_X, ball_Y, ball_Radius, ball_Velocity_X, ball_Velocity_Y;
double P1_Hit_X, P1_Hit_Y, P2_Hit_X, P2_Hit_Y;
int P1_Score, P2_Score, num_score1, num_score2;
String str_score1,str_score2, final_P1_Score, final_P2_Score;
//AudioClip wall_bounce;
//TODO: Create paddles and a ball
//Two paddles for two players: They will be controlled with W-S and UP-DOWN Keys
public void paddle1(){
drawSolidRectangle(P1_X, P1_Y, PP1_width, PP1_height);
}
public void paddle2(){
drawSolidRectangle(P2_X, P2_Y, PP2_width, PP2_height);
}
public void ball(){
drawSolidCircle(ball_X, ball_Y, ball_Radius);
}
public void createBall() {
// Generate a new ball
if(ball_X < 0 || ball_X > game_width ) {
ball_X = game_width/2;
ball_Y = game_height/2;
ball_X= 295;
ball_Y = rand(450);
}
}
//Scores
//TODO: Update Paddles
//DONE: Sources from PhysicsDemo lecture
/**
* These methods allow us the update the window with each framerate so we will be able to move the
* paddles
*/
public void paddle1_update(double dt){
// Paddle 1 Up with W
if(PP1_UP == true){
P1_Y -= PP_Velocity * dt;
P1_Hit_Y -= PP_Velocity *dt;
// Check paddle inside wall
if((P1_Y < 0) ) {
P1_Y = 0;
P1_Hit_Y = 0;
}
}
// Paddle 1 Down with S
if(PP1_DOWN == true){
P1_Y += PP_Velocity * dt;
P1_Hit_Y += PP_Velocity *dt;
// Check paddle inside wall
if(P1_Y > game_height-PP1_height){
P1_Y = game_height-PP1_height;
P1_Hit_Y = game_height;
}
}
}
public void paddle2_update(double dt){
// Paddle 2 Up with Up Key
if(PP2_UP == true){
P2_Y -= PP_Velocity * dt;
P2_Hit_Y -= PP_Velocity *dt;
// Check paddle inside wall
if((P2_Y < 0) ) {
P2_Y = 0;
P2_Hit_Y = 0;
}
}
// Paddle 2 Down with Down Key
if(PP2_DOWN == true){
P2_Y += PP_Velocity * dt;
P2_Hit_Y += PP_Velocity *dt;
// Check paddle inside wall
if(P2_Y > game_height-PP2_height){
P2_Y = game_height-PP2_height;
P2_Hit_Y = game_height;
}
}
}
public void ball_update(double dt){
ball_X += ball_Velocity_X * dt;
ball_Y += ball_Velocity_Y * dt;
//FIXME: A bug! When you touch the up edge of the line then paddle is not working!!!
// Bouncing the ball from edge of the wals
if(ball_Y < ball_Radius) {
ball_Velocity_Y *= -1;
//Play wall bounce sound
// playAudio(wall_bounce);
}else if (ball_Y > (game_height - ball_Radius)){
ball_Velocity_Y *= -1;
//Play wall bounce sound
// playAudio(wall_bounce);
}
if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
if(ball_X < P1_Hit_X){
ball_Velocity_X *= -1;
ball_X = P1_Hit_X;
}
}
if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
if(ball_X > P2_Hit_X){
ball_Velocity_X *= -1;
ball_X = P2_Hit_X;
}
}
}
public void score_update(double dt){
str_score1 = Integer.toString(P1_Score);
str_score2 = Integer.toString(P2_Score);
if(ball_X >= game_width) {
P1_Score = P1_Score +1;
createBall();
}
if(ball_X <= 0 ){
P2_Score = P2_Score + 1;
createBall();
}
}
public void check_winner(double dt) {
//10 is the max score
if(P1_Score == 10 || P2_Score == 10){
final_P1_Score = Integer.toString(P1_Score);
final_P2_Score = Integer.toString(P2_Score);
P1_Score = 0;
P2_Score = 0;
game_status = 2;
}
}
// Background
//TODO: Override init method in GameEngine
public void init(){
//Initialise Window size
game_width = 750;
game_height = 450;
//Position for Paddles
PP_Velocity = 250;
PP1_width = 10;
PP1_height = 100;
P1_X = 0;
P1_Y = 100;
PP2_width = 10;
PP2_height = 100;
P2_X = game_width - PP2_width;
P2_Y = 100;
//Initialise Keyboard variables
PP1_UP = false;
PP1_DOWN = false;
PP2_UP = false;
PP2_DOWN= false;
//Initialise Ball
ball_X = 250;
ball_Y = 250;
ball_Radius = 10;
ball_Velocity_X = 200;
ball_Velocity_Y = 200;
//Game Score
P1_Score = 0;
P2_Score = 0;
str_score1 = "0";
str_score2 = "0";
//Paddle ball bounce point
P1_Hit_X = PP1_width + ball_Radius;
P1_Hit_Y = P1_Y + PP1_height;
P2_Hit_X = P2_X - ball_Radius;
P2_Hit_Y = P2_Y + PP2_height;
/* NOTE: Sound Resources
* https://freesound.org/people/NoiseCollector/sounds/4391/
* https://freesound.org/people/NoiseCollector/sounds/4386/
*/
//wall_bounce = loadAudio("Audio/wall_bounce.wav");
// paddle = loadAudio("Audio/paddle_bounce.wav");
//background = loadAudio("Audio/background.wav");
}
// Override abstract method update(double) in GameEngine
public void update(double dt){
paddle1_update(dt);
paddle2_update(dt);
ball_update(dt);
score_update(dt);
check_winner(dt);
}
public void paintComponent() {
// Before Start game: Game Instruction and start button
if(game_status == 0){
setWindowSize(game_width, game_height);
changeBackgroundColor(white);
clearBackground(game_width, game_height);
changeColor(black);
drawText(50,50,"159.103 Game Programming - Pong Game", "Arial", 20);
changeColor(blue);
drawText(150,150,"Press 'p' for start", "Arial", 50);
changeColor(black);
drawText(400,400,"Created by ", "Arial", 10);
} else if (game_status ==1){
//Actual game status
// Clear the background to black
setWindowSize(game_width, game_height);
changeBackgroundColor(black);
clearBackground(game_width, game_height);
changeColor(white);
drawLine(game_width/2, 0, game_width/2, game_height);
paddle1();
paddle2();
ball();
drawText(50,59, str_score1);
drawText(400,59, str_score2);
num_score1 = Integer.parseInt(str_score1);
num_score2 = Integer.parseInt(str_score2);
} else if (game_status==2){
setWindowSize(game_width, game_height);
changeBackgroundColor(white);
clearBackground(game_width, game_height);
changeColor(blue);
if (num_score1 > num_score2){
drawText(150,70,"Player - 1 WON", "Arial", 70);
} else{
drawText(150,70,"Player - 2 WON", "Arial", 70);
}
drawText(200,150,"Press 'p' for start again", "Arial", 40);
//FIXME: Some Bug with these; Changing the score after a few second later!
//
// drawText(50,150,"Player-1:" + final_P1_Score, "Arial", 50);
// drawText(50,250,"Player-2:" + final_P2_Score, "Arial", 50);
}
}
//TODO: Key Listener for paddles ()
//DONE: KeyListener -> Space game example
// KeyPressed Event
public void keyPressed(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.VK_P && game_status == 0){
game_status =1;
}
if(event.getKeyCode() == KeyEvent.VK_P && game_status == 2){
game_status =1;
}
// Left Arrow
if(event.getKeyCode() == KeyEvent.VK_W) {
PP1_UP = true;
}
// Right Arrow
if(event.getKeyCode() == KeyEvent.VK_S) {
PP1_DOWN = true;
}
// Space Button
if(event.getKeyCode() == KeyEvent.VK_UP) {
PP2_UP = true;
}
if(event.getKeyCode() == KeyEvent.VK_DOWN) {
PP2_DOWN = true;
}
}
// KeyReleased Event
public void keyReleased(KeyEvent event) {
// Left Arrow
if(event.getKeyCode() == KeyEvent.VK_W) {
PP1_UP = false;
}
// Right Arrow
if(event.getKeyCode() == KeyEvent.VK_S) {
PP1_DOWN = false;
}
// Space Button
if(event.getKeyCode() == KeyEvent.VK_UP) {
PP2_UP = false;
}
if(event.getKeyCode() == KeyEvent.VK_DOWN) {
PP2_DOWN = false;
}
}
}
java game-engine game-physics
|
show 2 more comments
I am trying to create a Pong game in java. The game is very basic and I need to make a paddles that should bounce the ball when it touch;
The problem is it is working but both are paddles when touch up line (X=0)
then suddenly paddles are not working (not bouncing the ball just passing)
Here is the a video that you can see:
https://streamable.com/uu5xg
Here is one of the paddle part:
public void paddle2_update(double dt){
//P1_Hit_X = PP1_width + ball_Radius;
//P1_Hit_Y = P1_Y + PP1_height;
//P2_Hit_X = P2_X - ball_Radius;
//P2_Hit_Y = P2_Y + PP2_height;
// Paddle 2 Up with Up Key
if(PP2_UP){ //Define a bool for Up key
P2_Y -= PP_Velocity * dt;
P2_Hit_Y -= PP_Velocity *dt;
// Check paddle inside between the walls
if((P2_Y < 0) ) {
P2_Y = 0;
P2_Hit_Y = 0;
}
}
// Paddle 2 Down with Down Key
if(PP2_DOWN){
P2_Y += PP_Velocity * dt;
P2_Hit_Y += PP_Velocity *dt;
// Check paddle between inside the walls
if(P2_Y > game_height-PP2_height){
P2_Y = game_height-PP2_height;
P2_Hit_Y = game_height;
}
}
}
Also this part is update the ball;
When the ball is between the height of the paddle and X axis over the paddles then should bounce!
public void ball_update(double dt){
ball_X += ball_Velocity_X * dt;
ball_Y += ball_Velocity_Y * dt;
//P1_Hit_X = PP1_width + ball_Radius;
//P1_Hit_Y = P1_Y + PP1_height;
//P2_Hit_X = P2_X - ball_Radius;
//P2_Hit_Y = P2_Y + PP2_height;
// Collision up and below edge of game
if(ball_Y < ball_Radius) {
ball_Velocity_Y *= -1;
}else if (ball_Y > (game_height - ball_Radius)){
ball_Velocity_Y *= -1;
}
// If the ball between height of the paddle
if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
//If the ball close enough for Collision
if(ball_X < P1_Hit_X){
ball_Velocity_X *= -1;
ball_X = P1_Hit_X;
}
}
//If the ball between height of the paddle
if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
//If the ball close enough for Collision
if(ball_X > P2_Hit_X){
ball_Velocity_X *= -1;
ball_X = P2_Hit_X;
}
}
}
The second question is;
I have three stages in the game;
- (0) Introduction (press "p" to play)
- (1) Play
- (2) Result
so when I got the result I have a option to start a new game (press "p" to change stage to 2 to 1
) but when the new game open I can see already started game.
I am not sure for this but every stage I am creating a new background but I do not know how when I in stage-2
still game playing in stage-1
Can you see my mistakes?
Here is whole code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class PongGame extends GameEngine{
public static void main(String args) {
// Warning - don't edit this function.
// Create a new game.
createGame(new PongGame());
}
int game_status;
int game_width, game_height;
double PP1_width, PP1_height, PP2_width, PP2_height;
double P1_X, P1_Y, P2_X, P2_Y;
double PP_Velocity;
boolean PP1_UP, PP1_DOWN, PP2_UP, PP2_DOWN;
double ball_X, ball_Y, ball_Radius, ball_Velocity_X, ball_Velocity_Y;
double P1_Hit_X, P1_Hit_Y, P2_Hit_X, P2_Hit_Y;
int P1_Score, P2_Score, num_score1, num_score2;
String str_score1,str_score2, final_P1_Score, final_P2_Score;
//AudioClip wall_bounce;
//TODO: Create paddles and a ball
//Two paddles for two players: They will be controlled with W-S and UP-DOWN Keys
public void paddle1(){
drawSolidRectangle(P1_X, P1_Y, PP1_width, PP1_height);
}
public void paddle2(){
drawSolidRectangle(P2_X, P2_Y, PP2_width, PP2_height);
}
public void ball(){
drawSolidCircle(ball_X, ball_Y, ball_Radius);
}
public void createBall() {
// Generate a new ball
if(ball_X < 0 || ball_X > game_width ) {
ball_X = game_width/2;
ball_Y = game_height/2;
ball_X= 295;
ball_Y = rand(450);
}
}
//Scores
//TODO: Update Paddles
//DONE: Sources from PhysicsDemo lecture
/**
* These methods allow us the update the window with each framerate so we will be able to move the
* paddles
*/
public void paddle1_update(double dt){
// Paddle 1 Up with W
if(PP1_UP == true){
P1_Y -= PP_Velocity * dt;
P1_Hit_Y -= PP_Velocity *dt;
// Check paddle inside wall
if((P1_Y < 0) ) {
P1_Y = 0;
P1_Hit_Y = 0;
}
}
// Paddle 1 Down with S
if(PP1_DOWN == true){
P1_Y += PP_Velocity * dt;
P1_Hit_Y += PP_Velocity *dt;
// Check paddle inside wall
if(P1_Y > game_height-PP1_height){
P1_Y = game_height-PP1_height;
P1_Hit_Y = game_height;
}
}
}
public void paddle2_update(double dt){
// Paddle 2 Up with Up Key
if(PP2_UP == true){
P2_Y -= PP_Velocity * dt;
P2_Hit_Y -= PP_Velocity *dt;
// Check paddle inside wall
if((P2_Y < 0) ) {
P2_Y = 0;
P2_Hit_Y = 0;
}
}
// Paddle 2 Down with Down Key
if(PP2_DOWN == true){
P2_Y += PP_Velocity * dt;
P2_Hit_Y += PP_Velocity *dt;
// Check paddle inside wall
if(P2_Y > game_height-PP2_height){
P2_Y = game_height-PP2_height;
P2_Hit_Y = game_height;
}
}
}
public void ball_update(double dt){
ball_X += ball_Velocity_X * dt;
ball_Y += ball_Velocity_Y * dt;
//FIXME: A bug! When you touch the up edge of the line then paddle is not working!!!
// Bouncing the ball from edge of the wals
if(ball_Y < ball_Radius) {
ball_Velocity_Y *= -1;
//Play wall bounce sound
// playAudio(wall_bounce);
}else if (ball_Y > (game_height - ball_Radius)){
ball_Velocity_Y *= -1;
//Play wall bounce sound
// playAudio(wall_bounce);
}
if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
if(ball_X < P1_Hit_X){
ball_Velocity_X *= -1;
ball_X = P1_Hit_X;
}
}
if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
if(ball_X > P2_Hit_X){
ball_Velocity_X *= -1;
ball_X = P2_Hit_X;
}
}
}
public void score_update(double dt){
str_score1 = Integer.toString(P1_Score);
str_score2 = Integer.toString(P2_Score);
if(ball_X >= game_width) {
P1_Score = P1_Score +1;
createBall();
}
if(ball_X <= 0 ){
P2_Score = P2_Score + 1;
createBall();
}
}
public void check_winner(double dt) {
//10 is the max score
if(P1_Score == 10 || P2_Score == 10){
final_P1_Score = Integer.toString(P1_Score);
final_P2_Score = Integer.toString(P2_Score);
P1_Score = 0;
P2_Score = 0;
game_status = 2;
}
}
// Background
//TODO: Override init method in GameEngine
public void init(){
//Initialise Window size
game_width = 750;
game_height = 450;
//Position for Paddles
PP_Velocity = 250;
PP1_width = 10;
PP1_height = 100;
P1_X = 0;
P1_Y = 100;
PP2_width = 10;
PP2_height = 100;
P2_X = game_width - PP2_width;
P2_Y = 100;
//Initialise Keyboard variables
PP1_UP = false;
PP1_DOWN = false;
PP2_UP = false;
PP2_DOWN= false;
//Initialise Ball
ball_X = 250;
ball_Y = 250;
ball_Radius = 10;
ball_Velocity_X = 200;
ball_Velocity_Y = 200;
//Game Score
P1_Score = 0;
P2_Score = 0;
str_score1 = "0";
str_score2 = "0";
//Paddle ball bounce point
P1_Hit_X = PP1_width + ball_Radius;
P1_Hit_Y = P1_Y + PP1_height;
P2_Hit_X = P2_X - ball_Radius;
P2_Hit_Y = P2_Y + PP2_height;
/* NOTE: Sound Resources
* https://freesound.org/people/NoiseCollector/sounds/4391/
* https://freesound.org/people/NoiseCollector/sounds/4386/
*/
//wall_bounce = loadAudio("Audio/wall_bounce.wav");
// paddle = loadAudio("Audio/paddle_bounce.wav");
//background = loadAudio("Audio/background.wav");
}
// Override abstract method update(double) in GameEngine
public void update(double dt){
paddle1_update(dt);
paddle2_update(dt);
ball_update(dt);
score_update(dt);
check_winner(dt);
}
public void paintComponent() {
// Before Start game: Game Instruction and start button
if(game_status == 0){
setWindowSize(game_width, game_height);
changeBackgroundColor(white);
clearBackground(game_width, game_height);
changeColor(black);
drawText(50,50,"159.103 Game Programming - Pong Game", "Arial", 20);
changeColor(blue);
drawText(150,150,"Press 'p' for start", "Arial", 50);
changeColor(black);
drawText(400,400,"Created by ", "Arial", 10);
} else if (game_status ==1){
//Actual game status
// Clear the background to black
setWindowSize(game_width, game_height);
changeBackgroundColor(black);
clearBackground(game_width, game_height);
changeColor(white);
drawLine(game_width/2, 0, game_width/2, game_height);
paddle1();
paddle2();
ball();
drawText(50,59, str_score1);
drawText(400,59, str_score2);
num_score1 = Integer.parseInt(str_score1);
num_score2 = Integer.parseInt(str_score2);
} else if (game_status==2){
setWindowSize(game_width, game_height);
changeBackgroundColor(white);
clearBackground(game_width, game_height);
changeColor(blue);
if (num_score1 > num_score2){
drawText(150,70,"Player - 1 WON", "Arial", 70);
} else{
drawText(150,70,"Player - 2 WON", "Arial", 70);
}
drawText(200,150,"Press 'p' for start again", "Arial", 40);
//FIXME: Some Bug with these; Changing the score after a few second later!
//
// drawText(50,150,"Player-1:" + final_P1_Score, "Arial", 50);
// drawText(50,250,"Player-2:" + final_P2_Score, "Arial", 50);
}
}
//TODO: Key Listener for paddles ()
//DONE: KeyListener -> Space game example
// KeyPressed Event
public void keyPressed(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.VK_P && game_status == 0){
game_status =1;
}
if(event.getKeyCode() == KeyEvent.VK_P && game_status == 2){
game_status =1;
}
// Left Arrow
if(event.getKeyCode() == KeyEvent.VK_W) {
PP1_UP = true;
}
// Right Arrow
if(event.getKeyCode() == KeyEvent.VK_S) {
PP1_DOWN = true;
}
// Space Button
if(event.getKeyCode() == KeyEvent.VK_UP) {
PP2_UP = true;
}
if(event.getKeyCode() == KeyEvent.VK_DOWN) {
PP2_DOWN = true;
}
}
// KeyReleased Event
public void keyReleased(KeyEvent event) {
// Left Arrow
if(event.getKeyCode() == KeyEvent.VK_W) {
PP1_UP = false;
}
// Right Arrow
if(event.getKeyCode() == KeyEvent.VK_S) {
PP1_DOWN = false;
}
// Space Button
if(event.getKeyCode() == KeyEvent.VK_UP) {
PP2_UP = false;
}
if(event.getKeyCode() == KeyEvent.VK_DOWN) {
PP2_DOWN = false;
}
}
}
java game-engine game-physics
1
FYI,== true
is always redundant.
– shmosel
Dec 28 '18 at 3:34
I think you have a problem inball_update
method where you must validate theball_X
should never go beyond boundary or max_X
– shriyog
Dec 28 '18 at 3:36
I got it @shmosel thanks for it.
– Axis
Dec 28 '18 at 3:58
@shriyog I am updating ball_x with P1_hit.. values. Might I missed your point can you give a bit more detail please
– Axis
Dec 28 '18 at 3:58
@Axis Can you please point me out where it's done? Is itFixme
location? The variable names are a bit confusing.
– shriyog
Dec 28 '18 at 4:03
|
show 2 more comments
I am trying to create a Pong game in java. The game is very basic and I need to make a paddles that should bounce the ball when it touch;
The problem is it is working but both are paddles when touch up line (X=0)
then suddenly paddles are not working (not bouncing the ball just passing)
Here is the a video that you can see:
https://streamable.com/uu5xg
Here is one of the paddle part:
public void paddle2_update(double dt){
//P1_Hit_X = PP1_width + ball_Radius;
//P1_Hit_Y = P1_Y + PP1_height;
//P2_Hit_X = P2_X - ball_Radius;
//P2_Hit_Y = P2_Y + PP2_height;
// Paddle 2 Up with Up Key
if(PP2_UP){ //Define a bool for Up key
P2_Y -= PP_Velocity * dt;
P2_Hit_Y -= PP_Velocity *dt;
// Check paddle inside between the walls
if((P2_Y < 0) ) {
P2_Y = 0;
P2_Hit_Y = 0;
}
}
// Paddle 2 Down with Down Key
if(PP2_DOWN){
P2_Y += PP_Velocity * dt;
P2_Hit_Y += PP_Velocity *dt;
// Check paddle between inside the walls
if(P2_Y > game_height-PP2_height){
P2_Y = game_height-PP2_height;
P2_Hit_Y = game_height;
}
}
}
Also this part is update the ball;
When the ball is between the height of the paddle and X axis over the paddles then should bounce!
public void ball_update(double dt){
ball_X += ball_Velocity_X * dt;
ball_Y += ball_Velocity_Y * dt;
//P1_Hit_X = PP1_width + ball_Radius;
//P1_Hit_Y = P1_Y + PP1_height;
//P2_Hit_X = P2_X - ball_Radius;
//P2_Hit_Y = P2_Y + PP2_height;
// Collision up and below edge of game
if(ball_Y < ball_Radius) {
ball_Velocity_Y *= -1;
}else if (ball_Y > (game_height - ball_Radius)){
ball_Velocity_Y *= -1;
}
// If the ball between height of the paddle
if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
//If the ball close enough for Collision
if(ball_X < P1_Hit_X){
ball_Velocity_X *= -1;
ball_X = P1_Hit_X;
}
}
//If the ball between height of the paddle
if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
//If the ball close enough for Collision
if(ball_X > P2_Hit_X){
ball_Velocity_X *= -1;
ball_X = P2_Hit_X;
}
}
}
The second question is;
I have three stages in the game;
- (0) Introduction (press "p" to play)
- (1) Play
- (2) Result
so when I got the result I have a option to start a new game (press "p" to change stage to 2 to 1
) but when the new game open I can see already started game.
I am not sure for this but every stage I am creating a new background but I do not know how when I in stage-2
still game playing in stage-1
Can you see my mistakes?
Here is whole code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class PongGame extends GameEngine{
public static void main(String args) {
// Warning - don't edit this function.
// Create a new game.
createGame(new PongGame());
}
int game_status;
int game_width, game_height;
double PP1_width, PP1_height, PP2_width, PP2_height;
double P1_X, P1_Y, P2_X, P2_Y;
double PP_Velocity;
boolean PP1_UP, PP1_DOWN, PP2_UP, PP2_DOWN;
double ball_X, ball_Y, ball_Radius, ball_Velocity_X, ball_Velocity_Y;
double P1_Hit_X, P1_Hit_Y, P2_Hit_X, P2_Hit_Y;
int P1_Score, P2_Score, num_score1, num_score2;
String str_score1,str_score2, final_P1_Score, final_P2_Score;
//AudioClip wall_bounce;
//TODO: Create paddles and a ball
//Two paddles for two players: They will be controlled with W-S and UP-DOWN Keys
public void paddle1(){
drawSolidRectangle(P1_X, P1_Y, PP1_width, PP1_height);
}
public void paddle2(){
drawSolidRectangle(P2_X, P2_Y, PP2_width, PP2_height);
}
public void ball(){
drawSolidCircle(ball_X, ball_Y, ball_Radius);
}
public void createBall() {
// Generate a new ball
if(ball_X < 0 || ball_X > game_width ) {
ball_X = game_width/2;
ball_Y = game_height/2;
ball_X= 295;
ball_Y = rand(450);
}
}
//Scores
//TODO: Update Paddles
//DONE: Sources from PhysicsDemo lecture
/**
* These methods allow us the update the window with each framerate so we will be able to move the
* paddles
*/
public void paddle1_update(double dt){
// Paddle 1 Up with W
if(PP1_UP == true){
P1_Y -= PP_Velocity * dt;
P1_Hit_Y -= PP_Velocity *dt;
// Check paddle inside wall
if((P1_Y < 0) ) {
P1_Y = 0;
P1_Hit_Y = 0;
}
}
// Paddle 1 Down with S
if(PP1_DOWN == true){
P1_Y += PP_Velocity * dt;
P1_Hit_Y += PP_Velocity *dt;
// Check paddle inside wall
if(P1_Y > game_height-PP1_height){
P1_Y = game_height-PP1_height;
P1_Hit_Y = game_height;
}
}
}
public void paddle2_update(double dt){
// Paddle 2 Up with Up Key
if(PP2_UP == true){
P2_Y -= PP_Velocity * dt;
P2_Hit_Y -= PP_Velocity *dt;
// Check paddle inside wall
if((P2_Y < 0) ) {
P2_Y = 0;
P2_Hit_Y = 0;
}
}
// Paddle 2 Down with Down Key
if(PP2_DOWN == true){
P2_Y += PP_Velocity * dt;
P2_Hit_Y += PP_Velocity *dt;
// Check paddle inside wall
if(P2_Y > game_height-PP2_height){
P2_Y = game_height-PP2_height;
P2_Hit_Y = game_height;
}
}
}
public void ball_update(double dt){
ball_X += ball_Velocity_X * dt;
ball_Y += ball_Velocity_Y * dt;
//FIXME: A bug! When you touch the up edge of the line then paddle is not working!!!
// Bouncing the ball from edge of the wals
if(ball_Y < ball_Radius) {
ball_Velocity_Y *= -1;
//Play wall bounce sound
// playAudio(wall_bounce);
}else if (ball_Y > (game_height - ball_Radius)){
ball_Velocity_Y *= -1;
//Play wall bounce sound
// playAudio(wall_bounce);
}
if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
if(ball_X < P1_Hit_X){
ball_Velocity_X *= -1;
ball_X = P1_Hit_X;
}
}
if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
if(ball_X > P2_Hit_X){
ball_Velocity_X *= -1;
ball_X = P2_Hit_X;
}
}
}
public void score_update(double dt){
str_score1 = Integer.toString(P1_Score);
str_score2 = Integer.toString(P2_Score);
if(ball_X >= game_width) {
P1_Score = P1_Score +1;
createBall();
}
if(ball_X <= 0 ){
P2_Score = P2_Score + 1;
createBall();
}
}
public void check_winner(double dt) {
//10 is the max score
if(P1_Score == 10 || P2_Score == 10){
final_P1_Score = Integer.toString(P1_Score);
final_P2_Score = Integer.toString(P2_Score);
P1_Score = 0;
P2_Score = 0;
game_status = 2;
}
}
// Background
//TODO: Override init method in GameEngine
public void init(){
//Initialise Window size
game_width = 750;
game_height = 450;
//Position for Paddles
PP_Velocity = 250;
PP1_width = 10;
PP1_height = 100;
P1_X = 0;
P1_Y = 100;
PP2_width = 10;
PP2_height = 100;
P2_X = game_width - PP2_width;
P2_Y = 100;
//Initialise Keyboard variables
PP1_UP = false;
PP1_DOWN = false;
PP2_UP = false;
PP2_DOWN= false;
//Initialise Ball
ball_X = 250;
ball_Y = 250;
ball_Radius = 10;
ball_Velocity_X = 200;
ball_Velocity_Y = 200;
//Game Score
P1_Score = 0;
P2_Score = 0;
str_score1 = "0";
str_score2 = "0";
//Paddle ball bounce point
P1_Hit_X = PP1_width + ball_Radius;
P1_Hit_Y = P1_Y + PP1_height;
P2_Hit_X = P2_X - ball_Radius;
P2_Hit_Y = P2_Y + PP2_height;
/* NOTE: Sound Resources
* https://freesound.org/people/NoiseCollector/sounds/4391/
* https://freesound.org/people/NoiseCollector/sounds/4386/
*/
//wall_bounce = loadAudio("Audio/wall_bounce.wav");
// paddle = loadAudio("Audio/paddle_bounce.wav");
//background = loadAudio("Audio/background.wav");
}
// Override abstract method update(double) in GameEngine
public void update(double dt){
paddle1_update(dt);
paddle2_update(dt);
ball_update(dt);
score_update(dt);
check_winner(dt);
}
public void paintComponent() {
// Before Start game: Game Instruction and start button
if(game_status == 0){
setWindowSize(game_width, game_height);
changeBackgroundColor(white);
clearBackground(game_width, game_height);
changeColor(black);
drawText(50,50,"159.103 Game Programming - Pong Game", "Arial", 20);
changeColor(blue);
drawText(150,150,"Press 'p' for start", "Arial", 50);
changeColor(black);
drawText(400,400,"Created by ", "Arial", 10);
} else if (game_status ==1){
//Actual game status
// Clear the background to black
setWindowSize(game_width, game_height);
changeBackgroundColor(black);
clearBackground(game_width, game_height);
changeColor(white);
drawLine(game_width/2, 0, game_width/2, game_height);
paddle1();
paddle2();
ball();
drawText(50,59, str_score1);
drawText(400,59, str_score2);
num_score1 = Integer.parseInt(str_score1);
num_score2 = Integer.parseInt(str_score2);
} else if (game_status==2){
setWindowSize(game_width, game_height);
changeBackgroundColor(white);
clearBackground(game_width, game_height);
changeColor(blue);
if (num_score1 > num_score2){
drawText(150,70,"Player - 1 WON", "Arial", 70);
} else{
drawText(150,70,"Player - 2 WON", "Arial", 70);
}
drawText(200,150,"Press 'p' for start again", "Arial", 40);
//FIXME: Some Bug with these; Changing the score after a few second later!
//
// drawText(50,150,"Player-1:" + final_P1_Score, "Arial", 50);
// drawText(50,250,"Player-2:" + final_P2_Score, "Arial", 50);
}
}
//TODO: Key Listener for paddles ()
//DONE: KeyListener -> Space game example
// KeyPressed Event
public void keyPressed(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.VK_P && game_status == 0){
game_status =1;
}
if(event.getKeyCode() == KeyEvent.VK_P && game_status == 2){
game_status =1;
}
// Left Arrow
if(event.getKeyCode() == KeyEvent.VK_W) {
PP1_UP = true;
}
// Right Arrow
if(event.getKeyCode() == KeyEvent.VK_S) {
PP1_DOWN = true;
}
// Space Button
if(event.getKeyCode() == KeyEvent.VK_UP) {
PP2_UP = true;
}
if(event.getKeyCode() == KeyEvent.VK_DOWN) {
PP2_DOWN = true;
}
}
// KeyReleased Event
public void keyReleased(KeyEvent event) {
// Left Arrow
if(event.getKeyCode() == KeyEvent.VK_W) {
PP1_UP = false;
}
// Right Arrow
if(event.getKeyCode() == KeyEvent.VK_S) {
PP1_DOWN = false;
}
// Space Button
if(event.getKeyCode() == KeyEvent.VK_UP) {
PP2_UP = false;
}
if(event.getKeyCode() == KeyEvent.VK_DOWN) {
PP2_DOWN = false;
}
}
}
java game-engine game-physics
I am trying to create a Pong game in java. The game is very basic and I need to make a paddles that should bounce the ball when it touch;
The problem is it is working but both are paddles when touch up line (X=0)
then suddenly paddles are not working (not bouncing the ball just passing)
Here is the a video that you can see:
https://streamable.com/uu5xg
Here is one of the paddle part:
public void paddle2_update(double dt){
//P1_Hit_X = PP1_width + ball_Radius;
//P1_Hit_Y = P1_Y + PP1_height;
//P2_Hit_X = P2_X - ball_Radius;
//P2_Hit_Y = P2_Y + PP2_height;
// Paddle 2 Up with Up Key
if(PP2_UP){ //Define a bool for Up key
P2_Y -= PP_Velocity * dt;
P2_Hit_Y -= PP_Velocity *dt;
// Check paddle inside between the walls
if((P2_Y < 0) ) {
P2_Y = 0;
P2_Hit_Y = 0;
}
}
// Paddle 2 Down with Down Key
if(PP2_DOWN){
P2_Y += PP_Velocity * dt;
P2_Hit_Y += PP_Velocity *dt;
// Check paddle between inside the walls
if(P2_Y > game_height-PP2_height){
P2_Y = game_height-PP2_height;
P2_Hit_Y = game_height;
}
}
}
Also this part is update the ball;
When the ball is between the height of the paddle and X axis over the paddles then should bounce!
public void ball_update(double dt){
ball_X += ball_Velocity_X * dt;
ball_Y += ball_Velocity_Y * dt;
//P1_Hit_X = PP1_width + ball_Radius;
//P1_Hit_Y = P1_Y + PP1_height;
//P2_Hit_X = P2_X - ball_Radius;
//P2_Hit_Y = P2_Y + PP2_height;
// Collision up and below edge of game
if(ball_Y < ball_Radius) {
ball_Velocity_Y *= -1;
}else if (ball_Y > (game_height - ball_Radius)){
ball_Velocity_Y *= -1;
}
// If the ball between height of the paddle
if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
//If the ball close enough for Collision
if(ball_X < P1_Hit_X){
ball_Velocity_X *= -1;
ball_X = P1_Hit_X;
}
}
//If the ball between height of the paddle
if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
//If the ball close enough for Collision
if(ball_X > P2_Hit_X){
ball_Velocity_X *= -1;
ball_X = P2_Hit_X;
}
}
}
The second question is;
I have three stages in the game;
- (0) Introduction (press "p" to play)
- (1) Play
- (2) Result
so when I got the result I have a option to start a new game (press "p" to change stage to 2 to 1
) but when the new game open I can see already started game.
I am not sure for this but every stage I am creating a new background but I do not know how when I in stage-2
still game playing in stage-1
Can you see my mistakes?
Here is whole code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class PongGame extends GameEngine{
public static void main(String args) {
// Warning - don't edit this function.
// Create a new game.
createGame(new PongGame());
}
int game_status;
int game_width, game_height;
double PP1_width, PP1_height, PP2_width, PP2_height;
double P1_X, P1_Y, P2_X, P2_Y;
double PP_Velocity;
boolean PP1_UP, PP1_DOWN, PP2_UP, PP2_DOWN;
double ball_X, ball_Y, ball_Radius, ball_Velocity_X, ball_Velocity_Y;
double P1_Hit_X, P1_Hit_Y, P2_Hit_X, P2_Hit_Y;
int P1_Score, P2_Score, num_score1, num_score2;
String str_score1,str_score2, final_P1_Score, final_P2_Score;
//AudioClip wall_bounce;
//TODO: Create paddles and a ball
//Two paddles for two players: They will be controlled with W-S and UP-DOWN Keys
public void paddle1(){
drawSolidRectangle(P1_X, P1_Y, PP1_width, PP1_height);
}
public void paddle2(){
drawSolidRectangle(P2_X, P2_Y, PP2_width, PP2_height);
}
public void ball(){
drawSolidCircle(ball_X, ball_Y, ball_Radius);
}
public void createBall() {
// Generate a new ball
if(ball_X < 0 || ball_X > game_width ) {
ball_X = game_width/2;
ball_Y = game_height/2;
ball_X= 295;
ball_Y = rand(450);
}
}
//Scores
//TODO: Update Paddles
//DONE: Sources from PhysicsDemo lecture
/**
* These methods allow us the update the window with each framerate so we will be able to move the
* paddles
*/
public void paddle1_update(double dt){
// Paddle 1 Up with W
if(PP1_UP == true){
P1_Y -= PP_Velocity * dt;
P1_Hit_Y -= PP_Velocity *dt;
// Check paddle inside wall
if((P1_Y < 0) ) {
P1_Y = 0;
P1_Hit_Y = 0;
}
}
// Paddle 1 Down with S
if(PP1_DOWN == true){
P1_Y += PP_Velocity * dt;
P1_Hit_Y += PP_Velocity *dt;
// Check paddle inside wall
if(P1_Y > game_height-PP1_height){
P1_Y = game_height-PP1_height;
P1_Hit_Y = game_height;
}
}
}
public void paddle2_update(double dt){
// Paddle 2 Up with Up Key
if(PP2_UP == true){
P2_Y -= PP_Velocity * dt;
P2_Hit_Y -= PP_Velocity *dt;
// Check paddle inside wall
if((P2_Y < 0) ) {
P2_Y = 0;
P2_Hit_Y = 0;
}
}
// Paddle 2 Down with Down Key
if(PP2_DOWN == true){
P2_Y += PP_Velocity * dt;
P2_Hit_Y += PP_Velocity *dt;
// Check paddle inside wall
if(P2_Y > game_height-PP2_height){
P2_Y = game_height-PP2_height;
P2_Hit_Y = game_height;
}
}
}
public void ball_update(double dt){
ball_X += ball_Velocity_X * dt;
ball_Y += ball_Velocity_Y * dt;
//FIXME: A bug! When you touch the up edge of the line then paddle is not working!!!
// Bouncing the ball from edge of the wals
if(ball_Y < ball_Radius) {
ball_Velocity_Y *= -1;
//Play wall bounce sound
// playAudio(wall_bounce);
}else if (ball_Y > (game_height - ball_Radius)){
ball_Velocity_Y *= -1;
//Play wall bounce sound
// playAudio(wall_bounce);
}
if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
if(ball_X < P1_Hit_X){
ball_Velocity_X *= -1;
ball_X = P1_Hit_X;
}
}
if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
if(ball_X > P2_Hit_X){
ball_Velocity_X *= -1;
ball_X = P2_Hit_X;
}
}
}
public void score_update(double dt){
str_score1 = Integer.toString(P1_Score);
str_score2 = Integer.toString(P2_Score);
if(ball_X >= game_width) {
P1_Score = P1_Score +1;
createBall();
}
if(ball_X <= 0 ){
P2_Score = P2_Score + 1;
createBall();
}
}
public void check_winner(double dt) {
//10 is the max score
if(P1_Score == 10 || P2_Score == 10){
final_P1_Score = Integer.toString(P1_Score);
final_P2_Score = Integer.toString(P2_Score);
P1_Score = 0;
P2_Score = 0;
game_status = 2;
}
}
// Background
//TODO: Override init method in GameEngine
public void init(){
//Initialise Window size
game_width = 750;
game_height = 450;
//Position for Paddles
PP_Velocity = 250;
PP1_width = 10;
PP1_height = 100;
P1_X = 0;
P1_Y = 100;
PP2_width = 10;
PP2_height = 100;
P2_X = game_width - PP2_width;
P2_Y = 100;
//Initialise Keyboard variables
PP1_UP = false;
PP1_DOWN = false;
PP2_UP = false;
PP2_DOWN= false;
//Initialise Ball
ball_X = 250;
ball_Y = 250;
ball_Radius = 10;
ball_Velocity_X = 200;
ball_Velocity_Y = 200;
//Game Score
P1_Score = 0;
P2_Score = 0;
str_score1 = "0";
str_score2 = "0";
//Paddle ball bounce point
P1_Hit_X = PP1_width + ball_Radius;
P1_Hit_Y = P1_Y + PP1_height;
P2_Hit_X = P2_X - ball_Radius;
P2_Hit_Y = P2_Y + PP2_height;
/* NOTE: Sound Resources
* https://freesound.org/people/NoiseCollector/sounds/4391/
* https://freesound.org/people/NoiseCollector/sounds/4386/
*/
//wall_bounce = loadAudio("Audio/wall_bounce.wav");
// paddle = loadAudio("Audio/paddle_bounce.wav");
//background = loadAudio("Audio/background.wav");
}
// Override abstract method update(double) in GameEngine
public void update(double dt){
paddle1_update(dt);
paddle2_update(dt);
ball_update(dt);
score_update(dt);
check_winner(dt);
}
public void paintComponent() {
// Before Start game: Game Instruction and start button
if(game_status == 0){
setWindowSize(game_width, game_height);
changeBackgroundColor(white);
clearBackground(game_width, game_height);
changeColor(black);
drawText(50,50,"159.103 Game Programming - Pong Game", "Arial", 20);
changeColor(blue);
drawText(150,150,"Press 'p' for start", "Arial", 50);
changeColor(black);
drawText(400,400,"Created by ", "Arial", 10);
} else if (game_status ==1){
//Actual game status
// Clear the background to black
setWindowSize(game_width, game_height);
changeBackgroundColor(black);
clearBackground(game_width, game_height);
changeColor(white);
drawLine(game_width/2, 0, game_width/2, game_height);
paddle1();
paddle2();
ball();
drawText(50,59, str_score1);
drawText(400,59, str_score2);
num_score1 = Integer.parseInt(str_score1);
num_score2 = Integer.parseInt(str_score2);
} else if (game_status==2){
setWindowSize(game_width, game_height);
changeBackgroundColor(white);
clearBackground(game_width, game_height);
changeColor(blue);
if (num_score1 > num_score2){
drawText(150,70,"Player - 1 WON", "Arial", 70);
} else{
drawText(150,70,"Player - 2 WON", "Arial", 70);
}
drawText(200,150,"Press 'p' for start again", "Arial", 40);
//FIXME: Some Bug with these; Changing the score after a few second later!
//
// drawText(50,150,"Player-1:" + final_P1_Score, "Arial", 50);
// drawText(50,250,"Player-2:" + final_P2_Score, "Arial", 50);
}
}
//TODO: Key Listener for paddles ()
//DONE: KeyListener -> Space game example
// KeyPressed Event
public void keyPressed(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.VK_P && game_status == 0){
game_status =1;
}
if(event.getKeyCode() == KeyEvent.VK_P && game_status == 2){
game_status =1;
}
// Left Arrow
if(event.getKeyCode() == KeyEvent.VK_W) {
PP1_UP = true;
}
// Right Arrow
if(event.getKeyCode() == KeyEvent.VK_S) {
PP1_DOWN = true;
}
// Space Button
if(event.getKeyCode() == KeyEvent.VK_UP) {
PP2_UP = true;
}
if(event.getKeyCode() == KeyEvent.VK_DOWN) {
PP2_DOWN = true;
}
}
// KeyReleased Event
public void keyReleased(KeyEvent event) {
// Left Arrow
if(event.getKeyCode() == KeyEvent.VK_W) {
PP1_UP = false;
}
// Right Arrow
if(event.getKeyCode() == KeyEvent.VK_S) {
PP1_DOWN = false;
}
// Space Button
if(event.getKeyCode() == KeyEvent.VK_UP) {
PP2_UP = false;
}
if(event.getKeyCode() == KeyEvent.VK_DOWN) {
PP2_DOWN = false;
}
}
}
java game-engine game-physics
java game-engine game-physics
edited Dec 28 '18 at 7:31


c0der
8,21051744
8,21051744
asked Dec 28 '18 at 3:03


Axis
1,1041922
1,1041922
1
FYI,== true
is always redundant.
– shmosel
Dec 28 '18 at 3:34
I think you have a problem inball_update
method where you must validate theball_X
should never go beyond boundary or max_X
– shriyog
Dec 28 '18 at 3:36
I got it @shmosel thanks for it.
– Axis
Dec 28 '18 at 3:58
@shriyog I am updating ball_x with P1_hit.. values. Might I missed your point can you give a bit more detail please
– Axis
Dec 28 '18 at 3:58
@Axis Can you please point me out where it's done? Is itFixme
location? The variable names are a bit confusing.
– shriyog
Dec 28 '18 at 4:03
|
show 2 more comments
1
FYI,== true
is always redundant.
– shmosel
Dec 28 '18 at 3:34
I think you have a problem inball_update
method where you must validate theball_X
should never go beyond boundary or max_X
– shriyog
Dec 28 '18 at 3:36
I got it @shmosel thanks for it.
– Axis
Dec 28 '18 at 3:58
@shriyog I am updating ball_x with P1_hit.. values. Might I missed your point can you give a bit more detail please
– Axis
Dec 28 '18 at 3:58
@Axis Can you please point me out where it's done? Is itFixme
location? The variable names are a bit confusing.
– shriyog
Dec 28 '18 at 4:03
1
1
FYI,
== true
is always redundant.– shmosel
Dec 28 '18 at 3:34
FYI,
== true
is always redundant.– shmosel
Dec 28 '18 at 3:34
I think you have a problem in
ball_update
method where you must validate the ball_X
should never go beyond boundary or max_X– shriyog
Dec 28 '18 at 3:36
I think you have a problem in
ball_update
method where you must validate the ball_X
should never go beyond boundary or max_X– shriyog
Dec 28 '18 at 3:36
I got it @shmosel thanks for it.
– Axis
Dec 28 '18 at 3:58
I got it @shmosel thanks for it.
– Axis
Dec 28 '18 at 3:58
@shriyog I am updating ball_x with P1_hit.. values. Might I missed your point can you give a bit more detail please
– Axis
Dec 28 '18 at 3:58
@shriyog I am updating ball_x with P1_hit.. values. Might I missed your point can you give a bit more detail please
– Axis
Dec 28 '18 at 3:58
@Axis Can you please point me out where it's done? Is it
Fixme
location? The variable names are a bit confusing.– shriyog
Dec 28 '18 at 4:03
@Axis Can you please point me out where it's done? Is it
Fixme
location? The variable names are a bit confusing.– shriyog
Dec 28 '18 at 4:03
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953221%2fjava-pong-game-paddles-bouncing-problem-when-it-touch-up-end-of-the-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953221%2fjava-pong-game-paddles-bouncing-problem-when-it-touch-up-end-of-the-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
dd3F 8IEIJg,O,j7gLSLX09DP hikH3qp8tg1cNS,io6SxuIuSJUayz wK3bgQIrn
1
FYI,
== true
is always redundant.– shmosel
Dec 28 '18 at 3:34
I think you have a problem in
ball_update
method where you must validate theball_X
should never go beyond boundary or max_X– shriyog
Dec 28 '18 at 3:36
I got it @shmosel thanks for it.
– Axis
Dec 28 '18 at 3:58
@shriyog I am updating ball_x with P1_hit.. values. Might I missed your point can you give a bit more detail please
– Axis
Dec 28 '18 at 3:58
@Axis Can you please point me out where it's done? Is it
Fixme
location? The variable names are a bit confusing.– shriyog
Dec 28 '18 at 4:03