We are about to switch to a new forum software. Until then we have removed the registration on this forum.
For my computing class, I have to make a game that starts with a main menu screen, and when a key is pressed, moves into the game. While searching for how to do this, I found information about states, but I am not quite sure how to make this work for my code. This is what I have so far.
int u=325; //y-value for soccer ball
int v=250; //x-value for soccer ball
int w=0;//x-value for goal
int x=150; //y-value for easy
int y=90; //y-value for medium
int z=35; //y-value for difficult
int savedTime; //save the time
int totalTime=35000;
PImage goal; //goal image
PImage background; //background image
PImage ball; //soccer ball image
int score = 0;
int state=0;
final int mainMenu=0;
final int game=1;
void settings(){
size(500, 375);
}
void setup() {
//Makes images appear
goal=loadImage("Soccer Goal.png");
background=loadImage("Field picture.jpg");
ball=loadImage("Soccer ball.png");
savedTime=millis();
}
void draw() {
switch(state) {
case mainMenu:
background(255);
if(key=='z'){
break;
}
case game:
break;
}
//background must be in start of draw, otherwise unwanted trail occurs behind pictures
background(background);
textSize(30);
text("Score: " + score, 30, 30);
image(ball, v, u, 40, 40);
int passedTime=millis()-savedTime;
if (passedTime>totalTime) {
fill(255);
stroke(255, 10, 10);
rect(-20, 80, 520, 295); //background box
fill(255, 10, 10); //text color
text("GAME OVER", 165, 200);
text("PRESS X TO PLAY AGAIN",75,235);
if(key=='x'){
setup();
draw();
score=0;
}
}
if (score <= 8) {
game1();
}
if (score > 8 && score <= 16) {
game2();
}
if (score > 16) {
game3();
}
if (passedTime>totalTime) {
fill(255);
stroke(255, 10, 10);
rect(0, 80, 500, 295); //background box
fill(255, 10, 10); //text color
text("GAME OVER", 165, 200);
text("PRESS X TO PLAY AGAIN",75,235);
if(key=='x'){
setup();
draw();
score=0;
}
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == RIGHT) {
v=v+3;
}
if (keyCode == LEFT) {
v=v-3;
}
if (keyCode == UP) {
u=height-285; //difficult goal ball shoot
if (score <= 8) {
if(v > w && v < w +250) {
score++;
}
}
if (score > 8 && score <= 16) {
if(v > w && v < w +200) {
score ++;
}
}
if (score > 16) {
if(v > w && v < w +150) {
score ++;
}
}
}
if (keyCode == DOWN) {
u=325;
}
}
}
Answers
Why do you have key==z in line 34?
You are in the right track. You should do something like this:
And you switch between your menu and your game by changing the value of state. For example, in the above code and change the state by a mouse event. You should adapt your code to your needs.
Kf
I have an opening screen now, and it moves into the game when the key is pressed. When I go to shoot the ball, though, the game pulls back up the start screen.
Yes... ehhh, don't call draw and setup inside runGame(). The purpose of your program is to call your menu OR your game inside draw. The function draw() runs 60 times per second! Please check the reference:
https://processing.org/reference/draw_.html
Kf
I tried moving that section out of runGame() and it made the timer for the game to end stop working as well as the game's ability to reset without doing anything about the main menu redisplaying. I also tried to make the section into it's own stage, but it had the same effect.