How do I make a game with a timer that stops and shows the 'scores' in the game menu

edited December 2017 in Questions about Code
First Tab
int startTime=-1;
float playerRotation = ;
int x;
int y;
int Playerx1=-8;
int Playery1=100;
int playerWidth = 17;
int playerHeight = 1000;
int obstaclex1=-100;
int obstacley1=-8;
int obstaclex2=-100;
int obstacley2=-8;
float playerRotationSpeed = 0;


float obstacleRotation1 = 0;
float obstacleRotation2 = 0;


float obstacleRotationSpeed = 0;
int obstacleWidth = -1000;
int obstacleHeight = 16;
float touch;
float buttonx=200;
int buttonxmovement=1;
float buttony=325;
boolean startscreen=true;
boolean gameplay=false;
boolean endscreen=false;
int savedTime;

void setup(){
  size(1000, 600);
}


void draw(){
   if(startscreen){
     startscreen();
   }
   if(gameplay){
     gameplay();
   }
   if(endscreen){
     endscreen();
   }
 }

Second Tab
void startscreen(){
  background(0);
  fill(255);
  noStroke();
  textSize(80);
  textAlign(CENTER);
  text("L  I  N  E   G  A  M  E",500,200);
  textSize(20);
  text("By Jacob,Kobe,Kyle, and Kyle's fren Korey",500,250);
  text("Push button to start",500,500);
  buttonx+=1*buttonxmovement;
  if(buttonx<200 || buttonx>800){
    buttonxmovement*=-1;
  }
  fill(255,0,0);
  rect(buttonx,buttony,100,100);
  if(mousePressed && mouseX<buttonx+100 && mouseX>buttonx && mouseY>buttony && mouseY<buttony+100){    
    startscreen=false;
    gameplay=true;
    endscreen=false;
    if( isOver() && startTime==-1 ){
       startTime = millis();
      }

  }
}

Third Tab
void gameplay() {
  background(0);
  x=-8;
  y=90;

  int time=second();

  if(keyCode == LEFT && keyPressed == true){
      playerRotation = playerRotation+0.08;
    }
    if(keyCode == RIGHT && keyPressed == true){
      playerRotation = playerRotation-0.08;
    }

  pushMatrix();
  ellipseMode(CENTER);
  fill(0,255,0);
  translate(width/2,height/2);
  rotate(obstacleRotation1);
   if(time<11){

    obstacleRotation1+=.02;
    obstacleRotation2+= .02;
  }
  if(time>10 && time<14){
    obstacleRotation1-=.04;
    obstacleRotation2-=.04;
  }
  if(time>13 && time<18){
    obstacleRotation1+=.03;
    obstacleRotation2+=.03;
  }
    if(time>17 && time<21){
    obstacleRotation1-=.02;
    obstacleRotation2-=.02;
  }
  if(time>20 && time<31){
    obstacleRotation1+=.04;
    obstacleRotation2+=.04;
     }
    if(time>30 && time<33){
    obstacleRotation1-=.01;
    obstacleRotation2-=.01;
  }
    if(time>32 && time<36){
    obstacleRotation1+=.04;
    obstacleRotation2+=.04;
  }
    if(time>35 && time<41){
    obstacleRotation1-=.04;
    obstacleRotation2-=.04;
  }

  if(time>40 && time<46){
    obstacleRotation1+=.03;
    obstacleRotation2+=.03;
      }
    if(time>45 && time<51){
    obstacleRotation1-=.02;
    obstacleRotation2-=.02;
  }

  if(time>50 && time<54){
    obstacleRotation1+=.04;
    obstacleRotation2+=.04;
    }
      if(time>53 && time<58){
    obstacleRotation1-=.03;
    obstacleRotation2-=.03;
  }
    if(time>57 && time<560){
    obstacleRotation1+=.01;
    obstacleRotation2+=.01;
  }
  rect(obstaclex1,obstacley1,obstacleWidth,obstacleHeight);
  ellipse(-100,0,16,16);
  rotate(radians(180));
  rect(obstaclex2,obstacley2,obstacleWidth,obstacleHeight);
  ellipse(-100,0,16,16);
popMatrix();

  obstacleRotationSpeed = obstacleRotationSpeed + 0.000002;
  playerRotationSpeed = playerRotationSpeed + 0.000002;
  obstacleRotation1 = obstacleRotation1 + .02 + obstacleRotationSpeed;
  obstacleRotation2 = obstacleRotation2+.02+obstacleRotationSpeed; 

    //the code for the red stick 
   //the touch is the small red circle 
   pushMatrix();
  fill(255, 0, 0);
  translate(width/2,height/2);
  rotate(playerRotation);
  noStroke();
  ellipseMode(CORNER);
  rect(Playerx1,Playery1,playerWidth,playerHeight);
  ellipse(x,y,17,17);
  popMatrix();

  if(playerRotation >= obstacleRotation1+1.5){
    startscreen=false;
    gameplay=false;
    endscreen=true;
  }
 if(playerRotation+1.5<= obstacleRotation2){
   text("LOOOOSE",40,40);
    startscreen=false;
    gameplay=false;
    endscreen=true;
 }
     if( startTime != -1 ){
       fill(255);
       text("SCORE:",width/2,height/2);
        text((millis()-startTime)/1000, width/2, height/2+40);
      } else {
        fill(0,0,0);
        text("SCORE:",width/2,height/2-15);
        text("0", width/2,height/2+40);
      }
}
 boolean isOver(){
      return( mousePressed && mouseX<buttonx+100 && mouseX>buttonx && mouseY>buttony && mouseY<buttony+100 );
    }

Fourth Tab
void endscreen(){
  background(0);
  fill(255);
  noStroke();
  textSize(80);
  textAlign(CENTER);
  text("G  A  M  E   O  V  E  R",500,200);
  textSize(20);
  text("Better luck next time",500,250);
  text("Click anywhere to reset",500,500);
  buttonx+=1*buttonxmovement;
  if(buttonx<200 || buttonx>800){
    buttonxmovement*=-1;
  }
  fill(255,0,0);
  rect(buttonx,buttony,100,100);
  if(mousePressed){
    startscreen=true;
    gameplay=false;
    endscreen=false;
  }
}
Tagged:

Answers

  • Use millis(). It returns the number of milliseconds since the sketch started.

    When your game starts (probably in setup (but also maybe when your player clicks a start button)), record the current number of milliseconds elapsed (millis()), plus some total number of milliseconds that your game goes for (10000 is ten seconds, for example) in a global variable (int game_over_time;)(game_over_time = millis() + 10000;). Then, in draw() (or whatever functions is simulating your game), see if the current number of milliseconds elapsed is greater than the saved value (if( millis() > game_ove_time) {). if this condition is true, then you know the game is over and you can instead display your game over screen instead of continuing the game.

    As for keeping track of a score, do essentially the same thing. Have a global variable (int score;) that starts at 0 when your game starts (score = 0;). Increment it when points are scored. When the game is over, draw this score to the screen with text().

Sign In or Register to comment.