I’m trying to make a game for my Processing class. It is due tomorrow morning and is almost completely finished, but I am having trouble triggering the end game screen when the ball leaves the screen. I also need the ball to do more than bounce in one place when it hits the wall. Any help is appreciated.
here is what I have.
int gameScreen = 0;
int ballX, ballY;
int ballSize = 20;
int ballColor = color(0, 0, 255);
int score = 0;
void setup() {
size(500, 500);
ballX=width/4;
ballY=height/5;
}
void draw() {
if (gameScreen == 0) {
initScreen();
} else if (gameScreen == 1) {
gameScreen();
} else if (gameScreen == 2) {
gameOverScreen();
}
}
/********* SCREEN CONTENTS *********/
void initScreen() {
background(0);
textAlign(CENTER);
text("Click to start wall pong", height/2, width/2);
}
float gravity = .5;
float ballSpeedVert = 0;
float airfriction = 0.01;
float friction = 0.1;
color racketColor = color(255, 0, 255);
float racketWidth = 10;
float racketHeight = 100;
int racketBounceRate = 1;
void gameScreen() {
background(0, 255, 0);
drawBall();
applyGravity();
keepInScreen();
drawRacket();
watchRacketBounce();
printScore();
}
void drawRacket() {
fill(racketColor);
rectMode(CENTER);
rect(mouseX, mouseY, racketWidth, racketHeight);
}
void watchRacketBounce() {
float overhead = mouseY - pmouseY;
if ((ballX+(ballSize/2) > mouseX-(racketWidth/2)) && (ballX-(ballSize/2) < mouseX+(racketWidth/2))) {
if (dist(ballX, ballY, ballX, mouseY)<=(ballSize/2)+abs(overhead)) {
makeBounceBottom(mouseY);
if (overhead<0) {
ballY+=overhead;
ballSpeedVert+=overhead;
}
score();
}
}
}
void drawBall() {
fill(ballColor);
ellipse(ballX, ballY, ballSize, ballSize);
}
void applyGravity() {
ballSpeedVert += gravity;
ballX += ballSpeedVert;
ballSpeedVert -= (ballSpeedVert * airfriction);
}
void makeBounceBottom(int surface) {
ballX = surface-(ballSize/2);
ballSpeedVert*=-1;
ballSpeedVert -= (ballSpeedVert * friction);
}
void makeBounceTop(int surface) {
ballX = surface+(ballSize/2);
ballSpeedVert*=-1;
ballSpeedVert -= (ballSpeedVert * friction);
}
void keepInScreen() {
if (ballX-(ballSize/2) < 0) makeBounceTop(0);
if (ballX+(ballSize/2) > width) endGame();
}
void lose() {
if (ballX-(ballSize/2) > -5) {
endGame();
}
}
void score() {
score++;
}
void gameOverScreen() {
background(0);
textAlign(CENTER);
fill(255);
textSize(30);
text("Game Over", height/2, width/2 - 20);
textSize(15);
text("Click to Restart", height/2, width/2 + 10);
}
void printScore() {
textAlign(CENTER);
fill(0);
textSize(30);
text(score, height/2, 50);
}
/********* INPUTS *********/
public void mousePressed() {
if (gameScreen==0) {
startGame();
}
if (gameScreen==2) {
restart();
}
}
/********* OTHER FUNCTIONS *********/
void startGame() {
gameScreen=1;
}
void endGame () {
gameScreen=2;
}
void restart() {
score = 0;
ballX=width/4;
ballY=height/5;
gameScreen = 0;
}