Proccessing Game Over funktion


#1

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:

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

#2

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 :wink:


#3

I’ve uploaded my files here: 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.


#4

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

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

void mousePressed(){
gamestate=1;
}

this is the code that causes your player to never die

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