Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//**------ Defeat Oculi Magni ------**//
//------------------------------------//
//--------   Jordan Baltich   --------//
//---- A and D to move, W to fire ----//

//global variables for enemy
float enemyX = 0;
float enemyY = 10;
float enemyW = 100;
float enemyH = 100;
float moveSpeed = 2;
float enemyShotX = 50;
float enemyShotY = 50;
float enemyShotSpeed = 8;
float enemyXSpeed = 2;
float enemyHealth = 20;
boolean isHit = false;
boolean isDead = false;
boolean hasShot = false;
boolean stage2 = false;

// gloabal variables for player
float playerX = 180;
float playerY = 365;
float playerW = 30;
float playerH = 30;
float pMoveX = 5;
boolean playerHit = false;
boolean isLeft = false;
boolean isRight = false;
float pshotY = 375;
float pshotX;
float pshotMoveX = 5;
boolean isShot = false;
float lives = 3;

// global variables for enemy health bar
float eBarWidth = 200;
float eBarHit = 10;

// gloabl variables for player health bar
float playerLifeW = 10;
float playerLifeH = 10;
float spacing = 15;

//minion global variables
float minionX = random(10, 350);
float minionY = -800;
float minionW = 40;
float minionH = 40;
float minionXSpeed = 1.25;
float minionYSpeed = 6;
boolean minionHit = false;
boolean onScreen = false;

// set screen size and frame rate
void setup() {
  size(400, 400);
  frameRate (60);
}

void draw() {
  background(65, 63, 70);
  displayBackground();

  checkDead();
}

// draw the background
void displayBackground() {
  noStroke();
  rectMode(CORNER);
  fill(96,91,108);
  rect(0,height-100,width,100);
  fill(48,46,55);
  rect(0,0,width,25);
  fill(55,53,60);
  rect(0,height-120,width,20);
  fill(86,81,98);
  rect(0,height-100,width,30);
}

// if player is alive run game, if player or enemy has died show proper screen
void checkDead() {
  if (isDead == false) {
    displayPlayer();
    playerShoot(8);
    playerMovement();
    playerCollision();
    playerHitDetect();
    playerHealthBar(350, 380);

    displayEnemy(enemyX, enemyY, enemyW, enemyH);
    moveEnemy();
    enemyShoot(10);
    enemyHitDetect();
    enemyHealthBarDisplay(5, 5, eBarWidth, 10);
    enemyHealthBarHit();

    minionMovement();
    minionHitDetect();
  } else if (isDead == true) {
    gameOver(70,100);
  }
}

//**---------- All code for Enemy / Boss Eye ----------**//

// draw the enemy, if hit flash red, if dead do not draw enemy
void displayEnemy(float x, float y, float w, float h ) {
    ellipseMode(CORNER);
    noStroke();
    fill(200, 100, 100);
    ellipse(x, y, w, h);
    fill(255);
    ellipse(x+25, y+25, w-50, h-50);
    fill(0);
    ellipse(x+35, y+45, w-70, h-70);
    fill(255);
    ellipse(x+55, y+45, w-90, h-90);
    if (isHit == true) {
      fill(255, 0, 0, 100);
      ellipse(x, y, w, h);
      ellipse(x+25, y+25, w-50, h-50);
      ellipse(x+35, y+35, w-70, h-70);
      ellipse(x+55, y+35, w-90, h-90);
      isHit =false;
    } else if (isHit == false) {
    }
  }

// move enemy on x-axis back and forth
void moveEnemy() {
  enemyX = enemyX + moveSpeed;

  if (enemyX > width - 100 || enemyX < 0) {
    moveSpeed *= -1;
  }
}

// display the enemy's projectile, or if dead, do not
void displayShot() {
    ellipseMode(CORNER);
    noStroke();
    fill(206, 44, 59);
    ellipse(enemyShotX, enemyShotY+50, 20, 20);
  } 

//enemy shoot a projectile after a set amount of time
void enemyShoot(float shotDelay) {
    enemyShotX += enemyXSpeed;
    if (enemyShotX >= width - 20 || enemyShotX <= 0) {
      enemyXSpeed *= -1;
    }

    if (enemyShotY <= height + shotDelay) {
      enemyShotY += enemyShotSpeed;
      enemyXSpeed = 0;
      displayShot();
    } else if (enemyShotY > shotDelay) {
      enemyShotX = enemyX + 50;
      enemyXSpeed += 2.5;
      enemyShotY = 50;
    }
  }

//detect if the enemy has been hit and track its health, if health 5 enter stage 2 movement
void enemyHitDetect() {  
  if ((pshotX >= enemyX && pshotX <= enemyX+enemyW)&&(pshotY >= enemyY && pshotY <=enemyY+enemyH)&& isShot == true) {
    enemyHealth += - 1;
    isHit = true;
    isShot = false;
    if (enemyHealth == 5) {
      stage2 = true;
      if (stage2 == true) {
        moveSpeed *= 2;
        enemyXSpeed *= 2;
        enemyShotSpeed *= 1.5;
      }
    }
  }
  if (enemyHealth <= 0) {
    isDead = true;
  }
}

//display the enemy health bar
void enemyHealthBarDisplay(float x, float y, float w, float h) {
    noStroke();
    fill(218, 56, 102);
    rectMode(CORNER);
    rect(x, y, w, h);
}

// shrink health bar if hit
void enemyHealthBarHit() {
  if (isHit == true) {
    eBarWidth += -eBarHit;
  }
}

//**---------- All code for Player Character ----------**//

// draw the player
void displayPlayer() {
  noStroke();
  ellipseMode(CORNER);
  fill(150, 150, 250);
  ellipse(playerX, playerY, playerW, playerH);
  fill(255);
  ellipse(playerX+5, playerY+5, playerW-25, playerH-20);
  ellipse(playerX+20, playerY+5, playerW-25, playerH-20);
  fill(0);
  ellipse(playerX+5, playerY+5, playerW-25, playerH-25);
  ellipse(playerX+20, playerY+5, playerW-25, playerH-25);
}

//shoot a projectile if a projectile is not already being shot
void playerShoot(float shotSpeed) {
  if (isShot == true) {
    pshotMoveX = 0;
    noStroke();
    fill(117, 141, 187);
    ellipse(pshotX + 10, pshotY, 5, 5);
    if (pshotY >= 0) {
      pshotY += - shotSpeed;
    } else if (pshotY <= 0) {
      isShot = false;
    }
  } else if (isShot == false) {
    pshotY = 350;
    pshotX = playerX;
    pshotMoveX = 5;
    fill(0, 0, 0, 0);
  }
}

//moves player left or right depending on keypresses, also allows player to shoot
void playerMovement() {
  if (keyPressed) {
    if (key == 'd' || key == 'D') {
      isRight = true;
      if (isRight == true) {
        isLeft = false;
        keyReleased();
        playerX += pMoveX;
        pshotX += pshotMoveX;
      }
    } else if (key == 'a' || key =='A') {
      isLeft = true;
      if (isLeft == true) {
        isRight = false;
        keyReleased();
        playerX += -pMoveX;
        pshotX += -pshotMoveX; 
      }
    } else if (key == 'w' || key == 'W') {
      isShot = true;
    }
  }
}

void keyReleased() {
  if (isLeft == true){
    isRight = true;
    isLeft = false;
  } else if (isRight == true){
    isLeft = true;
    isRight = false;
  }
}

//check to see if player is going off screen and correct it
void playerCollision() {
  if (playerX >= width - 20) {
    playerX = width - 20;
  } else if (playerX <= 0) {
    playerX = 0;
  }
}

//check to see if the player has been hit, lower lives if hit
void playerHitDetect() {
  if ((enemyShotX >= playerX && enemyShotX <= playerX + playerW) && (enemyShotY >= playerY && enemyShotY <= playerY + playerH) 
    || (enemyShotX+20 >= playerX && enemyShotX+20 <= playerX+playerW) && (enemyShotY+20 >= playerY && enemyShotY+20 <= playerY+playerH)) { 
    lives += -1;
    playerHit = true;
    if (playerHit == true) {
      enemyShotX = enemyX + 50;
      enemyShotY = 50;
      playerX = 200;
    }
    if (lives <= 0) {
      isDead = true;
    }
  } else if ((minionX >= playerX && minionX <= playerX + playerW) && (minionY >= playerY && minionY <= playerY + playerH) 
    || (minionX+minionW >= playerX && minionX+minionW <= playerX+playerW) && (minionY+minionH >= playerY && minionY+minionH <= playerY+playerH)) {
    lives += -1;
    playerX = 200;
  }
  if (lives <= 0) {
      isDead = true;
  }
}

//draw health bar whith while loop on screen according to player's lives
void playerHealthBar(float x, float y) {
  noStroke();
  fill(74, 110, 204);
  rectMode(CORNER);

  if (lives == 3) {
    while (x < 390) {
      rect(x,y,playerLifeW,playerLifeH);
      x += spacing;
    }
  } else if (lives == 2) {
    while (x < 370){
      rect(x,y,playerLifeW,playerLifeH);
      x += spacing;
    }
  } else if (lives == 1) {
    rect(x, y, playerLifeW, playerLifeH);
  } else {
  }
}

//**----------- All code for Minion / Small Eye -----------**//

//draw the minion on the screen
void displayMinion(float x, float y, float w, float h) {
  ellipseMode(CORNER);
  noStroke();
  fill(100, 200, 150);
  ellipse(x, y, w, h);
  fill(255);
  ellipse(x+7, y+7, w-15, h-15);
  fill(0);
  ellipse(x+12, y+14, w-25, h-25);
  fill(255);
  ellipse(x+20, y+14, w-35, h-35);
}

//set minion to slowy move towards the player once on screen
void minionMovement() {
    if (minionY <= height + minionH) {
      displayMinion(minionX, minionY, minionW, minionH);
      minionY += minionYSpeed;

      if (minionY >= 0) {
        minionXSpeed = 1.5;
        onScreen = true;
        if (onScreen == true) {
          if (playerX > minionX) {
            minionX += minionXSpeed;
          } else if (playerX < minionX) {
            minionX += -minionXSpeed;
          }
        }
      } else if (minionY < 0) {
        onScreen = false;
        if (onScreen == false) {
          minionXSpeed = 0;
        }
      }
    } else if (minionY >= height + minionH) {
      minionX = random(10, 350);
      minionY = -800;
    }
  }

//detect if minion was hit, if so reset
void minionHitDetect() {
  if ((pshotX >= minionX && pshotX <= minionX+minionW)&&(pshotY >= minionY && pshotY <=minionY+enemyH)&& isShot == true) {
    isShot = false;
    minionX = random(10, 350);
    minionY = -800;
  }
}

//**---------- Game Over Screens ----------**//

// if player has won or lost display proper screen
void gameOver(float x , float y) {
  if (lives > 0) {
    rectMode(CORNER);
    noStroke();
    fill(100,69,125);
    rect(x,y,20,100);
    rect(x+20,y + 80,20,20);
    rect(x+40,y+60,20,20);
    rect(x+60,y+80,20,20);
    rect(x+80,y,20,100);
    rect(x+120,y,20,100);
    rect(x+160,y,20,100);
    rect(x+180,y,20,40);
    rect(x+200,y+40,20,20);
    rect(x+220,y+60,20,40);
    rect(x+240,y,20,100);
    tryAgain(x+100,y+140,60,60);
    
  } else if (lives <= 0) {
    rectMode(CORNER);
    noStroke();
    fill(100,69,125);
    rect(x-20,y,20,100);
    rect(x,y+80,40,20);
    rect(x+60,y,20,100);
    rect(x+80,y,40,20);
    rect(x+80,y+80,40,20);
    rect(x+100,y,20,100);
    rect(x+140,y,60,20);
    rect(x+140,y+80,60,20);
    rect(x+140,y+20,20,20);
    rect(x+160,y+40,20,20);
    rect(x+180,y+60,20,20);
    rect(x+220,y,20,100);
    rect(x+220,y,60,20);
    rect(x+220,y+80,60,20);
    rect(x+220,y+40,40,20);
    tryAgain(x+100,y+140,60,60);
    
  }
}

// if button clicked reset variables to try again
void tryAgain(float x ,float y, float w, float h){
  ellipseMode(CORNER);
  noStroke();
  fill(50,200,50);
  ellipse(x,y,w,h);
  if ((mouseX >= x && mouseX <= x+w) && (mouseY >= y && mouseY <= y+h) && mousePressed){
    lives = 3;
    enemyHealth = 20;
    eBarWidth = 200;
    enemyX = 0;
    enemyShotX = 50;
    minionY = -800;
    playerX = 170;
    isShot = false;
    moveSpeed = 2;
    enemyShotSpeed = 8;
    enemyXSpeed = 2;
    isDead = false;
  }
}