Getting a simple game to restart
in
Programming Questions
•
3 months ago
I am working on a simple game and trying to get it to restart by either clicking a button (or the mouse). I've looked at many different topics in the forum and tried to get the code to work, but it won't restart. Everything I read previously about making a restart point in setup() makes sense, I just can't get it to work. Here is my code (I have it working without LOSER being displayed, but I'm not sure why it is permanently there now...) Any help would be appreciated!
color bg = color(220,220,220);
float ballsize=40.0;
float speed = 1;
float yball = random(ballsize,(height-ballsize));
float xball = random(ballsize,(width-ballsize));
float ball_xdirection=1;
float ball_ydirection=1;
float textspeed = 0.01;
boolean lost = false;
int points = 0;
int opacity = 250;
PFont font1;
PFont font2;
PFont font3;
PFont font4;
void setup(){
size(300,300);
font1 = loadFont("BradleyHandITCTT-Bold-48.vlw");
font2 = loadFont("Garamond-14.vlw");
font3 = loadFont("Stencil-14.vlw");
font4 = loadFont("StarJedi-14.vlw");
reset();
}
void draw(){
smooth();
fill(214,22,44);
background(bg);
strokeWeight(1);
ellipse(xball,yball,ballsize,ballsize);
xball=xball+((.5*speed)*ball_xdirection);
yball=yball+(speed*ball_ydirection);
textFont(font3);
textAlign(CENTER);
textLeading(30);
fill(0,opacity);
String s = " BOUNCING BALLS! Use your mouse to keep the ball from touching the bottom. You get a point each time you save the ball, but be careful it gets faster!";
text(s,width/2,height/2,200,200);
opacity +=-1.2*textspeed;
fill(106,8,160);
rectMode(CENTER);
rect(mouseX,280,50,15);
if(yball == 252 && xball <= mouseX+27 && xball >= mouseX-27){
speed *=1.1;
ball_ydirection *= -1;
points = points+1;
println(+points+" points for you");
}
if (xball > width-(ballsize/2) || xball<(ballsize/2)){
ball_xdirection*=-1;
}
if(yball<(ballsize/2)) {
ball_ydirection *=-1;
}
if(yball>height-(ballsize/2)){
textFont(font1);
textAlign(CENTER);
text("GAME OVER!", width/2,height/2);
textFont(font2);
textAlign(RIGHT);
fill(0);
text("Points:"+points,width-20,20);
noLoop();
lost = true;
}
if(points <= 5){
fill(69,17,245);
textFont(font1);
text("Loser!", width/2, height/3);
}
if(points >= 20){
fill(244,245,17);
textFont(font4);
text("You're a Master!",200, height/3);
}
}
void keyPressed(){
reset();
}
void reset(){
smooth();
fill(214,22,44);
background(bg);
strokeWeight(1);
ellipse(xball,yball,ballsize,ballsize);
xball=xball+((.5*speed)*ball_xdirection);
yball=yball+(speed*ball_ydirection);
}
1