# Proccessing Game Over funktion

**URL:** https://discourse.processing.org/t/proccessing-game-over-funktion/7403
**Category:** Coding Questions
**Created:** [January 11, 2019, 12:43am UTC](https://discourse.processing.org/t/proccessing-game-over-funktion/7403 "2019-01-11T00:43:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![TheSkyCrafter](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@TheSkyCrafter](https://discourse.processing.org/u/TheSkyCrafter)
#### Post date: [January 11, 2019, 12:43am UTC](https://discourse.processing.org/t/proccessing-game-over-funktion/7403/1 "2019-01-11T00:43:42Z")

</div>

Hello, I have created a little game in progress. You can move the character in the X position and fall down from above. You have a random X position and a random speed. If you run into an obstacle, I want the Gamestate variable to be set to 0 and die. I am new at Processing and do not know how to do that, can someone help me please?

Here is my code:

```auto
PImage bg,bg1,P1,fb;
int gamestate = 0;
int x,fbx, fby = - 200;
int speed = 5;
int score = 0 ;
int hgt = height;
int maus;

void setup()
{
  //size(displayWidth,displayHeight); 
  size(600, 900);
  orientation(PORTRAIT);
  bg = loadImage("bgw.png");
  bg1 = loadImage("bg1.jpg");
  P1 = loadImage("P1.png");
  fb = loadImage("fb.png");
  imageMode(CENTER);
  fbx = (int)random(0, width);
  textSize(32);
  hgt = height;
}

void draw()
{
  maus = mouseX;
  //TEST IF MOUSE IS CLICKING
    if (mousePressed){
       gamestate = 1;
        }
    else {
       gamestate = 0;
  }
  //x = width/2;
  
  // START SCREEN
  fby += speed;
    if(gamestate == 0) {
      image(bg1, width/2,height/2,width,height);
      fby = -200;
      score = 0;
      fbx = (int)random(0, width);
      }
  //MAIN GAME
  else {
    background(22, 22, 22);
      //if(fby == height) {
        if(fby > hgt) {
        speed = 5;
        fbx = (int)random(0, width);
        fby = -200;
        speed = (int)random(10, 20);
        }
    image(P1, mouseX, height/8*6,width/4,height/4); // Images are 1920 by 1080
    image(fb, fbx,fby,width/3,height/3);
    text(maus, 300,800); 
    score += 1;
    }
    
//Try to make a game over funktion but its not working :(
   if (maus < fbx && x == gamestate) {
     gamestate = 0;
   }
}

```

---

<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 11, 2019, 1:30am UTC](https://discourse.processing.org/t/proccessing-game-over-funktion/7403/2 "2019-01-11T01:30:43Z")

</div>

I think one of the main issues is how the gameState variable is controlled by the mouse. You’re gonna get a lot of interference from the mouse if gameState is always set to 1 when the mouse is pressed

maybe move this code to void mousePressed() where it doesn’t check every frame?

also I don’t have your images… maybe convert to them rect() whenever you post code 😉

---

<div class="post-metadata">

### Author: ![TheSkyCrafter](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@TheSkyCrafter](https://discourse.processing.org/u/TheSkyCrafter)
#### Post date: [January 11, 2019, 2:37am UTC](https://discourse.processing.org/t/proccessing-game-over-funktion/7403/3 "2019-01-11T02:37:57Z")

</div>

I’ve uploaded my files here: [https://drive.google.com/drive/folders/1CWFbBu6QzyyzABam7k\_J57AhD8u\_B7n1?usp=sharing](https://drive.google.com/drive/folders/1CWFbBu6QzyyzABam7k_J57AhD8u_B7n1?usp=sharing)

The Problem of puting the Code in the "if (mousePressed){ " is that you can´t die. Whenever you are pressing the mouse the game will keep runing. Thats why I created the gamestate variable.

---

<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 11, 2019, 3:22am UTC](https://discourse.processing.org/t/proccessing-game-over-funktion/7403/4 "2019-01-11T03:22:41Z")

</div>

Okay. Well as long as the game is running the player will never be able to die, since gameState will always be equal to 1

also the code

```auto
if (maus < fbx && x == gamestate) {
gamestate = 0;
}

```

will never run since you haven’t set x equal to anything

I think adding this code to the end will fix one of your problems

```auto
void mousePressed(){
gamestate=1;
}

```

this is the code that causes your player to never die

```auto
if (mousePressed){
gamestate = 1;
}
else {
gamestate = 0;
}

```
