We are about to switch to a new forum software. Until then we have removed the registration on this forum.
` // variables for the ball int ball_width = 15, ball_height = 15; int ballX = width/2, ballY; // // variables for the paddles int paddle_width = 20, paddle_height = 200; int paddle1 = 60, paddle2; // // direction variables int directionX = 15, directionY = 15; // // variables for the score int scorecounter = 0; // //game states boolean playing = false, gameover = false, finalscore = false, score = true;
void setup () {
size (1900, 1300); // the field game is going to be 1900x1300 px big
rectMode (CENTER);
paddle2 = width - 60;
}
void draw () {
background (0); // black background
playing ();
contact ();
gameover ();
finalscore();
}
//
void playing () {
if (keyPressed == true) { // starts the game and makes the restart possible
playing = true;
gameover = false;
ballX = width/2;
ballY = height/2;
}
if (!playing) { // playing = false
fill(255);
textSize(80);
textAlign(CENTER);
text("Press Space to Play", width/2, height/4);
fill (255);
ellipse (width/2, height/2, ball_width, ball_height); // this is the starting point of the ball
fill (255, 10, 20);
rect(paddle1, (height/2), paddle_width, paddle_height); // red pong
fill (60, 255, 0);
rect(paddle2, (height/2), paddle_width, paddle_height); // green pong
}
if (playing) { // playing = true
score(); // does what i wrote down in void score () -- keeps track of the score
ballX = ballX + directionX;
ballY = ballY + directionY; // gives the directions of the ball
fill (255);
ellipse (ballX, ballY, ball_width, ball_height); // ball itself
fill ( 255, 10, 20 );
rect(paddle1, mouseY, paddle_width, paddle_height); // red pong
fill ( 60, 255, 0 );
rect(paddle2, mouseY, paddle_width, paddle_height); // green pong
if ( ballY > height ) {
directionY = -directionY;
} // if the ball reaches the lower wall it will bounce off
if ( ballY < 0 ) {
directionY = -directionY;
} // if the ball reaches the upper wall it will bounce off
if ( ballX > width || ballX < 0 ) {
gameover = true; } // if the ball reaches one of the bounderies it will be game over
}
}
void contact () {
if (ballX + ball_width/2 < paddle1 + paddle_width/2 || ballX + ball_width/2 < paddle2 - paddle_width/2) {
directionX = -directionX;
}
if (ballY + ball_width/2 < paddle1 + paddle_height/2 || ballY + ball_width/2 < paddle2 + paddle_height/2) {
directionX = -directionX;
}
if (ballY + ball_height/2 > paddle1 + paddle_height/2 || ballY + ball_height/2 > paddle2 + paddle_height/2) {
gameover = true;
}
score ();
}
void gameover () {
if (gameover) {
background (0);
} // it overrides the scorecounter with a black background
finalscore (); // gives me the final score + play again option
score = false;
}
void score () {
if (playing) {
fill(255);
textSize(45);
textAlign(CENTER);
text ( scorecounter, width/2, height/4); // keeps the score on the display while playing
if (ballX + ball_width/2 < paddle1 + paddle_width/2 ) {
scorecounter = scorecounter + 10;
}
if (ballX + ball_width/2 > paddle2 - paddle_width/2) {
scorecounter = scorecounter + 10;
}
// if the ball touches one paddle the score will be added by 10 points
}
}
void finalscore () {
if (gameover) {
score = false; // the scorecounter will stop counting
fill(255);
textSize(45);
textAlign(CENTER);
text("Game Over. Press a key to play again.", width/2, height/4);
fill(255);
textSize(80);
textAlign(CENTER);
text("You scored " + scorecounter + " points", width/2, (height/4) * 3); // the final score will appear here
}
} `
I need the ball to bounce off only when it hits the paddle, so not as it is doing now - bouncing off when it reaches the y - axis of the paddle, no matter whether the paddle is there not.
Answers
Remove line 71 to 73?
When your paddles are on the left and right side
you need to check the y of the ball against y top and bottom of the paddle as I have shown
PM me