Midterm Progress

For the midterm project, I decided to expand on the game I created for the Week 3 assignment. The rules of this game, similar to those of the game I previously created, are pretty straightforward. Attached below is the link to my game for the Week 3 assignment.

Week 3 – Go, get that 4.0!

The game I am creating is primarily based on collision detection. In the previous version of the game (Week 3), the player played the game as Faiza the Falcon, and had to avoid certain obstructions (referred to as distractions in the game) on the way to obtain a 4.0 GPA.

My midterm project, however, will come with a lot more features. I aim to extend my previous idea in a way where I can create multiple levels (I am aiming for 10 levels), in increasing orders of difficulty. My Week 3 game will only be the final level of my midterm project.

The first (hopefully) 9 levels are based on an entirely different concept, where Faiza has to avoid different obstructions, again, but in order to save her mental health. These obstructions will be things like early morning/late night classes (due to timezone differences), assignments and anxiety. Unlike my previous game where all obstructions behaved in the same way, this version will have all of the obstructions  behaving in entirely different ways. For instance, anxiety will constantly be following Faiza, and assignments will be shot at Faiza’s coordinates as they continue to change.

Over the weekend, did a lot of research on how I could implement many of these features. Firstly, I worked on learning how to use inheritance in Java. The previous version of my game had a lot of blocks of code which were repeated, and I learnt how using inheritance could let me create a Creature() class with all the basic attributes and methods, which other classes could inherit from. This made my code a lot simpler and nicer to work with.

Secondly, I worked on getting suitable sprites and backgrounds for different parts of the game, including the introduction screen, the final screen and the main game screen. I also managed to do some research on the implementation of the “following” behavior of some of the obstructions (as I intend to make anxiety follow Faiza), and realized that this had to be done using trigonometric functions (and I’m still in the process of figuring that out).

Overall, I believe this week was more about getting things together in one place and finalizing what the final game should look like. There’s a lot of work to be done in the coming week, but I’m really looking forward to it.

Attached below are the screenshots of some of the things I have managed to come up with.

import java.lang.Math;
import processing.sound.*;
SoundFile file;

String audioName = "intro.mp3";
String path;

Game game;

// creating an array for distractions which are used later
Distractions[] distractions;

class Creature {
  float posX, posY, radius, velocityX, velocityY, imgwidth, imgheight;
  PImage sprite_image;
  int num_frames, frame;
  String directionX;

  Creature(float x, float y, float r, String image_name, float img_w, float img_h, int number_frames) {
    posX = x;
    posY = y;
    radius = r;
    velocityX = 0;
    velocityY = 0;
    directionX = "right";
    sprite_image = loadImage(image_name);
    imgwidth = img_w;
    imgheight = img_h;
    num_frames = number_frames;
    frame = 0;
  }

  void display() {
    update();
    if (directionX == "right") {
      image(sprite_image, float(int(posX - imgwidth/2)), float(int(posY - imgheight/2)), imgwidth, imgheight, int(frame * imgwidth), 0, int((frame + 1) * imgwidth), int(imgheight));
    } else if (directionX == "left") {
      image(sprite_image, float(int(posX - imgwidth/2)), float(int(posY - imgheight/2)), imgwidth, imgheight, int((frame + 1) * imgwidth), 0, int(frame * imgwidth), int(imgheight));
    }
  }

  void update() {
  }
}


class Faiza extends Creature {
  boolean move_up, move_down, move_right, move_left;
  boolean alive;
  int counter;

  Faiza(float x, float y, float r, String image_name, float img_w, float img_h, int number_frames) {
    super(x, y, r, image_name, img_w, img_h, number_frames);
    move_up = false;
    move_down = false;
    move_right = false;
    move_left = false;
    alive = true;
    counter = 0;
  }

  void update() {
    //The condition below is for when Faiza moves left
    if (move_left == true) {
      velocityX = -2;
      if (posX - radius + velocityX < 6) {
        velocityX = 0;
      }
      posX += velocityX;
    }

    //The condition below is for when Faiza moves right
    else if (move_right == true) {
      velocityX = 2;
      if (posX + radius + velocityX > 1018) {
        velocityX = 0;
      }
      posX += velocityX;
    }

    //If none of the left and right keys are being pressed, Faiza stops moving horizontally
    else {
      velocityX = 0;
    }        

    if (move_up == true) {
      velocityY = -2;
      if (posY - radius + velocityY <= 5) {
        velocityY = 0;
      }
      posY += velocityY;
    }

    //The condition below is for when Faiza moves downwards
    else if (move_down == true) {
      velocityY = 2;
      if (posY + radius + velocityY >= 762) {
        velocityY = 0;
      }      
      posY += velocityY;
    }

    //If none of the up and down keys are being pressed, Faiza stops moving vertically
    else {
      velocityY = 0;
    }

    if ((frameCount%5 == 0) && (velocityX != 0 || velocityY != 0)) {
      frame = (frame + 1) % (num_frames - 1);
    } else if (velocityX == 0 && velocityY == 0) {
      frame = 8;
    }

    if (distance(game.gpa) <= (radius + game.gpa.radius) && game.level == 11) {
      game.level += 1;
    }

    if (!(posX >= 0 && posX <= 100 && posY >= 530 && posY <= 640)) { 
      for (int i = 0; i < 6; i++) {
        if (distance(distractions[i]) <= radius + distractions[i].radius) {
          counter += 1;
          alive = false;
        }
      }
    }
  }

  // this distance method will be used to check for collisions with distractions
  double distance(Distractions target) {
    float a = (posX - target.posX);
    float b = (posY - target.posY);
    double c = Math.pow(a, 2);
    double d = Math.pow(b, 2);
    return Math.pow(c + d, 0.5);
  }

  // this distance method will be used to check for collisions with the gpa (marking the end of the game)
  double distance(GPA target) {
    float a = (posX - target.posX);
    float b = (posY - target.posY);
    double c = Math.pow(a, 2);
    double d = Math.pow(b, 2);
    return Math.pow(c + d, 0.5);
  }
}

class Distractions extends Creature {

  Distractions(float x, float y, float r, String image_name, float img_w, float img_h, int number_frames) {
    super(x, y, r, image_name, img_w, img_h, number_frames);
    velocityX = random(2, 5);
    velocityY = -1 * random(2, 5);
  }

  void update() {
    if (frameCount % 12 == 0) {
      frame = (frame + 1) % num_frames;
    }

    if (posX + radius >= 1024) {
      velocityX *= -1;
    }
    if (posX - radius <= 0) {
      velocityX *= - 1;
    }
    if (posY - radius <= 10) {
      velocityY *= -1;
    }
    if (posY + radius >= 780) {
      velocityY *= -1;
    }

    posX += velocityX;
    posY += velocityY;
  }
}

class GPA extends Creature {

  GPA(float x, float y, float r, String image_name, float img_w, float img_h, int number_frames) {
    super(x, y, r, image_name, img_w, img_h, number_frames);
  }
}

class Game {
  float game_width, game_height;
  Faiza faiza;
  GPA gpa;
  int level;
  PImage intro_bg, final_bg, over_bg;

  Game(float game_wth, float game_hght) {
    level = 0;
    game_width = game_wth;
    game_height = game_hght;
    faiza = new Faiza(34, 585, 27, "faiza.png", 66, 66, 9);
    gpa = new GPA(990, 35, 25, "gpa.png", 70, 56, 1);
    intro_bg = loadImage("start_background.png");
    final_bg = loadImage("background.png");
    over_bg = loadImage("gameover_background.png");
    
    distractions = new Distractions[6];
    distractions[0] =  new Distractions(100, 300, 58, "jake.png", 120, 120, 6);
    distractions[1] =  new Distractions(444, 333, 48, "insta.png", 100, 100, 1);
    distractions[2] =  new Distractions(900, 120, 48, "facebook.png", 100, 100, 1);
    distractions[3] =  new Distractions(887, 635, 48, "netflix.png", 100, 100, 1);
    distractions[4] =  new Distractions(134, 587, 48, "youtube.png", 100, 100, 1);
    distractions[5] =  new Distractions(55, 100, 48, "ps.png", 120, 120, 1);
  }

  void update() {
    if (faiza.alive == false) {
      faiza.posX = 34;
      faiza.posY = 585;
      faiza.alive = true;
    }
  }

  void display() {
    update();

    if (level == 0) {
      image(intro_bg, 0, 0);
    }

    if (level == 11) {
      image(final_bg, 0, 0);
    }

    if (level == 11) {
      textMode(CENTER);
      textSize(40);
      fill(255, 213, 43);
      text("GET THAT 4.0!", 310, 65);
    }

    if (level == 12) {
      image(over_bg, 0, 0);
      textSize(150);
      fill(255, 213, 43);
      text("GAME", 270, 220); 
      text("OVER", 290, 350);
      textSize(50);
      text(faiza.counter + " distraction(s) later,", 240, 550);
      text("you achieved that 4.0 GPA!", 200, 600);
    }

    if (level > 0 && level <= 11) {
      faiza.display();
    }

    if (level == 11) {
      gpa.display();
    }

    if (level == 11) {
      for (int i = 0; i < 6; i++) {
        distractions[i].display();
      }
    }
  }
}


void setup() {
  size(1024, 768);
  game = new Game(1024, 768);
  path = sketchPath(audioName);
  file = new SoundFile(this, path);
  file.loop();
}

void draw() {
  background(255, 255, 255);
  game.display();
}

// allowing key presses to dictate Faiza's movement
void keyPressed() {
  if (keyCode == 32 && game.level == 0) {
    game.level = 11;
  }
  if (key == CODED) {
    if (keyCode == RIGHT) {
      game.faiza.move_right = true;
    }
    if (keyCode == LEFT) {
      game.faiza.move_left = true;
    }
    if (keyCode == UP) {
      game.faiza.move_up = true;
    }
    if (keyCode == DOWN) {
      game.faiza.move_down = true;
    }
  }
}

void keyReleased() {
  if (key == CODED) {
    if (keyCode == RIGHT) {
      game.faiza.move_right = false;
    }
    if (keyCode == LEFT) {
      game.faiza.move_left = false;
    }
    if (keyCode == UP) {
      game.faiza.move_up = false;
    }
    if (keyCode == DOWN) {
      game.faiza.move_down = false;
    }
  }
}

 

Leave a Reply