We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Trying to make it so that if you hit the moving balls its a "gameover", screen crashes/shut down, and make it so that if you reach the finish line the game also shuts down, any advice?
-Code
int x = 100;
int y = 0;
int b = 500;
int c = 500;
int dx = 5;
int v = 500;
int vx = 5;
int s = 500;
int sx = 5;
int marve = 500;
int sherl = 5;
int h = 500;
int hx = 5;
int j = 500;
int jx = 5;
void setup(){
size (1000, 1000);
}
void draw(){
background(0);
textSize(32);
text("FINISH", 450, 950);
fill(0, 102, 153);
textSize(20);
text("X: " + x, 5,25);
text("Y: " + y, 100,25);
textSize(20);
text("Made by Sherlyn and Marvellous", 650,25);
fill(#FF1242);
ellipse ( c,300, 50,50);
c += vx;
if(c+75 > width||c-25 < 75) {
vx *= -1;
}
fill(#FF1212);
ellipse (x, y, 40,40);
fill(#E5BD2A);
ellipse(b,100, 50,50);
b -= dx;
if(b+150 > width||b-50 < 0) {
dx *= -1;
}
fill(#12C490);
ellipse(s, 500, 50,50);
s -= sx;
if(s+25 > width||s-25 < 0) {
sx *= -1;
}
fill(#C600E5);
ellipse(marve, 700,50,50);
marve += sherl;
if(marve + 200 > width|| marve - 100 < 0) {
sherl *= -1;
}
fill(#E52A65);
ellipse(h, 600,25,25);
h -= hx;
if(h + 200 > width|| h - 200 < 0) {
hx *= -1;
}
fill(#FCFBFA);
ellipse(j, 200, 25,25);
j += jx;
if(j + 250 > width|| j - 150 < 0) {
jx *= -1;
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
y -= 8;
} else if (keyCode == DOWN) {
y += 8;
} else if (keyCode == LEFT) {
x -= 8;
} else if (keyCode == RIGHT) {
x += 8;
}
} }
Answers
Go back edit your post
Click on the gear then click edit
Two lines before and after the code
Select entire code with mouse
Then hit ctrl-o
Done and Thanks
Getting your program to quit is easy: Just call
exit()
.Adding states is a bit more work. First, number the states. For example, state #0 might be the start menu, state #1 is when game is running, and state #2 is the game over screen. Add a new variable
int state;
that tracks what state the sketch is currently in. Set it to a meaningful value insetup()
. Depending on the value, draw different things insidedraw()
. For example, if the state is 1, simulate and render the game and check if a game over has happened. If it has, then set state to the game over state's number, 2. If, instead, the state is 2 in draw, draw the game over screen!You can also do different things in other functions based on the state. Maybe key presses are only maningful in the game state, so
keyPressed()
canreturn
if the state isn't 1 (playing game state). Maybe you want the sketch toexit()
when the user clicks in the game over state, so you can add that condition.Hopefully you get the idea. Post your attempt at this for more help.