How To Create "Game Over"

edited November 2016 in Questions about Code

Hello! So I am creating a game similar to a snake game but with one individual shape moving instead of getting bigger. The issue with my code that I'm having is that when the icon hits the wall I can't figure out how to get it to be a full on Game Over where you have to press a button to restart the game. Below I have posted my code so any help that can be given is greatly appreciated!!

Logo l;
int grid = 35;
PImage BH;
PImage SC;
PImage Rink;

PVector Cup;


void setup() {

  size(800, 422);
  l = new Logo();
  frameRate(8);
  pickLocation();
  BH = loadImage("BH.png");
  SC = loadImage("SC.png");
  Rink = loadImage("Rink.jpg");

}

void pickLocation() {
  int across = width/grid;
  int down = height/grid;
  Cup = new PVector(floor(random(across)), floor(random(down)));
  Cup.mult(grid);
}

void draw() {
  background(Rink);

  if (l.capture(Cup)) {
    pickLocation();
  }
  l.death();
  l.update();
  l.show();

  image(SC,Cup.x, Cup.y, grid, grid);
}

void keyPressed() {
  if (keyCode == UP) {
    l.dir(0, -1);
  } else if (keyCode == DOWN) {
    l.dir(0, 1);
  } else if (keyCode == RIGHT) {
    l.dir(1, 0);
  } else if (keyCode == LEFT) {
    l.dir(-1, 0);
  }
}

Logo.pde

class Logo {
  float x = 0;
  float y = 0;
  float xspeed = 1;
  float yspeed = 0;
  int total = 0;
  ArrayList<PVector> tail = new ArrayList<PVector>();

  Logo() {
  }

  boolean capture(PVector pos) {
    float d = dist(x, y, pos.x, pos.y);
    if (d < 1) {
      return true;
    } else {
      return false;
    }
  }

  void dir(float x, float y) {
    xspeed = x;
    yspeed = y;
  }

  void death() {
    for (int i = 0; i < tail.size(); i++) {
      PVector pos = tail.get(i);
      float d = dist(x, y, pos.x, pos.y);
      if (d < 1) {
        println("starting over");
        total = 0;
        tail.clear();
      }
    }
  }

  void update() {
    if (total > 0) {
      if (total == tail.size() && !tail.isEmpty()) {
        tail.remove(0);
      }
      tail.add(new PVector(x, y));
    }

    x = x + xspeed*grid;
    y = y + yspeed*grid;

    x = constrain(x, 0, width-grid);
    y = constrain(y, 0, height-grid);
  }

  void show() {
    fill(255);
    for (PVector v : tail) {
      rect(v.x, v.y, grid, grid);
    }
    image(BH, x, y, grid, grid);
  }
}

Answers

  • Answer ✓

    Please note that you created an object Logo and assigned to the letter >> l <<. That is not a good idea as l could be confused with a 1. Plus your code will be hard to follow and to understand. Clarity is more important than size. Important!

    It is not possible to run your code since the images are not available :-??

    Does your code work and does it start over when a death event happens?

    One suggestion is to have a message box. Check the code in this link, which I will post below next: https://forum.processing.org/two/discussion/comment/75357/#Comment_75357

    import javax.swing.*;
    
    void setup(){
      size(300,300);
    }
    
    void draw(){
      background(240);
      fill(0);
      text(millis(), 40,50);
    }
    
    void keyTyped(){
      if(key == 'q')
      exit();
    }
    
    void exit(){
      noLoop();
      surface.setVisible(false);
      showMessage("Execution time : " + millis()/1000 + " seconds", "Sketch terminated");  
      super.exit();
    }
    
    public void showMessage(String message, String title) {
      String m;
      if (platform == MACOSX) {
        m = PANE_TEXT_STYLE_MACOS.replaceAll("@@TITLE@@", title);
     //   title = "";
        m = m.replaceAll("@@MESSAGE@@", message);
      } else {
        m = PANE_TEXT_STYLE_OTHER.replaceAll("@@MESSAGE@@", message);
      }
      JOptionPane.showMessageDialog(null, m, title, JOptionPane.INFORMATION_MESSAGE);
    }
    
    final String PANE_TEXT_STYLE_MACOS = "<html> <head> <style type=\"text/css\">"+
      "b { font: 13pt \"Lucida Grande\" } p { font: 11pt \"Lucida Grande\"; margin-top: 8px }"+
      "</style> </head> <b>@@TITLE@@</b> <p>@@MESSAGE@@</p>";
    
    final String PANE_TEXT_STYLE_OTHER = "<html> <head> <style type=\"text/css\">"+
      "b { font: 12pt \"Lucida Grande\" } p { font: 11pt \"Lucida Grande\"; margin-top: 8px }"+
      "</styl
    

    Another way is to black out your screen and print in the middle "game Over" and the screen will be presented for a short period of time, say 3 seconds before it restarts. For that you will need to have a boolean value in draw() checking continuously if to show the "Game Over" screen. Then when a death event happens, you activate the boolean variable. An example will explain this concept better:

    _boolean presentGameOverScreen=false;
    int splashTime;
    
    void setup() {
      size(800, 422);
      textAlign(CENTER, CENTER);
    }
    
    
    
    void draw() {
      background(0);
    
      if (presentGameOverScreen) {
        textSize(28);
        fill(255);
        text("GAME OVER\nIt will restart in 3 secs....", width/2, height/2);
    
        //Restart game after 5 second check
        if (millis()>splashTime) {
          presentGameOverScreen=false;
        }
      } else {
        textSize(18);
        fill(255,255,25);
        text("Game is running\n\n I am not death yet...\n Press a number to simulate death", width/2, height/2);
      }
    }
    
    
    
    void keyPressed() {
      if (key>='0'&&key<='9') {
        presentGameOverScreen=true;
        splashTime=millis()+3000;  //Current time + 3seconds... to reset game over aka. restart game
      }
    }
    

    I hope this helps,

    Kf

Sign In or Register to comment.