Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*//////////////////////////////////////////////////////////////////////////////////////////////
 * Germ Dodger by Nima Gholipour
 * Student ID: 991391215
 * Intstructions:
 *   The player has to try and dodge the incoming waves of germs using the mouse to move only up and down
 *
 //////////////////////////////////////////////////////////////////////////////////////////////*/


// declare variables
int germNumber=50;
White whiteCell;

Germs[] germCell;

void setup() {

  //set windows size
  size(400, 400);

  noStroke();

  //initialize player white blood cell
  whiteCell = new White();

  //initialize the number of germs
  germCell = new Germs[germNumber];

  //declare germ values
  for (int i=0; i<germCell.length; i++) {
    germCell[i]=new Germs(random(400), random(400), random(20, 50), random(20, 50), color(random(255), random(255), random(255)), random(-1, 1), random(-1, 1));
  }
}



void draw() {

  //call functions
  drawBackground();
  whiteCell.drawPlayer();

  //make the germs move
  for (int i=0; i<germCell.length; i++) {
    germCell[i].germUpdate();
  }
  // display the germs
  for (int i=0; i<germCell.length; i++) {
    germCell[i].drawGerm();
  }
}

void drawBackground() {

  //level background
  background(255);
  rectMode(CORNER);
  fill(#e09faa);
  rect(0, 0, 400, 40);
  fill(#c77f8b);
  rect(0, 40, 400, 60);
  fill(#ba707c);
  rect(0, 100, 400, 40);
  fill(#a45d69);
  rect(0, 140, 400, 120);
  fill(#ba707c);
  rect(0, 260, 400, 40);
  fill(#c77f8b);
  rect(0, 300, 400, 60);
  fill(#e09faa);
  rect(0, 360, 400, 40);
}
class Germs {

  //set variables
  PVector position;
  PVector velocity;

  float germWidth;
  float germHeight;
  color germColor;

  // set the constructor variables
  Germs(float tempPositionX, float tempPositionY, float tempGermWidth, float tempGermHeight, color tempgermColor, float tempVelocityX, float tempVelocityY) {

    position=new PVector(tempPositionX, tempPositionY);
    velocity=new PVector(tempVelocityX, tempVelocityY);

    germWidth=tempGermWidth;
    germHeight=tempGermHeight;

    germColor=tempgermColor;
  }

  Germs() {
    init();
  }

  void init() {
    // germ start values
    position.x=width+random(186);
    position.y=random(0, 400);
    velocity.x=random(-5, -5);
    germColor=color(random(114, 190), random(50, 150), random(67, 167));
  }

  void germUpdate() {
    //move the germs
    position.add(velocity);

    //respawn germs after they move offscreen
    if (position.x<-100) {
      init();
    }
  }

  void drawGerm() {
    //how the germ looks
    fill(germColor);
    ellipse(position.x, position.y, germWidth, germHeight);
  }
}

class White {

  void drawPlayer () {

    //display the player
    ellipseMode(CENTER);

    //color of the player and controls 
    fill(#8fa9ff);
    ellipse(50, mouseY, 45, 45);


    fill(255);
    ellipse(50, mouseY
      , 35, 35);
  }
}