We are about to switch to a new forum software. Until then we have removed the registration on this forum.
so the code does not perform two things I need him to do. First I need the ball to bounce off the little paddles and second I need the game to restart once I reach gameover and press a key. can someone help me with this?! please. an explanation with whatever answer you give would be nice.
thx abigail
// variables for the ball
int ball_width = 15, ball_height = 15;
int ballX = width/2, ballY = height/2;
//
// variables for the paddles
int paddle_width = 20, paddle_height = 150;
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 ();
gameover ();
finalscore();
}
//
void playing () {
if (keyPressed == true) {
playing = true;
}
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();
ballX = ballX + directionX;
ballY = ballY + directionY;
fill (255);
ellipse (ballX, ballY, ball_width, ball_height);
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 (ballX == paddle1 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
if (ballX == paddle2 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
}
void gameover () {
if (gameover) {
background (0);
}
finalscore ();
score = false;
if (keyPressed) {
playing = true;
}
}
void score () {
if (playing) {
fill(255);
textSize(45);
textAlign(CENTER);
text ( scorecounter, width/2, height/4);
if (ballX == paddle1 && ballY <= paddle_height) {
scorecounter = scorecounter + 10;
}
if (ballX == paddle2 && ballX <= paddle_height) {
scorecounter = scorecounter + 10;
}
}
if (!playing) {
score = false;
}
}
void finalscore () {
if (gameover) {
score = false;
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);
if (keyPressed == true) {
background (0);
playing = !playing;
}
}
}
Answers
for reset
in line 105
reflect