making a Gameover screen when a collision occurs

edited November 2017 in Questions about Code

Hey I was wondering how I would be able to go about making a gameover screen when a collision occurs. Right now i have a static wall which is called wal2 in the code below and i'm using the dist function to detect when my mouse is touching the ellipse and what I want is for it to change to screen GAMEOVER when the collision is true.

void draw() { if (screen == MAIN_MENU) { MAIN_MENU(); } else if (screen == INSTRUCTIONS) { INSTRUCTIONS(); } else if (screen == GAMEOVER) { GAMEOVER(); } else if (screen == STORY_1) { STORY_1(); } else if (screen == LEVEL_1) { LEVEL_1(); } } ` // wall collisions// boolean collision = true;

if (collision) { screen = GAMEOVER; } else { screen = LEVEL_1; }

if (dist(wal1_x, wal1_y, mouseX, mouseY)<=162) { collision = true; } else { collision = false; }`

Answers

  • Well you have the right idea. You're using a variable (screen, in your case) to track which state the sketch is in. When it is equal to LEVEL_1, your draw() function calls the LEVEL_1() function (which, one hopes, simulates and draws level #1). When you change the value stored in this screen variable (the one that is tracking the sketch state), then a different function gets called by draw() (GAMEOVER(), for example).

    Hopefully, your GAMEOVER() function (simulates and?) draws the game over screen...?

  • ya it does but it only draws the GAMEOVER screen while the collision is occurring, if the mouse is then moved it goes back to the LEVEL_1 screen. I want it to just switch to the GAMEOVER screen once a collision occurs and stay that way

  • edited November 2017 Answer ✓

    Well, look at your code. Here, we check if a collision occurs:

    if (dist(wal1_x, wal1_y, mouseX, mouseY)<=162) {
      collision = true;
    } else {
      collision = false;
    }
    

    So now collision is either true if the collision is happening, or false if it is not.

    Next, the value of screen is set based on the value of collision.

    if (collision) {
      screen = GAMEOVER;
    } else {
      screen = LEVEL_1;
    }
    

    If there is a collision, it gets set to GAMEOVER. Great! But if the collision has stopped, then the value gets set back to LEVEL_1! Oh no, that's not what you want at all. You don't want the value set to LEVEL_1 again. Basically, you don't need the else clause in this case. Just this:

    if (collision) {
      screen = GAMEOVER;
    } 
    
Sign In or Register to comment.