COVID NINJA – Rhythm Kukreja’s Midterm Game

Inspiration:

As I mentioned in my proposal, my game was inspired by Fruit Ninja. Apart from slicing the fruits, you had to catch the covid objects, like pills, syringes, sanitizers, and masks. You also had to avoid catching the virus. There is also a powerup in the game, that looks like a clock, which slows the frameRate for about 5 seconds.

Process:

The game starts by showing a start screen, which gives the instructions for the game. Then, when you click any key on the keyboard, the game starts.

I used the keypressed function in order to switch from the start screen to the main game.

After that, you can see a glove and objects falling from the top. The glove is made on mouse X and mouse Y. The objects use a random function on the width, to fall randomly from different sides and I have used gravity along with velocity to randomise the speed of the objects falling from the top.

Everytime, you catch the object, score increases by 1 and everytime you miss an object, the X on top gets red. If you miss 10 objects, the game ends. If you catch the virus game ends. I have used null to remove the objects.

Once the game ends, you see the following screen and I have again used keypressed function to restart the game, once the game ends.

This is what the game looks like:-

The code for the above game is below:-

//setting up the variables, importing libraries, importing images and fonts. 
import processing.sound.*;
SoundFile file;
SoundFile objects;
SoundFile bomb;
SoundFile gameover;
PImage back;
PImage knife;
PImage start;
PFont font;

int height = 720;
int width = 1040;
int frame = 0;



//initialising variables
Game game = new Game();
boolean gameOver = false;
boolean startScreen = true;

void setup(){
  //setting up the screen and the background
  size(1040, 720);
  back = loadImage("back.jpg");
  background(0);
  font = createFont("Optima-Bold", 20);
  
  //accessing sound path
  file = new SoundFile(this, "main.wav");
  objects = new SoundFile(this, "objects.mp3");
  bomb = new SoundFile(this, "bomb.mp3");
  gameover = new SoundFile(this, "gameover.mp3");
  
  //loading the images
  knife = loadImage("knife.png");
  knife.resize(100,150);
  
  start = loadImage("start.jpeg");
  start.resize(width,height);
  
  //looping the background music
  file.amp(0.05);
  file.loop();
}

void draw(){
  // setting font to optima bold
  textFont(font);
  
  //changing cursor settings
  if(startScreen || gameOver){
    cursor(CROSS);
  } else {
    cursor(knife);
  }
  
  // start screen for instructions
  if(startScreen){
    background(start);
    strokeWeight(0);
    fill(200,0,0,100);
    rect((width/2)-400,(height/2)-150,800,400);
    
    fill(200,0,0,200);
    stroke(255);
    textSize(80);
    textAlign(CENTER);
    text("COVID NINJA", (width/2)-20,(height/2)-180);
    
    fill(255);
    textSize(40);
    textAlign(CENTER);
    text("INSTRUCTIONS", (width/2)-20,(height/2)-100);
    
    textSize(30);
    textAlign(CENTER);
    text("Catch every object on the screen apart from the virus. \n If you catch the virus, you die. If you catch the clock, \n the time slows and the objects move slower. Use the cursor \n to catch the objects", (width/2),(height/2)-50); 
    text("PRESS ANY KEY TO START",(width/2), (height/2)+150);
    textAlign(BASELINE);
    
  }
  
  //condition to remove the start screen after keypressed
  if(keyPressed){
    startScreen = false;
  }
  
  //starting the game
  if(!startScreen){
    background(back);  
    //score change
    fill(255); textSize(30); text("SCORE: ", 40, 50);
    text(game.score, 150, 50);
    
    //x's turing red after every miss
    for(int i = 0; i < 10; i++){
      if(i < game.missed){ 
           fill(255,0,0); 
      } 
      else { 
            fill(220, 220, 220); 
          }
      textSize(40);
      text("x", width - 40 - (30*i), 50);
    }
    
    game.display();
    
    //restarting the game after game is over
    if(gameOver == true && keyPressed){
      game = new Game();
      frameRate(60);
      gameOver = false;
    }
  }
  
}

//setting up class covid with variables
class Covid {
  int x, y;
  float vy, g;
  boolean virus = false;
  boolean clock = false;
  PImage img;
  int img_w, img_h;
  
  Covid() {
    x = (int) random(150, width-150);
    y = 60;
    g = random(0.1,0.3);
    int selection = (int) random(1,60);
    if(selection < 8){
      selection = 6;
    } else if(selection < 10) {
      selection = 5;
    } else {
      selection = (selection%4) + 1;
    }
    img = loadImage(str(selection) + ".png");
    if(selection == 6){ 
      virus = true;
    }
    if(selection == 5){
      clock = true;
    }
    img_w = 100; img_h = 100;
  }
  
  void gravity() {
    vy = vy + g;
  }
  
  void update() {
    gravity();
    y += vy;
  }
  
  void display() {
    update();
    image(img, x, y, img_w, img_h);
  }
}

class Game {
  int difficulty;
  int score;
  int missed;
  int numCovidItems;
  boolean roundOver;
  Covid[] covid;
  
  //using the constructor
  Game() {
    difficulty = 1;
    score = 0;
    missed = 0;
    roundOver = true;
  }
  
  //defining method
  void updateCovid(){
    //increasing the number of objects 
    difficulty++;
    numCovidItems = difficulty;
    covid = new Covid[numCovidItems];
    for (int i=0; i<numCovidItems; i++){
      covid[i] = new Covid();
    }
  }
  
  //method display
  void display(){
    //condition for game over
    if(missed >= 10){
      // GAME OVER SCREEN
      if (gameOver == false){
        gameover.play();
      }
      gameOver = true;
      
      textSize(30);
      fill(255);
      text("PRESS ANY KEY TO RESTART.", (width/2)-200, height/2);
      
    } else {
      if(frame+150 < frameCount){
        frameRate(60);
      }
      if(roundOver == true){
        updateCovid();
        roundOver = false;
      }
      
      numCovidItems = difficulty;
      
      //making the objects disappear
      int killedobjs = 0;
      for (int i=0; i<numCovidItems; i++){
        if(covid[i] == null){
          killedobjs++;
        }        
      }
      if(killedobjs == numCovidItems){
        roundOver = true;
      }
  
      for (int i=0; i<numCovidItems; i++){
        // checks if object is missed
        if(covid[i] != null){
          if(covid[i].y > height) {
            if(!covid[i].virus) { missed++; }
            covid[i] = null;
          }
        }
        // checks if object is cut else displays
        if(covid[i] != null){
          if((covid[i].x <= (mouseX+50) && (mouseX-50) <= (covid[i].x + covid[i].img_w)) && (covid[i].y <= (mouseY+40) && (mouseY-50) <= (covid[i].y + covid[i].img_h))){
            if(covid[i].virus) { 
              missed = 11; 
              bomb.play();
            } else if (covid[i].clock) {
              covid[i] = null;
              frameRate(20);
              frame = frameCount;
            } else {
              covid[i] = null;
              score++;
              objects.amp(0.1);
              objects.play();
            }
          } else {
            covid[i].display();
          }
        }
      }
    }
  }
}

 

Leave a Reply