Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Assignment 2 Project
//WARNING: RISK OF SEIZURE PLEASE PLAY WITH CAUTION
//Interactive Toy by Wulusi Zhang 2017
//Introduction to Media Computation
//Press "ENTER" to fire, WASD controls, beat Ridley before the reactor blows out and survive the bombs he drop

//Establish Global Parameters

float shapeVaria;
float shapeHeight;
float shapeWidth;
float gravity = 1.2;

float ridleyHealth;
float ridleyX = 300;
float ridleyY = 150;
float enemyFireX = 80;
float enemyFireY = 50;
float enemyFireX2 = 150;
float enemyFireY2 = 100;
float enemyFireX3 = 300;
float enemyFireY3 = 100;

float rocketX = 0;
float rocketY = 0;
float rocketSpeed = 2;
float rocketSize = 10;
float damage;

float playerHealth = 200;
float playerDamage = 30;
float playerX =100;
float playerY =200;

float playerSpeedX = 4.0;
float playerSpeedY = 5;

float randomMovementX;
float randomMovementY;

float lights;
float y;
float z;
float time = millis();

boolean dLeft = false;
boolean dRight= false;
boolean dUp= false;
boolean dShoot= false;
boolean dHit= false;
boolean dExplosion = false;
boolean wingUp= false;

//Setp up Canvas
void setup() {
  size (400, 400);
  background (0);
  lights =50;
  y =60;
  z =80;
  ridleyHealth = 300;
  damage = 10;
}

//Draw boss on map

void drawRidley() {

  fill(163, 182, 126);
  frameRate(60);
  randomMovementX = cos(frameCount/19.0)*50;
  randomMovementY = sin(frameCount/19.0)*50;
  shapeVaria = 3;
  shapeHeight = sin(frameCount/7.0)*5+60;
  shapeWidth = cos(frameCount/5.0)*5+70;
  fill(#380142);
  stroke(0);
  quad(262+ randomMovementX, 122+ randomMovementY, 228+ randomMovementX, 152+ randomMovementY, 284+ randomMovementX, 143+ randomMovementY, 289+ randomMovementX, 125+ randomMovementY);
  quad(267+ randomMovementX, 120+ randomMovementY, 280+ randomMovementX, 130+ randomMovementY, 306+ randomMovementX, 104+ randomMovementY, 303+ randomMovementX, 99+ randomMovementY);
  quad(268+ randomMovementX, 144+ randomMovementY, 238+ randomMovementX, 162+ randomMovementY, 282+ randomMovementX, 156+ randomMovementY, 287+ randomMovementX, 135+ randomMovementY);
  quad(286+ randomMovementX, 154+ randomMovementY, 306+ randomMovementX, 183+ randomMovementY, 314+ randomMovementX, 174+ randomMovementY, 291+ randomMovementX, 136+ randomMovementY);
  quad(309+ randomMovementX, 125+ randomMovementY, 291+ randomMovementX, 135+ randomMovementY, 306+ randomMovementX, 156+ randomMovementY, 320+ randomMovementX, 125+ randomMovementY);
  quad(302+ randomMovementX, 172+ randomMovementY, 292+ randomMovementX, 196+ randomMovementY, 313+ randomMovementX, 189+ randomMovementY, 313+ randomMovementX, 164+ randomMovementY);
  quad(292+ randomMovementX, 195+ randomMovementY, 298+ randomMovementX, 221+ randomMovementY, 300+ randomMovementX, 218+ randomMovementY, 307+ randomMovementX, 190+ randomMovementY);
  quad(297+ randomMovementX, 218+ randomMovementY, 302+ randomMovementX, 214+ randomMovementY, 318+ randomMovementX, 235+ randomMovementY, 315+ randomMovementX, 242+ randomMovementY);
  quad(318+ randomMovementX, 235+ randomMovementY, 315+ randomMovementX, 242+ randomMovementY, 333+ randomMovementX, 242+ randomMovementY, 328+ randomMovementX, 235+ randomMovementY);
  quad(331+ randomMovementX, 236+ randomMovementY, 329+ randomMovementX, 241+ randomMovementY, 336+ randomMovementX, 250+ randomMovementY, 342+ randomMovementX, 249+ randomMovementY);
  quad(336+ randomMovementX, 250+ randomMovementY, 342+ randomMovementX, 249+ randomMovementY, 339+ randomMovementX, 263+ randomMovementY, 336+ randomMovementX, 259+ randomMovementY);
  quad(339+ randomMovementX, 263+ randomMovementY, 336+ randomMovementX, 259+ randomMovementY, 330+ randomMovementX, 266+ randomMovementY, 331+ randomMovementX, 271+ randomMovementY);
  quad(330+ randomMovementX, 266+ randomMovementY, 331+ randomMovementX, 271+ randomMovementY, 319+ randomMovementX, 273+ randomMovementY, 319+ randomMovementX, 268+ randomMovementY);
  quad(319+ randomMovementX, 273+ randomMovementY, 319+ randomMovementX, 268+ randomMovementY, 309+ randomMovementX, 263+ randomMovementY, 309+ randomMovementX, 267+ randomMovementY);
  triangle(309+ randomMovementX, 263+ randomMovementY, 309+ randomMovementX, 267+ randomMovementY, 301+ randomMovementX, 260+ randomMovementY);
  triangle(302+ randomMovementX, 251+ randomMovementY, 300+ randomMovementX, 258+ randomMovementY, 302+ randomMovementX, 261+ randomMovementY);
  ellipse(ridleyX+ randomMovementX, ridleyY+ randomMovementY, 25, 25);
  fill(244, random(60, 200), 66);
  triangle(255+randomMovementX, 140+randomMovementY, 272+randomMovementX, 136+randomMovementY, 268+randomMovementX, 132+randomMovementY);
  ellipse (ridleyX + randomMovementX, ridleyY + randomMovementY, 20, 20); //Ridley Heart 

  //wingbeats
  fill(#380142);
  frameRate(60);
  if (wingUp == false) {
    quad(313+randomMovementX, 127+randomMovementY, 319+randomMovementX, 164+randomMovementY, 386+randomMovementX, 118+randomMovementY, 379+randomMovementX, 62+randomMovementY);

    wingUp = true;
  } else {
    quad(313+randomMovementX, 201+randomMovementY, 319+randomMovementX, 164+randomMovementY, 386+randomMovementX, 210+randomMovementY, 379+randomMovementX, 266+randomMovementY);
    wingUp = false;
  }
}

//draw enemy projectiles

void drawEnemyfire() {
  

  frameRate(60);
  stroke(0);
  fill(#ff2e00);
  ellipse(enemyFireX, enemyFireY-10, 10, 30+sin(frameCount/2.0)*rocketSize*2);
  fill(#e8b017);
  ellipse(enemyFireX, enemyFireY-10, 10, 15+sin(frameCount/2.0)*rocketSize*2);
  fill(#401660);
  ellipse(enemyFireX, enemyFireY, 10, 30);
  fill(255);
  rect(enemyFireX, enemyFireY-5, 10, 18);
  enemyFireY = enemyFireY+6;

  if (enemyFireY > 450) { 
    enemyFireY = random(-200, -100);
    enemyFireX = random(100, 350);
  }
  if (enemyFireX >= playerX && enemyFireX <= playerX+20 && enemyFireY >= playerY && enemyFireY <= playerY+20) {
    frameRate(30);
    fill(#ff9d00);
    stroke(0);
    ellipse(playerX, playerY, cos(frameCount/5.0)*30, cos(frameCount/5.0)*40);
    playerHealth = playerHealth - playerDamage;
    //println(playerHealth);
  }
}

//draw enemy projectiles

void drawEnemyfire2() {


  frameRate(60);
  stroke(0);
  fill(#ff2e00);
  ellipse(enemyFireX2, enemyFireY2-10, 10, 30+sin(frameCount/2.0)*rocketSize*2);
  fill(#e8b017);
  ellipse(enemyFireX2, enemyFireY2-10, 10, 15+sin(frameCount/2.0)*rocketSize*2);
  fill(#401660);
  ellipse(enemyFireX2, enemyFireY2, 10, 30);
  fill(255);
  rect(enemyFireX2, enemyFireY2-5, 10, 18);
  enemyFireY2 = enemyFireY2+6;

  if (enemyFireY2 > 450) { 
    enemyFireY2 = random(-300, -200);
    enemyFireX2 = random(100, 350);
  }

  if (enemyFireX2 >= playerX && enemyFireX2 <= playerX+20 && enemyFireY2 >= playerY && enemyFireY2 <= playerY+20) {
    frameRate(30);
    fill(#ff9d00);
    stroke(0);
    ellipse(playerX, playerY, cos(frameCount/5.0)*30, cos(frameCount/5.0)*40);
    playerHealth = playerHealth - playerDamage;
    //println(playerHealth);
  }
}

 //draw enemy projectiles

void drawEnemyfire3() {
 

  frameRate(60);
  stroke(0);
  fill(#ff2e00);
  ellipse(enemyFireX3, enemyFireY3-10, 10, 30+sin(frameCount/2.0)*rocketSize*2);
  fill(#e8b017);
  ellipse(enemyFireX3, enemyFireY3-10, 10, 15+sin(frameCount/2.0)*rocketSize*2);
  fill(#401660);
  ellipse(enemyFireX3, enemyFireY3, 10, 30);
  fill(255);
  rect(enemyFireX3, enemyFireY3-5, 10, 18);


  enemyFireY3 = enemyFireY3+6;

  if (enemyFireY3 > 450) { 
    enemyFireY3 = random(-300, -200);
    enemyFireX3 = random(100, 350);
  }

  if (enemyFireX3 >= playerX && enemyFireX3 <= playerX+20 && enemyFireY3 >= playerY && enemyFireY3 <= playerY+20) {
    frameRate(30);
    fill(#ff9d00);
    stroke(0);
    ellipse(playerX, playerY, cos(frameCount/5.0)*30, cos(frameCount/5.0)*40);
    playerHealth = playerHealth - playerDamage;
    //println(playerHealth);
  }
}

//Draw player spaceship on Map

void drawPlayer() {
  
  stroke(0);
  fill(#2baf36);
  ellipse(playerX+8, playerY-12, 10, 8);
  fill(255);
  frameRate(60);
  ellipse(playerX, playerY, 4*sin(frameCount/6.0)+45, 4*sin(frameCount/6.0)+25);
  fill(#f90000);
  ellipse(playerX-25, playerY, 4*sin(frameCount/1.0)+35, 15);
  fill(#f9cf00);
  ellipse(playerX-20, playerY, 4*sin(frameCount/1.0)+25, 15);

  fill(#ff6a00);
  ellipse(playerX, playerY, 12, 10);
  quad(playerX-50, playerY-20, playerX-20, playerY, playerX, playerY, playerX-30, playerY-20);
  quad(playerX-20, playerY, playerX-50, playerY+20, playerX-30, playerY+20, playerX, playerY);
  quad(playerX+10, playerY+10, playerX+10, playerY-10, playerX+40, playerY, playerX+40, playerY+1);

  fill(#ff6a00);
  ellipse(playerX-12, playerY, 25, 10);
  //Gravity
  playerY = (playerY+gravity); 
  //Player Vertical Jump Speed
  if (dRight == true) {
    playerX = playerX + playerSpeedX;
  }
  if (dLeft==true) {
    playerX = playerX - playerSpeedX;
  }
  if (dUp==true) {
    playerY+=-playerSpeedY;
  }
  playerX = constrain(playerX, 65, 335);
  playerY = constrain(playerY, 14, 365);
}

//Draw Rocket from player
//Press enter to fire rocket at a straight line to the right, rocket will travel offscreen before another rocket can be fired

void drawRocket() {
  
  if (dShoot==true) 
  {
    if (key==ENTER)
    { 
      if (rocketX <= width)
      { 
        stroke(0);
        fill(#ffb71c);
        ellipse((rocketX +=(rocketSpeed-1))-5, rocketY, cos(frameCount/2.0)*rocketSize+20, rocketSize-2);
        fill(#db5c02);
        ellipse((rocketX +=(rocketSpeed-1))-3, rocketY, cos(frameCount/1.0)*rocketSize+17, rocketSize-2);
        rect(rocketX +=rocketSpeed, rocketY, rocketSize+10, rocketSize, 2);
        fill(255);
        ellipse(rocketX +=(rocketSpeed-1), rocketY, rocketSize+15, rocketSize);
        fill(#ff0000);
        ellipse(rocketX +=(rocketSpeed-1), rocketY, rocketSize+5, rocketSize);
      }
    }
  } else { 
    rocketX = playerX + 8;
    rocketY = playerY;
  } 

  //replaces the rocket at the location of the player if rocket went offscreen
  if (rocketX >= 400)
  { 
    rocketX = playerX;
    rocketY = playerY;
    dShoot = false; //resets Shoot state to false until dShoot is true again
  }

  // Rocket hit detection (not sure if hitbox is correctly coded)
  if (rocketX >= (ridleyX) && rocketX <= (ridleyX+ randomMovementX) && rocketY <= (ridleyY+ randomMovementY+10) && rocketY >= (ridleyY+randomMovementY-10) && ridleyHealth>0 ) //rocket must be within hitbox to damage ridley
  {  
    frameRate(80);
    fill(#ff9d00);
    stroke(0);
    ellipse(ridleyX+ randomMovementX, ridleyY+ randomMovementY, cos(frameCount/5.0)*30, cos(frameCount/5.0)*40);
    //println(ridleyHealth);
    ridleyHealth = ridleyHealth - damage;
    dShoot = true;
    if (damage > 25) {
      damage = 25;
    }
  }
}

//Draw HP Bar for Ridley
void drawhpCounter() {

  stroke(0);
  fill(#ff4830);
  rect(ridleyX+randomMovementX, ridleyY+randomMovementY-60, (ridleyHealth/5), 10);
}

//Draw HP Bar for Player

void drawPlayerhpCounter() {
  stroke(0);
  fill(#2db201);
  rect(playerX, playerY-30, (playerHealth/5), 10);
}

//Key Pressed controls for player

void keyPressed() {
  if (key == 'w') {
    dUp=true;
    //println("w");
  } else if (key == 'a') {
    dLeft=true;
    //println("a");
  } else if (key == 'd') {
    dRight=true;
    //println("d");
  } else if (key == ENTER) {
    dShoot=true;
    //println("shoot");
  }
}

//Confirm keyPressed release to make sure controls do not lock

void keyReleased() {
  if (key == 'w') {
    dUp=false;
  } else if (key == 'a') {
    dLeft=false;
  } else if (key == 'd') {
    dRight=false;
  }
}

//Draws Background elements and planet destroying giant beam

void drawElevator() {
  
  for (float i=0; i<400; i+=4)
  { 
    float lights;
    lights = random(0, 50);
    stroke(150);
    fill(#f7ff0f);
    rect(lights, (5*i)+lights, 1, 16, 3);
    rect(400-lights, (5*i)+lights, 1, 16, 3);
    lights = lights+1;
  }  
  float section1 =0;

  noStroke();
  fill(#dadbce);
  rect(section1+lights, section1+lights+150, 5, 400);
  rect(width-(section1+lights), section1+lights+150, 5, 400);

  //Background Elevator assets
  fill(#ffd800);
  rect(195, 200, 150+sin(frameCount/10.0)*130, 500, 6);
  fill(#ff5900);
  rect(195, 200, 60+sin(frameCount/10.0)*40, 500, 6);

  //Back Panel for Other lights
  fill(#e2e2e2, 80);
  ellipse(200, z+520, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z+500, 290, 200);
  fill(#878787, 50);
  ellipse(200, z+480, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z+460, 290, 200);
  fill(#e2e2e2, 80);
  ellipse(200, z+440, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z+420, 290, 200);
  fill(#878787, 50);
  ellipse(200, z+400, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z+380, 290, 200);
  fill(#e2e2e2, 80);
  ellipse(200, z+360, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z+340, 290, 200);
  fill(#878787, 50);
  ellipse(200, z+320, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z+300, 290, 200);
  fill(#e2e2e2, 80);
  ellipse(200, z+280, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z+260, 290, 200);
  fill(#878787, 50);
  ellipse(200, z+240, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z+220, 290, 200);
  fill(#e2e2e2, 80);
  ellipse(200, z+200, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z+180, 290, 200);
  fill(#878787, 50);
  ellipse(200, z+160, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z+140, 290, 200);
  fill(#e2e2e2, 80);
  ellipse(200, z+120, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z+100, 290, 200);
  fill(#878787, 50);
  ellipse(200, z+80, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z+60, 290, 200);
  fill(#e2e2e2, 80);
  ellipse(200, z+40, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z+20, 290, 200);
  fill(#878787, 50);
  ellipse(200, z, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z-20, 290, 200);
  fill(#e2e2e2, 80);
  ellipse(200, z-40, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z-60, 290, 200);
  fill(#878787, 50);
  ellipse(200, z-80, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z-100, 290, 200);
  fill(#e2e2e2, 80);
  ellipse(200, z-120, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z-140, 290, 200);
  fill(#878787, 50);
  ellipse(200, z-160, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z-180, 290, 200);
  fill(#e2e2e2, 80);
  ellipse(200, z-200, 290, 200);
  fill(#c4c4c4, 80);
  ellipse(200, z-220, 290, 200);
  fill(#878787, 50);
  ellipse(200, z-240, 290, 200);
  fill(random(200, 220), random(80, 206), 21, 50);
  ellipse(200, z-260, 290, 200);

  z-=6;
  if (z<-100)
  {
    z = 200;
  }
}

//Game Over Screen once conditions are met, condition in void draw section below

void drawGameOver() {

  fill(random(200, 250), random(120), 30);
  noStroke();
  background(0);
  rect(200, 200, 160, 160);
  stroke(0);
  strokeWeight(4);
  line(170, 170, 150, 190);
  line(230, 170, 250, 190);
  line(150, 217, 150, 237);
  line(250, 217, 250, 237);
  line(150, 217, 250, 217);
  line(150, 237, 250, 237);
}

//Game Won Screen once conditions are met, condition in void draw section below

void drawGameWon() {
  
  fill(random(0, 50), random(200, 250), 30);
  stroke(0);
  background(0);
  rect(200, 200, 160, 160);
  fill(0);
  strokeWeight(4);
  line(170, 170, 170, 190);
  line(230, 170, 230, 190);
  line(150, 217, 150, 237);
  line(250, 217, 250, 237);
  line(150, 237, 250, 237);
}

//Time Counter for game

void drawTimeCounter() {
  stroke(0);
  fill(0, 255, 255);
  rect(200, 20, 300-(time/200), 5);
  
  //TIME
  strokeWeight(2.5);
  line(150,4,158,4);
  line(154,4,154,12);
  line(179,4,179,12);
  line(200,4,210,4);
  line(200,4,200,12);
  line(205,4,205,12);
  line(210,4,210,12);
  line(225,4,233,4);
  line(225,4,225,12);
  line(225,8,232,8);
  line(225,13,233,13);
  strokeWeight(1.5);
}

//draw all elements in project

void draw() {
  background (0);
  fill(255);
  rectMode(CENTER);

  drawElevator();
  if (playerHealth >0) {
    drawPlayer();
    drawPlayerhpCounter();
  }
  if (ridleyHealth > 0) {
    drawRidley();
    drawhpCounter();
  }
  drawRocket();
  drawEnemyfire();
  drawEnemyfire2();
  drawEnemyfire3();

  time = millis(); //set timer 
  if (playerHealth<0 || time > 60000) {
    drawGameOver();
  }
  if (time<60000 && playerHealth>0 && time > 0) { 
    drawTimeCounter();
  }
  if (ridleyHealth == 0) {
    drawGameWon();
  }
}