David – Midterm

zip file : Midterm_IM

This surely was the project in which I tried my best to incorporate everything we have learned so far. Initially, I wanted to make a game that resembles an already existing game called, “Flappy Bird”, but I diverted my plan to render something different and original to a certain extent. The game’s goal is to fly out of the mysterious cave by avoiding blocks of flying lava. Prior to the instructions of the game, which is fairly simple, I gave a short dystopian background narrative that the player somehow ended up in the cave because things fell apart ever since 2020.

I made the instructions fairly easy. The player will be floating along the cave and the only way to maneuver is to use UP, DOWN, RIGHT (to speed up) keys to avoid the flying lava bombs. If you successfully get out of the cave, you will have to go back to the society, and that requires MONEY. Other people have also attempted to get out of the cave, only to be buried there. So you will pick up the coins that the previous adventurers have left behind and follow their legacy… If you fail, you will become one of them.

I used a few images of caves and rocks to design the game as follow.

Cave1Cave2Rock1 Rock2

To talk more about the functionalities of the game, a player’s health decreases everytime he hits a block of lava. It will keep decreasing until your lives (initally out of 3) have fully depleted. You will then have to restart from the beginning. The placement of coins and lava bombs will be randomly assigned for every level. A very short song from PacMan will be played once you start the game and during the game a player will be able to hear sound effects when he hits a coin or a lava. You will see that there is a flashlight effect on the instructions page that was employed to create a more eerie and dystopian atmosphere.

Here is what it looks like:

The code is as follows:

import ddf.minim.*;

PFont font, font2;

Player player;
ArrayList<Coin> coins;
ArrayList<Bomb> bombs;
boolean [] keys = new boolean[128];
boolean instruction = true;
boolean gameover = true;
PImage photo, cave, rock, rock2;

color player_c = color(100, 200, 200);

int myPlayer = 3;
int myCoins = 0;
float myHealth = 100;

//sound effect
Minim minim;
AudioPlayer sound, sound2, sound3;

void setup(){
  size(800, 400);
  background(0);
  smooth();
  rectMode(CENTER);
  font = createFont("Trattatello",60);
  font2 = createFont("Arial",16);
  
  player = new Player();

  coins = new ArrayList<Coin>();
  for(int i=0; i < 10; i++){
    coins.add(new Coin());
  }

  bombs = new ArrayList<Bomb>();
  for(int i=0; i < 15; i++){
    bombs.add(new Bomb());
  }
  minim = new Minim(this);
  sound = minim.loadFile("1.aif");
  sound2 = minim.loadFile("2.mp3");
  sound3 = minim.loadFile("3.mp3");
  sound.setGain(-5);
  sound2.setGain(-5);
  sound3.setGain(-3);
  sound3.play();
  sound3.rewind();
  
}

void draw(){
  if (instruction){
    photo = loadImage("cave.jpg");
    //Using the width and height of the photo for the screen size
    photo.resize(width,height); 
    image(photo,0,0);
    loadPixels();
    photo.loadPixels();
    for(int x= 0; x<width; x++){
      for(int y=0; y<height; y++){
        //location in the pixel array
        int loc = x+y*width;
        float r = red(photo.pixels[loc]);
        float g = green(photo.pixels[loc]);
        float b = blue(photo.pixels[loc]);
        //get distance between the cursor and the pixel
        float d = dist(mouseX,mouseY,x,y);
        float mult = map(d,0,300,1.8,0);
        //the closer the distance, the brighter.
        pixels[loc] = color(r*mult,g*mult,b*mult);
      }
    }
    updatePixels();
    textFont(font);
    fill(#FFFF00);
    text("Cave Escape",width/2-200,height/2-70);
    textFont(font2);
    fill(255);
    text("It's year 2020... Things went south...\nIn fact, so south that you got stuck in a mysterious cave...\nSurvive the cave of HELL with full of GOLD COINS.\nMake your way out through flying lava bombs.\n\nTo maneuver, press → ↑ ↓\nPress ANY KEY to start",width/2-200,height/2-20);
    return;
  }
  
  background(0);
  cave = loadImage("cave2.jpg");
  //Using the width and height of the photo for the screen size
  cave.resize(width,235); 
  image(cave,0,67);
  caveStone();
  
  for (int i = coins.size() -1; i >= 0; i--){
    Coin c = coins.get(i);
    c.display();
    
    if(player.checkCollisionCoins(c)){
      sound2.rewind();
      sound2.play();
      
      myCoins += 10;

      coins.remove(i);
    }
  }
  
  for (int i = bombs.size() -1; i >= 0; i--){
    Bomb b = bombs.get(i);
    b.display();
   
    if(player.checkCollisionBombs(b)){
      sound.rewind();
      sound.play();
      player_c = color(255, 0, 0);
      myHealth -= 1.3;
      if(myHealth <= 0){
        myPlayer -= 1;
        myHealth = 100;
      }
    } 
  }

  player.display();
  player.move();
 
  //score
  textSize(15);
  fill(255);
  text("Player life = " + myPlayer, 60, 30, 255); // show life
  textAlign(LEFT);
  text("Coins = " + myCoins, 10, 55, 255); // show coins gathered 
  textAlign(LEFT);
  text("Health = " + myHealth, 110, 55, 255); 
  
  if(myPlayer == 0){
    setup();
    myCoins = 0;
    myHealth = 100;
    myPlayer = 3;
  } else {
    player_c = color(100, 200, 200);
  }

}


//Player
class Player{
  PVector pos;
  PVector acc;
  PVector vel;
  PVector speed;
  
  Player(){
    pos = new PVector(-5, height/2);
    acc = new PVector(0.8, 0.8);
    vel = new PVector(0,50);
    speed = new PVector(0.9,0);
  }
  
  void display() {
    fill(player_c);
    rect(pos.x, pos.y,30,30, 7);
  }
  
  void move(){
    vel.mult(5);
    vel.limit(10);
    vel = vel.add(acc);
    pos.add(speed);
    
    if(pos.x < -50){
      pos.x = width; 
    }
    if(pos.x > width){
      pos.x = -50;  
      bombs = new ArrayList<Bomb>();
      coins = new ArrayList<Coin>();
      for(int i=0; i < 10; i++){
        coins.add(new Coin());
      }
      for(int i=0; i < 15; i++){
        bombs.add(new Bomb());
      }
    }
    if(pos.y > height - 120 ){
      pos.y = height - 120; 
    }
    if(pos.y < 120){
      pos.y = 120; 
    }
    
    if(keys[RIGHT])
      pos.x += vel.x;
    if(keys[UP])
      pos.y -= vel.y;
    if(keys[DOWN])
      pos.y += vel.y;
  }
  
  boolean checkCollisionBombs(Bomb b){
     if(dist(player.pos.x, player.pos.y, b.pos.x, b.pos.y) <= b.b_size){
        return true; 
      } else {
        return false;
      }
  }

  boolean checkCollisionCoins(Coin c){
    if(dist(player.pos.x, player.pos.y, c.pos.x, c.pos.y) < c.radius){
        return true;
      } else {
        return false;
      }
  }
}

class Coin{
  
  PVector pos;
  PVector acc;
  PVector vel;
  color c;
  float radius;
  
  Coin(){
    pos = new PVector(random(50, width), random(110, height-110));
    acc = new PVector(0,0);
    vel = new PVector(0,0);
    radius = 20;
    c = color(255, 204, 0);
  }
  
  void display(){
    pushMatrix();
    noStroke();
    translate(pos.x, pos.y);
    fill(80);
    ellipse(2,2, radius, radius);
    fill(c);
    ellipse(0,0,radius, radius);
    fill(0);
    textSize(15);
    textAlign(CENTER);
    text("$",0,4);
    popMatrix();
  }
}


class Bomb {
  PVector pos;
  PVector acc;
  PVector vel;
  float b_size = 25;
  color c;
  float direction = random(2,8);
  
  Bomb (){
    pos = new PVector(random(100, width), random(110, height- 110));
    acc = new PVector(0,0);
    vel = new PVector(0,0);
    c = color(255, 0, 0);
  }
  
  void display(){
    pushMatrix();
    translate(pos.x, pos.y);
    fill(c);
    rect(0, 0, b_size, b_size);
    pos.y += direction;
    if (pos.y < 110 || pos.y > height-110){
      direction *= -1;
    }
    popMatrix();
  }
  
}

void keyPressed(){
  if (instruction) {
    instruction = false;
  }
  else if (gameover){
    gameover = false;
  }
  else {
    keys[keyCode] = true;
  }
}

void keyReleased(){
  keys[keyCode] = false;
}



void caveStone(){
  rock = loadImage("base.jpg");
  rock2 = loadImage("base2.jpg");
  rock.resize(30,30);
  rock2.resize(30,30);
  
  for(int i = 0; i <= width; i += 30) {
    image(rock,i,67);
    image(rock,i,302);
  }
  for(int j = 0; j <= width; j += 90) {
    image(rock2,j,67);
    image(rock2,j,302);
  }
}

 

Leave a Reply