# Unsure if I'm programming this game to restart properly

**URL:** <https://discourse.processing.org/t/unsure-if-im-programming-this-game-to-restart-properly/7260>\
**Category:** Coding Questions\
**Created:** [January 6, 2019, 11:18pm UTC](https://discourse.processing.org/t/unsure-if-im-programming-this-game-to-restart-properly/7260 "2019-01-06T23:18:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![francium401](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@francium401](https://discourse.processing.org/u/francium401)\
**Post date:** [January 6, 2019, 11:18pm UTC](https://discourse.processing.org/t/unsure-if-im-programming-this-game-to-restart-properly/7260/1 "2019-01-06T23:18:40Z")

</div>

I attempted to reset the relevant variables back to it’s initial value whenever the space-bar button is pressed after the GAME OVER screen comes up, but something about it just isn’t right. After a second round of the game, the GAME OVER screen only appears for a split second and immediately restarts the game without me even having to press the space-bar. Why is this happening?

```auto
flake[] flake = new flake[30];
float speed = 10;
int b;
color b1 = color(0, 0, 0);
color b2 = color(141, 84, 216);
int Y_AXIS = 1;
int s = 0;
int m = 0;
int health = 3;
boolean gameOver = false;

void setup(){
 size(1000,800);
 
 for(int i = 0; i < flake.length; i++){
  flake[i] = new flake(); 
 }
}

void draw(){
  setGradient(0, 0, width, height, b1, b2, Y_AXIS);
  b = mouseY;  
  
  for(int i = 0; i < flake.length; i++){
   flake[i].show();
   flake[i].update();
  }
  
  display();  
  timer();
  healthScore();
    
}

void display() {
 fill(243, 142, 255);
 noStroke();
 triangle(917, b, 950, b-15, 950, b+15);
 
}

void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) {
  float deltaR = red(c2)-red(c1);
  float deltaG = green(c2)-green(c1);
  float deltaB = blue(c2)-blue(c1);

  if (axis == Y_AXIS) {

    for (int i=x; i<=(x+w); i++) {
      // row
      for (int j = y; j<=(y+h); j++) {
        color c = color(
          (red(c1)+(j-y)*(deltaR/h)), 
          (green(c1)+(j-y)*(deltaG/h)), 
          (blue(c1)+(j-y)*(deltaB/h)));
        set(i, j, c);
      }
    }
  }
}

void timer(){
  textAlign(100,100);
  textSize(30);
  
  if(s<=59){
   text(m+":"+s,100,100);
   s=s+1;
  }
  else{
   m=m+1;
   s=0;
   text(m+":"+s,100,100);
  }
}

void healthScore(){
  textSize(32);
  text("Health: "+health, 100, 50); 
  
    if(health == -1){
    gameOver = true;
  }
  
  if (gameOver == true){
    background(0);
    text("GAME OVER.", 100,100); 
    text("YOU SURVIVED FOR: ", 100,200); 
    text(m+ " SECONDS", 100,300); 
    s=0;
    textSize(20);
    text("PRESS SPACEBAR TO PLAY AGAIN", 100,500); 
    textSize(30);
    if(m<20){
      text("YOU CAN DO BETTER!", 100,400); 
    }
    if(20<m && 60>m){
      text("NOT TOO BAD!", 100,400); 
    }
    if(m>60){
      text("EXCELLENT SCORE!", 100,400); 
    }
    if (key == ' ') {    
      gameOver = false;    
      health = 3;
      m=0;
      s=0;      
    }
  } 
}

```

```auto
class flake {
  float y=random(height);
  float x=random(-width);

  float z = random(20, 100);
  float size=map(z, 1, 30, 3, 20);
  float speed=map(z, 1, 30, 0.1, 5);

  boolean active = true;

  void show() {
    if (active) {
      stroke(248, 188, 255);
    } else {
      stroke(7, 255-188, 0);
    }
    strokeWeight(size);
    point(x, y);
  }

  void update() {
    x+=speed;

    if (x>width) reset();
    float x1 = 917;     
    float y1 = b;
    float x2 = 950;
    float y2 = b-15;
    float x3 = 950;
    float y3 = b+15;
    if (active) {
      if (triPoint(x1, y1, x2, y2, x3, y3, x, y)) {
        println("hit");
        health--;
        active=false;
      }
    }
  }

  void reset() {
    y = random(height);
    x = random(-width, 0);
    z = random(20, 100);
    size=map(z, 1, 29, 3, 30);
    speed=map(z, 1, 29, 0.1, 5);
    active=true;
  }
}
boolean triPoint(float x1, float y1, float x2, float y2, float x3, float y3, float x, float y) {

  float areaOrig = abs( (x2-x1)*(y3-y1) - (x3-x1)*(y2-y1) );

  float area1 = abs( (x1-x)*(y2-y) - (x2-x)*(y1-y) );
  float area2 = abs( (x2-x)*(y3-y) - (x3-x)*(y2-y) );
  float area3 = abs( (x3-x)*(y1-y) - (x1-x)*(y3-y) );

  if (area1 + area2 + area3 == areaOrig) {
    return true;
  }
  return false;
}

```

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 7, 2019, 12:12am UTC](https://discourse.processing.org/t/unsure-if-im-programming-this-game-to-restart-properly/7260/2 "2019-01-07T00:12:53Z")

</div>

Hello again 🙂 There’s a very simple reason your code isn’t working. checking if _key == ’ ’_ (or any other character) will return true since it isn’t specified to check whether or not the key is actually pressed. You must also check if the global _keyPressed_ variable is true as well:

```auto
if (key == ' '&&keyPressed) {    
      gameOver = false;    
      health = 3;
      m=0;
      s=0;      
    }

```

Or you can put the code inside void keyPressed() or void keyReleased(), where it automatically checks to see if the key is pressed.

```auto
void keyPressed(){
if (key == ' ') {    
      gameOver = false;    
      health = 3;
      m=0;
      s=0;      
    }
}

```
