Hey guys I'm pretty new to processing. I've been trying to get falling balls working for a few days now. I have ALMOST everything where I want. But not quite. When you lose, I want my timer to reset and start new when you click play again. Suggestions?
Here is my source code.
int gamestate = 1; //gamestate 1 = menu, gamestate 2 = difficulty menu, gamestate 3 = instructions, gamestate 4= game running, gamestate 5 = gameover
int time;
int nBalls = 40;
float [] ySpeed = new float [nBalls];
float [] xBall= new float [nBalls];
float [] yDir= new float [nBalls];
float [] yBall= new float [nBalls];
float [] sizes = new float [nBalls];
color [] colors = new color [nBalls];
float xBlock=100;
float boost=0;
float diff;
float speed;
float easy=40;
float medium=80;
float hard=100;
float eSpeed=0;
float mSpeed=20;
float hSpeed=40;
void setup() {
size(1000, 1000);
initBalls();
}
void draw () {
background(0);
if (gamestate == 1) {
menu();
if (mouseX> 255 && mouseX< 755 && mouseY > 200 && mouseY < 300 && mousePressed) {
gamestate = 2;
time=0;
}
if (mouseX>255 && mouseX< 755 && mouseY > 400 && mouseY < 500 && mousePressed) {
gamestate = 3;
}
}
else if (gamestate == 3) {
instructions();
if (mouseButton== RIGHT) {
gamestate = 1;
}
}
else if (gamestate == 2) {
difficulty();
if (mouseX> 255 && mouseX < 755 && mouseY > 300 && mouseY< 400 && mousePressed) {
gamestate = 4;
diff=easy;
speed=eSpeed;
}
if (mouseX> 255 && mouseX < 755 && mouseY > 500 && mouseY< 600 && mousePressed) {
gamestate = 4;
diff=medium;
speed=mSpeed;
}
if (mouseX> 255 && mouseX < 755 && mouseY > 700 && mouseY< 800 && mousePressed) {
gamestate = 4;
diff=hard;
speed=hSpeed;
}
}
else if (gamestate== 4) {
drawBalls();
moveBalls();
character();
bounceBalls();
timer();
}
else if (gamestate == 5) {
gameOver();
if (mouseX>0 && mouseX<300 && mouseY> height -100 && mouseY<height &&mousePressed) {
initBalls();
gamestate =2 ;
}
}
}
void menu() {
background(255, 100, 255);
textSize(50) ;
fill(0);
text("Falling Balls", width/2-140, height/8);
for (int i=0; i<2; i ++) {
fill(0, 0, 255);
rect(255, 200+i*200, 500, 100);
}
fill(0);
textSize(30);
text("Start", 475, 265);
text("Instructions", 425, 455);
}
void instructions() {
background(255, 255, 0);
fill(0);
textSize(20);
text("The object of the game is to avoid the balls for as long as possible.", width/6+40, height/3);
text("Use the left and right arrow keys to control the block. Press spacebar to boost.", width/6, height/3+50);
text("Try to last longer than your friends! Enjoy.", width/6+150, height/3+100);
text("Right click to return.", width/2-100, height/3+150);
}
void difficulty () {
background(0, 255, 0);
for (int i=0; i<3; i ++) {
fill(255, 0, 0);
rect(255, 300+i*200, 500, 100);
}
fill(0);
textSize(40);
text("Select a difficulty", width/2-170, height/6);
textSize(30);
text("Easy", 475, 365);
text("Medium", 450, 555);
text("Hard", 475, 750);
fill(255, 0, 0);
}
void initBalls() {
for (int i=0;i<nBalls;i++) {
xBall[i] = i*50+30;
yBall[i] = 100;
colors[i] = color(0);
sizes[i] = int(80);
yDir[i] = 1;
}
}
void drawBalls() {
for (int i=0;i<nBalls;i++) {
drawBall(xBall[i], yBall[i], sizes[i], colors[i]);
}
}
void drawBall (float x, float y, float size, color c) {
fill(c);
ellipse(x, y, size, size);
}
void moveBalls() {
for (int i=0;i<nBalls;i++) {
ySpeed [i] = (30-sizes[i]+speed);
yBall [i] += ySpeed[i]*yDir[i];
if (ySpeed[i]<20) {
ySpeed[i]+=10;
}
}
}
void bounceBalls() {
for (int i=0;i<nBalls;i++) {
if (yBall[i]<-100) {
yDir[i]=1;
colors[i] = color(random(250), random(255), random(255));
sizes[i] = int(random(15, diff));
}
if (yBall[i]>height) {
yDir[i]=-1*5;
}
}
}
void character() {
fill(255, 0, 0);
rect(xBlock, 950, 50, 50);
keyPress();
for (int i=0;i<nBalls;i++) {
if (yBall[i]>950 && yBall[i]<1000 && xBall[i]> xBlock && xBall[i]<xBlock+50) {
gamestate = 5;
}
}
}
void keyPress() { //assigns the left, right, and spacebar keys as commands for the character
if (keyPressed && (key == CODED)) {
if (keyCode == LEFT) {
xBlock-=15;
}
else if (keyCode == RIGHT) {
xBlock+=15;
}
else if (keyCode == UP && keyCode==LEFT ) { //boost function
xBlock-=30;
}
else if (keyCode == UP && keyCode==RIGHT) {
xBlock+=30;
}
}
}
void gameOver() {
background(0, 255, 255);
text("Game Over", width/2-100, height/2-100);
text("Your time was:", width/2-150, height/2-50);
text(time/1000, width/2+100, height/2-50 );
textSize(20);
fill(255, 0, 0);
rect(0, height-100, 300, 100);
fill(0);
text("Click here to play again", 25, height-40);
textSize(30);
}
void timer () {
fill(255);
time= millis();
text("Time:", 10, 30);
text(time/1000, 100, 30);
}
1