Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Variables for the Player's X and Y position and the Player speed //<>//
float PlayerX;
float PlayerY;
float PlayerSpeed;

//Variables for the Enemy's X and Y positions and Enemy's speed
float EnemyX;
float EnemyY;
float EnemySpeed;

float EnemyX2;
float EnemyY2;
float EnemySpeed2;

float EnemyX3;
float EnemyY3;
float EnemySpeed3;

//Variable for the timer
int Timer;

//Variables for the Player shot's X and Y position and it's speed
float PlayerbulletX;
float PlayerbulletY;
float PlayerbulletSpeed;

//Variable for the score
int score;

//Boolean variables the Enemy's visibility
boolean enemyvis;
boolean enemy2vis;
boolean enemy3vis;

void setup(){
  size(400, 400);  // Set the size of the window
  background(255); // Draw a white background 
  smooth(); 
  
  //Player X and Y set positions 
  PlayerX = 190; 
  PlayerY = 350;
  //Speed of the Player
  PlayerSpeed = 4;
  
  //Player shot's set positions
  PlayerbulletX = PlayerX;
  PlayerbulletY = PlayerY;
  //Speed of the Player's shot
  PlayerbulletSpeed = 20;
 
  //X position of all enemies are spawned at random spots at the top 
  EnemyX = (int)(random(0,400));
  EnemyX2= (int)(random(0,400));
  EnemyX3= (int)(random(0,400));
  
  //Y position of all enemies are set to 40
  EnemyY = 40;
  EnemyY2 = EnemyY;
  EnemyY3 = EnemyY;
  
  //The speed of all enemies are also randomized between 1 - 3
  EnemySpeed = (int)(random(1,3));
  EnemySpeed2= (int)(random(1,3));
  EnemySpeed3= (int)(random(1,3));
  
  //Timer is set to 5000 
  Timer = 5000;
  
  //The Player's Score is set to 0
  score = 0;
  
  //Enemies are automatically visible
  enemyvis = true;
  enemy2vis = true;
  enemy3vis = true;
  
}

void draw(){
  drawbg(); //Draw the background
  drawPlayer(); //Draws Player
  playermove(); //Moves Player
  BulletCollision(); //Collision function of the player's bullets and enemies
  fire(); //Player's fire function
  updateTimer(); //Timer function
}

void updateTimer(){
  //The interactive toy functions under a timer. Once the timer stops, the interactive toy ends.
  
  //When the timer hasn't hit zero:
  if(Timer > 0){
    //The timer begins counting down and the score increases
    text("Timer:" + Timer, 20, 40);
    text("Score:" + score, 20, 80);
    textSize(18);
    //Timer goes down by an increment
    Timer-=1;
    //When the timer is active, all enemies are visible
    if(enemyvis == true && enemy2vis == true && enemy3vis == true){
      drawEnemy(); 
      drawEnemy2();
      drawEnemy3();
    }  
      
    }      
  
  //When the timer hits zero:
  else if(Timer <= 0){
    //The "GAME OVER" text displays
    text("GAME OVER", 160, 200);
    textSize(18);
    
    //The timer displays as zero and the total score is displayed
    text("Timer:" + Timer, 20, 40);
    text("Score:" + score, 160, 230);
    textSize(18);
    Timer=0; 
    
    //Everything but the player disappears when the timer hits zero and the interactive game ends   
  }
  
}

//The following functions below draws all the enemies. They all move down the screen by an increment,
//but when they reach the bottom of the screen they respawn at different co-ordinates and 
//at different speeds.
void drawEnemy(){
  noStroke();
  fill(255, 36, 99);
  
  ellipse(EnemyX, EnemyY, 50,50);
  fill(255, 230, 237);
  ellipse(EnemyX, EnemyY, 30, 30);  
  fill(0, 0, 0);
  ellipse(EnemyX, EnemyY, 10, 10);  
  fill(255, 255, 255);
  ellipse(EnemyX-10, EnemyY, 10, 10);
  
  EnemyY += EnemySpeed; 
  
  if(EnemyY > height){
    EnemyX = (int)(random(0,400));
    EnemyY = 40;
    EnemySpeed = (int)(random(1,3));
  }
   
}

void drawEnemy2(){
  noStroke();
  fill(91, 164, 241);
  
  ellipse(EnemyX2, EnemyY2, 50,50);
  fill(139, 195, 255);
  ellipse(EnemyX2, EnemyY2, 30, 30);  
  fill(0, 0, 0);
  ellipse(EnemyX2, EnemyY2, 10, 10);  
  fill(255, 255, 255);
  ellipse(EnemyX2-10, EnemyY2, 10, 10);
  
  EnemyY2 += EnemySpeed2;
  
  if(EnemyY2 > height){
    EnemyX2 = (int)(random(0,400));
    EnemyY2 = 40;
    EnemySpeed2 = (int)(random(1,3));
  }
     
}

void drawEnemy3(){
  noStroke();
  fill(92, 201, 43);
  
  ellipse(EnemyX3, EnemyY3, 50,50);
  fill(173, 255, 136);
  ellipse(EnemyX3, EnemyY3, 30, 30);  
  fill(0, 0, 0);
  ellipse(EnemyX3, EnemyY3, 10, 10);  
  fill(255, 255, 255);
  ellipse(EnemyX3-10, EnemyY3, 10, 10);
  
  EnemyY3+= EnemySpeed3;
  
  if(EnemyY3 > height){
    EnemyX3 = (int)(random(0,400));
    EnemyY3 = 40;
    EnemySpeed3 = (int)(random(1,3));
  }
     
}

//This function draws the Player
void drawPlayer(){
  //Ears
  noStroke();
  fill(255, 255, 255);
  rect(PlayerX-10,PlayerY-30,5,30);
  rect(PlayerX+5,PlayerY-30,5,30);
    
  //Skirt
  noStroke();
  fill(244, 209, 195);
  triangle(PlayerX, PlayerY+10, PlayerX-10, PlayerY+30, PlayerX+10, PlayerY+30);
  
  //Legs
  noStroke();
  fill(255, 255, 255);
  triangle(PlayerX, PlayerY+30, PlayerX-10, PlayerY+30, PlayerX, PlayerY+50);
  triangle(PlayerX+10, PlayerY+30, PlayerX-10, PlayerY+30, PlayerX, PlayerY+50);
  
  //Shoes
  noStroke();
  fill(94, 0, 0);
  triangle(PlayerX, PlayerY+40, PlayerX-5, PlayerY+40, PlayerX, PlayerY+50);
  triangle(PlayerX+10, PlayerY+40, PlayerX-5, PlayerY+40, PlayerX, PlayerY+50);
  
  //Hands
  noStroke();
  fill(250, 245, 226);
  triangle(PlayerX, PlayerY+10, PlayerX-10, PlayerY+10, PlayerX-10, PlayerY+30);
  triangle(PlayerX, PlayerY+10, PlayerX+10, PlayerY+10, PlayerX+10, PlayerY+30);
  
  //Head
  noStroke();
  fill(187, 153, 205);
  ellipse(PlayerX, PlayerY, 20, 20);
  rect(PlayerX-10, PlayerY, 20, 20);
  
  //Tail
  noStroke();
  fill(255, 255, 255);
  ellipse(PlayerX, PlayerY+20, 10, 10);
    
}

//This function moves the player and makes sure the player shots move along with the player
void playermove(){
  //If a specific key is pressed, they move in the direction of that key
  if(keyPressed){
    if(keyCode == LEFT){
      PlayerX = PlayerX - PlayerSpeed;
      PlayerbulletX = PlayerX-5;
      PlayerbulletY = PlayerY-5;
  }
  else if(keyCode == RIGHT){
      PlayerX = PlayerX + PlayerSpeed;
      PlayerbulletX = PlayerX-5;
      PlayerbulletY = PlayerY-5;
  }
  else if(keyCode == UP){
      PlayerY = PlayerY - PlayerSpeed;
      PlayerbulletX = PlayerX-5;
      PlayerbulletY = PlayerY-5;
  }
  else if(keyCode == DOWN){
      PlayerY = PlayerY + PlayerSpeed;
      PlayerbulletX = PlayerX-5;
      PlayerbulletY = PlayerY-5;
  }
  //This makes sure the Player stays within the screen boundaries
    PlayerY= constrain(PlayerY, 15, 350);
    PlayerX= constrain(PlayerX, 20, 380);
  }  

}

//This function allows for the Player to fire the Player shots when 'z' is pressed
void fire(){    
  if(keyPressed){
    if(key == 'z'){
      //This creates the shape of the bullet
      stroke(255, 255, 255);
      fill(255, 255, 255);
      rect(PlayerX-5, PlayerbulletY-50, 10, 20);
      PlayerbulletY = PlayerbulletY - PlayerbulletSpeed;
    }
  }
  //This makes sure the bullet stays within the screen boundaries
  PlayerbulletY= constrain(PlayerbulletY, 0, height);
}

//This function sets the collision between the Player's bullets and the enemies themselves.
void BulletCollision(){
  //The hitbox of the enemies to put it simply is rect(EnemyX-15, EnemyY-15, 30, 30). It is a rectangular hitbox to make the collision
  //detection easier to find.  
  //If the Player's bullets hit that rectangular hitbox, the Enemies disappear and respawns at the top of the screen with different
  //co-ordinates and different speeds
  if(PlayerbulletX >= EnemyX-15 && PlayerbulletX <= EnemyX-15 + 30 && PlayerbulletY >= EnemyY-15 && PlayerbulletY <= EnemyY-15 + 30){
    enemyvis = false;
    EnemyX = (int)(random(0,400));
    EnemyY = 40;
    EnemySpeed = (int)(random(1,3));
    score += 10;
  }
  else if(PlayerbulletX > EnemyX2-15 && PlayerbulletX < EnemyX2-15 + 30 && PlayerbulletY > EnemyY2-15 && PlayerbulletY < EnemyY2-15 + 30){
    enemy2vis = false;
    EnemyX2 = (int)(random(0,400));
    EnemyY2 = 40;
    EnemySpeed2 = (int)(random(1,3));
    score += 100;
  }
  else if(PlayerbulletX > EnemyX3-15 && PlayerbulletX < EnemyX3-15 + 30 && PlayerbulletY > EnemyY3-15 && PlayerbulletY < EnemyY3-15 + 30){
    enemy3vis = false;
    EnemyX3 = (int)(random(0,400));
    EnemyY3 = 40;
    EnemySpeed3 = (int)(random(1,3));
    score += 1000;
  }
  //If the Player's bullets has not hit the Enemies, they will remain visible.
  else{
    enemyvis = true;
    enemy2vis = true;
    enemy3vis = true;
    
  }
}

//This draws the sky background
void drawbg(){
  noStroke();
  fill(29, 42, 97, 50);
  rect(0, 0, width, height);
  
  //sky shades:
  noStroke();
  fill(25, 33, 68, 50);
  rect(0, 0, width+100, height-100);
  
  noStroke();
  fill(10, 16, 41, 50);
  rect(0, 0, width+200, height-200);
  
  noStroke();
  fill(0, 0, 0, 50);
  rect(0, 0, width+300, height-300);
  
  //This overlays the purple image over the background
  PImage img = loadImage("purpleoverlay.jpg");
  image(img, 0, 0);
  tint(255, 30); 
  
  //The image is a free public-domain image:
  //MaxPixel.net. (n.d.). Purple Background Gradient Teal Texture Colorful [Digital image]. 
  //Retrieved from https://www.maxpixel.net/Purple-Background-Gradient-Teal-Texture-Colorful-1741713
}