No Escape

noescape

Simple yet fun game where essentially you are a white square trying to get as far as you can in an endless hallway while dodging all the obstacles. Getting hit by the obstacle = Game Over! For some reason you are wanted and you are trying to escape while someone is firing shots from behind you. This game keeps score of how far you can go. Score is only incremented when the square moves forward. You can move the square with the left and right arrows <-/-> and jump using the space bar [space]. Obstacles, the background, and the player are all classes.

Images:

screenfirstgameingameGameOver

Code:

Everytime I draw() in the main file, I redraw the background as it moves, and the obstacle using their personal draw() methods as well as the player. When a key is pressed, the moveLeft()/moveRight()/jump() method is called on the player object. Distance is incremented an arbitrary amount of times, but only when the player is moving forward. When the obstacle’s x and y coordinate is close enough to that of the player’s, then it is considered a hit and the player has reached the end of the game and the distance is presented on screen. They are able to restart if they wish. The first time a user plays, the obstacle is always shot above the player, giving them a chance to move, but afterwards the obstacle shoots randomly between 2 heights.

Main File:

Player player;
Background background;
Obstacle obstacle;
int playerPositionX = 50;
boolean moveRight = false;
int score = 0;
int timer = 0;
int bgColor = 1;
boolean play = false;
boolean gameOver = false;


void setup() {
  size(800, 600);
  background(0);
  textSize(15);
  player = new Player();
  background = new Background();
  obstacle = new Obstacle();
}

void draw() {
  background(0);
  if (!play) {
    textSize(30);
    text("How Far Could You Possibly Get?", 180, 150);
    textSize(15);
    text("Press Spacebar to Begin", 300, 300);
  } else if (gameOver) {
    textSize(30);
    text("Game Over", 300, 150);
    textSize(15);
    fill(#ED7707);
    text("Distance: " + score, 330, 200);
    text("Press 'n' to Retry", 320, 300);
  } else {
    player.draw();
    obstacle.draw();
    stroke(#FFFFFF);
    line(0, 400, 800, 400);
    shiftBackground(false);
    if (moveRight) {
      moveCharacter(true);
      //moveRight = false;
    } else {
      moveCharacter(false);
      //moveLeft = false;
    }
    //platform.generate();
    textSize(15);
    //println("numObstacles: " + obstacle.getNumObstacles());
    text("Distance: " + score, 700, 500);
    if (player.getX() == obstacle.getX()) {
      if (player.isGrounded() == obstacle.isGrounded()) {
        //println("HIT");
        //textSize(30);
        //println("Game Over!");
        //text("Game Over!", 300, 300);
        gameOver = true;
        //stop();
        //exit();
      }
    }
  }
}

void keyPressed() {
  if (!gameOver) {
    if (keyCode == RIGHT) {
      moveRight = true;
    }
    if (keyCode == LEFT) {
      moveRight = false;
    }
    if (key == ' ') {
      if (!play) {
        play = true;
      } else {
        player.jump();
      }
    }
  } else {
    if (key == 'n') {
      gameOver = false; 
      player = new Player();
      background = new Background();
      obstacle = new Obstacle();
      score = 0;
      moveRight = false;
      playerPositionX = 50;
    }
  }
}

void shiftBackground(boolean isRight) {
  if (isRight) {
    background.moveRight();
  } else {
    background.moveLeft();
    background.setColorBasedOnScore(score);
  }
}

void moveCharacter(boolean isRight) {
  fill(#FFFFFF);
  stroke(1);
  if (isRight && playerPositionX < 600) {
    println(1);
    background(0);
    fill(bgColor);
    stroke(#FFFFFF);
    rect(0, 400, 800, 200);
    stroke(1);
    fill(#FFFFFF);
    playerPositionX+=10;
    player.moveRight();
    timer++;
    if (timer == 10) {
      score++;
      timer=0;
    }
    background.draw();
  } else if (isRight) {
    println(2);
    //  background(0);
    fill(bgColor);
    rect(0, 0, 800, 400);
    player.draw();
    //  redraw();
    shiftBackground(false);
    timer++;
    if (timer == 10) {
      score++;
      timer=0;
    }
  } else if (!isRight && playerPositionX > 50) {
    println(3);
    background(0);
    fill(bgColor);
    stroke(#FFFFFF);
    rect(0, 400, 800, 200);
    stroke(1);
    fill(#FFFFFF);
    playerPositionX-=10;
    player.moveLeft();
    background.draw();
  } else if (!isRight) {
    println(4);
    //  background(0);
    fill(bgColor);
    rect(0, 0, 800, 400);
    player.draw();
    //  redraw();
    shiftBackground(true);
  }
  line(0, 400, 800, 400);
  obstacle.draw();
  if (player.getX() == obstacle.getX()) {
    if (player.getY() == obstacle.getY()) {
      //println("HIT");
      textSize(30);
      //text("Game Over!", 300, 300);
      gameOver = true;
      //stop();
    }
  }
  textSize(15);
  fill(#ED7707);
  text("Distance: " + score, 700, 500);
}

Background:

class Background {
  int loc = 200;
  int numBackground;
  int bgColor = 1;
  int counter = 0;
  int[] colors = {#000000, #FFFFFF, #343434, #505050, #838383, #9D9D9D, #BCBCBC, #DBDBDB, #F0F0F0, #FFFFFF};

  Background() {
    stroke(#FFFFFF);
    line(100, 0, 100, 400);
    numBackground = 1;
    draw();
  }

  void setColorBasedOnScore(int score) {
    //bgColor = colors[(score%2)];
    println("bgColor: " + bgColor);
  }

  public void draw() {
    counter++;
    println("Counter: " + counter);
    //if (loc % 100 == 0) {
    //  numBackground++;
    //}
    //fill(#D7DB00);
    //noStroke();
    stroke(#FFFFFF);
    for (int i = loc; i < 800; i += 200) {
      println("Loc: " + loc);
      println("i: " + i);
      println("numBackground: " + numBackground);
      fill(1);
      rect(i-50, 150, 100, 80);
      fill(#FFFFFF);
      text("Wanted", i-25, 175);
      rect(i-10, 200, 30, 30);
      //fill(bgColor);
      //ellipse(i, 200, 50, 50);
      stroke(#FFFFFF);
      fill(#FFFFFF);
      //text(numBackground, i, 200);
      line(i+100, 0, i+100, 400);
    }
  }

  public void moveLeft() {
    loc -=10;
    draw();
  }

  public void moveRight() {
    loc +=10;
    draw();
  }
}

Obstacle:

class Obstacle {
  int numObstacles;
  float x;
  float y;
  float type;
  boolean isPresent;
  int speed = 10;
  Obstacle() {
    isPresent = false;
    numObstacles = 0;
  }

  public float getX() {
    return x;
  }

  public float getY() {
    return y;
  }

  public int getNumObstacles() {
    return numObstacles;
  }

  public boolean isGrounded() {
    return (y == 375);
  }

  public void draw() {
    if (!isPresent) {
      isPresent = true;
      //type = random(1, 3);
      type = 1;
      x = 0;
      float rand = random(1, 2);
      if (rand > 1.5) {
        y = 275;
      } else {
        y = 375;
      }
      if(numObstacles == 0) y = 275;
      //y = random(200, 300);
    } else {
      if (type == 1) {
        fill(#FFFFFF);
        noStroke();
        //ellipse(x, y, 20, 20);
        fill(#ED7707);
        ellipse(x, y, 20, 20);
        x+=speed;
        if (x == 800) {
          numObstacles++;
          isPresent = false;
        }
      }
    }
  }
}

Player:

class Player {
  int score;
  int x;
  int y;
  int count;

  Player() {
    x = 50;
    y = 350;
    count = 0;
    rect(x, y, 50, 50);
  }
  
  public boolean isGrounded(){
     return (y == 350); 
  }
  
   public float getX(){
   return x; 
  }
  
  public float getY(){
    return y;
  }

  public void moveRight() {
    //println("MOVE RIGHT");
    if (x < 600) {
      x+=10;
      draw();
    }
  }
  public void moveLeft() {
    //println("MOVE LEFT");
    if (x > 50) {
      x-=10;
      draw();
    }
  }
  public void draw() {
    pushMatrix();
    fill(#FFFFFF);
    stroke(1);
    rect(x, y, 50, 50);
    popMatrix();
    if (y == 250) {
      count++;
      if (count == 40) {
        y = 350;
        count = 0;
      }
    }
  }
  public void jump() {
    pushMatrix();
    //for (int i = 1; i < 51; i ++) {
    //  stroke(1);
    //  fill(1);
    //  rect(x, 350+i-1, 50, 50);
    //  fill(#FFFFFF);
    //  stroke(1);
    //  rect(x, 350+i, 50, 50);
    //}
    //for (int i = 50; i > 0; i--) {
    //  stroke(1);
    //  fill(1);
    //  rect(x, 400-i+1, 50, 50);
    //  fill(#FFFFFF);
    //  stroke(1);
    //  rect(x, 400-i, 50, 50);
    //}
    fill(1);
    noStroke();
    rect(x, y, 50, 50);
    fill(#FFFFFF);
    y = 250;
    stroke(1);
    rect(x, y, 50, 50);
    popMatrix();
  }
}

 

Leave a Reply