Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Main menu to game glitch

$
0
0

I am working on a computing project for a class, and I have run into an issue. The program is required to move from a main menu screen into a game and be able to reset. It is able to move from the main screen to the game; however, when any key is pressed on the keyboard aside from the key for the transition, the main screen displays again. Not only does it display again, but the text is significantly enlarged and moved across the screen. I am thinking it is an error in my code, but I am unsure at this point. My professor has suggested a toggle switch, but I do not know how to input one into my program. This is what I have at this point.

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

Minim m;
AudioPlayer s1;
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;
boolean menuToggle, gameToggle;

void settings(){
    size(500, 375);
}

void setup() {
  //Makes images appear
  m= new Minim(this);
  s1=m.loadFile("music.mp3",1024);
  s1.play();
  s1.loop();
  goal=loadImage("Soccer Goal.png");
  background=loadImage("Field picture.jpg");
  ball=loadImage("Soccer ball.png");
  savedTime=millis();
}

void draw() {
  switch(state) {
  case mainMenu:
    showMenu();
    break;
  }
  switch(state){
  case game:
    runGame();
    break;
  }
}

void showMenu(){
  background(255);
  fill(50);
  text("Using the arrow keys, move the soccer ball left and right",100,100);
  text("Use the up button to shoot the ball towards the moving goal",90,125);
  text("Press z to begin the game",150,150);
  if(key=='z'){
    runGame();
  }
}

void runGame(){
  //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+10;
    }
    if (keyCode == LEFT) {
      v=v-10;
    }

    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;
    }
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles