Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*////////////////////////
 
  < Avoid Stuff >
 
 Lawrence Le
 991467736
 IMC Interactive Toy Assignment
 
 
 - Click and drag the ship with the mouse
 - Dodge the red ships
 - Collect the orbs
 - Score bar increases in length with each orb collected
 - Getting hit by a red ship resets the score bar and your ship
 
 ////////////////////////*/


////////Variables////////

//Ship's Starting Coordinates
float shipOriginX = 200; //X value
float shipOriginY = 200; // Y value

//Score Bar Length Increase
int scoreCounter = 0;

//Enemies Types
float mobTopX = random(25, 375); //Mob coming from the top - x 
float mobTopY = random(0, -100); //Mob coming from the top - y

float mobLeftX = random(0, -100); //Mob coming from the left - x
float mobLeftY = random(25, 375); //Mob coming from the left - y

float mobRightX = random(400, 500); //Mob coming from the right - x
float mobRightY = random(25, 375); //Mob coming from the right - y

float mobDownX = random(25, 375); //Mob coming from the bottom - x
float mobDownY = random(400, 500); //Mob coming from the bottom - y

//Collectibles
float collectRightX = random(400, 500); //Collectible coming from the right side - x
float collectRightY = random(25, 375); //Collectible coming from the right side - y

float collectLeftX = random(0, -100); //Collectible coming from the left side - x
float collectLeftY = random(25, 375); //Collectible coming from the left side - y

//Movement Speed
float mobSpeed = 4; //Speed for the four mob types
float collectSpeed = 2; //Speed for the two collectibles

//Background Scroll Speed
float scrollSpeed; //Speed at which background scrolls upwards

////////////////////////

//Basic Settings (Size, No Outlines, Frame Rate)
void setup() {

  size(400, 400);
  noStroke();
  frameRate(60);
}

//Placing All The Functions, Etc.
void draw() {

  scrollBackground();
  drawShip();
  drawMobTop();
  drawMobLeft();
  drawMobRight();
  drawMobDown();
  mobReset();
  drawCollectLeft();
  drawCollectRight();
  collectReset();
  borderPattern();
  scoreBar();
  drawMobCollision();
  drawCollectCollision();
}


// Player's Ship (Drawing and Moving)
void drawShip() {

  // Hold Mouse Button While Hovering Over To Drag Ship
  if (mousePressed) {
    if ((mouseX >= shipOriginX - 25) && (mouseX <= shipOriginX + 25)) {
      if ((mouseY >= shipOriginY - 25) && (mouseY <= shipOriginY + 25)) {
        shipOriginX = mouseX;
        shipOriginY = mouseY;
      }
    }
  }

  //Limiting Movement To The Border
  if (shipOriginX > 390) {
    shipOriginX = 390;
  }
  if (shipOriginY > 390) {
    shipOriginY = 390;
  }
  if (shipOriginX < 10) {
    shipOriginX = 10;
  }
  if (shipOriginY < 10) {
    shipOriginY = 10;
  }

  // Bubble Around Ship
  ellipseMode(CENTER);
  fill(150, 150, 255);
  ellipse(shipOriginX, shipOriginY, 50, 50);
  fill(255, 125);
  ellipse(shipOriginX, shipOriginY, 47 + sin(frameCount/16)*8, 47 + sin(frameCount/16)*8);

  //Ship Shapes
  rectMode(CENTER);
  fill(0 + sin(frameCount/10)*5);
  rect(shipOriginX, shipOriginY, 28, 28);
  fill(190 + sin(frameCount/10)*5);
  rect(shipOriginX, shipOriginY, 26, 26);
  fill(0);
  quad(shipOriginX + 20, shipOriginY, shipOriginX, shipOriginY - 20, shipOriginX - 20, shipOriginY, shipOriginX, shipOriginY + 20);
  fill(240);
  quad(shipOriginX + 19, shipOriginY, shipOriginX, shipOriginY - 19, shipOriginX - 19, shipOriginY, shipOriginX, shipOriginY + 19);
  fill(200 + sin(frameCount/10)*5, 25 + sin(frameCount/10)*5, 25 + sin(frameCount/10)*5);
  ellipseMode(CENTER);
  ellipse(shipOriginX, shipOriginY, 15, 15);
  fill(255, 100 + sin(frameCount/10)*5);
  ellipse(shipOriginX + 1.2, shipOriginY + 1.2, 12, 12);
  fill(255, 125 + sin(frameCount/10)*5);
  ellipse(shipOriginX + 3, shipOriginY + 2.5, 5, 5);
}

//Dotted Border
void borderPattern() {
  fill(255);
  rectMode(CENTER);
  
  //Top edge 
  for (int square = 10; square < width; square = square + 20) {
    rect(square, 10, 10, 10);
  }
  //Bottom edge
  for (int square = 10; square < height; square = square + 20) {
    rect(square, 390, 10, 10);
  }
  //Left edge
  for (int square = 10; square < width; square = square + 20) {
    rect(10, square, 10, 10);
  }
  //Right edge
  for (int square = 10; square < height; square = square + 20) {
    rect(390, square, 10, 10);
  }
}

//Mobs That Spawn From The TOP
void drawMobTop() {
  fill(255, 100, 100);
  triangle(mobTopX, mobTopY + 10, mobTopX - 10, mobTopY - 10, mobTopX + 10, mobTopY - 10); 
  fill(255, 100, 100);
  triangle(mobTopX - 10, mobTopY - 10, mobTopX + 10, mobTopY - 10, mobTopX, mobTopY - 35);
  fill(255, 255, 125, 50);
  triangle(mobTopX - 10, mobTopY - 15, mobTopX + 10, mobTopY - 15, mobTopX, mobTopY - 40);
  fill(255, 255, 50);
  ellipse(mobTopX, mobTopY - 3, 7, 7);
  fill(255, 255, 125);
  ellipse(mobTopX, mobTopY - 3, 4, 4);

  //Top Mob Movement
  mobTopY += mobSpeed;
}

//Mobs That Spawn From The LEFT
void drawMobLeft() {
  fill(255, 0, 0);
  ellipse(mobLeftX, mobLeftY, 20, 20);
  fill(240, 100, 100);
  ellipse(mobLeftX, mobLeftY, 15, 15);
  fill(75, 240, 75);
  ellipse(mobLeftX, mobLeftY, 10, 10);
  fill (240, 100, 100);
  triangle(mobLeftX + 7, mobLeftY + 5, mobLeftX + 7, mobLeftY - 5, mobLeftX + 23, mobLeftY);

  //Left Mob Movement
  mobLeftX += mobSpeed;
} 

//Mobs That Spawn From The RIGHT
void drawMobRight() {
  rectMode(CENTER);
  fill(190, 50, 50);
  rect(mobRightX, mobRightY, 25, 25);
  rect(mobRightX + 15, mobRightY, 5, 5);
  rect(mobRightX - 15, mobRightY, 5, 5);
  rect(mobRightX, mobRightY + 15, 5, 5);
  rect(mobRightX, mobRightY - 15, 5, 5);
  fill(60, 180, 60);
  rect(mobRightX, mobRightY, 15, 15);
  fill(30, 210, 30);
  rect(mobRightX, mobRightY, 5, 5);

  //Right Mob Movement
  mobRightX -= mobSpeed;
}  

//Mobs That Spawn From DOWN
void drawMobDown() {
  fill(225, 0, 0);
  quad(mobDownX, mobDownY, mobDownX + 8, mobDownY + 8, mobDownX, mobDownY - 32, mobDownX - 8, mobDownY + 8);
  fill(255, 200, 0);
  quad(mobDownX, mobDownY - 4, mobDownX + 5, mobDownY + 5, mobDownX, mobDownY - 24, mobDownX - 5, mobDownY + 5);

  //Down Mob Movement
  mobDownY -= mobSpeed;
}

//Mob Reset Points
void mobReset() {

  //Top Mob Reset Position
  if (mobTopY >= 400) {
    mobTopX = random(25, 375);
    mobTopY = random(0, -100);
  }   

  //Left Mob Reset Position
  if (mobLeftX >= 400) {
    mobLeftX = random(0, -100);
    mobLeftY = random(25, 375);
  }   

  //Right Mob Reset Position
  if (mobRightX <= 0) {
    mobRightX = random(400, 500);
    mobRightY = random(25, 375);
  }   

  //Down Mob Reset Position
  if (mobDownY <= 0) {
    mobDownX = random(25, 375);
    mobDownY = random(400, 500);
  }
}

//Collectible That Spawns From The LEFT

void drawCollectLeft() {
  fill(255, 255, 0);
  ellipse(collectLeftX, collectLeftY, 15, 15);
  fill(240, 240, 30);
  ellipse(collectLeftX, collectLeftY, 12, 12);

  //Left Collectible Movement
  collectLeftX += collectSpeed;
}

//Collectible That Spawns From The RIGHT

void drawCollectRight() {
  fill(255, 255, 0);
  ellipse(collectRightX, collectRightY, 15, 15);
  fill(240, 240, 30);
  ellipse(collectRightX, collectRightY, 12, 12);

  //Right Collectible Movement
  collectRightX -= collectSpeed;
}

//Collectible Reset Points

void collectReset() {

  //Left Collectible Reset Position
  if (collectLeftX >= 400) {
    collectLeftX = random(-100, -150);
    collectLeftY = random(25, 375);
  }

  //Right Collectible Reset Position
  if (collectRightX <= 0) {
    collectRightX = random(600, 650);
    collectRightY = random(25, 375);
  }
}

//Score Bar

void scoreBar() {
  fill(255, 0, 0);
  rectMode(CORNERS);
  rect(50, 40, 50 + scoreCounter*3, 50); //Increase Length With Every Pick-Up
}

//Collisions Between Player's Ship And Mobs (Sent Back To Starting Positon)

void drawMobCollision() {

  //Collision With Mob From The Top
  if (dist(mobTopX, mobTopY, shipOriginX, shipOriginY) <= 35) {
    shipOriginX = 200;
    shipOriginY = 200;
    scoreCounter = 0; //Score resets
  }
  
  //Collision With Mob From The Left
  if (dist(mobLeftX, mobLeftY, shipOriginX, shipOriginY) <= 35) {
    shipOriginX = 200;
    shipOriginY = 200;
    scoreCounter = 0; //Score resets
  }
  
  //Collision With Mob From The Right
  if (dist(mobRightX, mobRightY, shipOriginX, shipOriginY) <= 35) {
    shipOriginX = 200;
    shipOriginY = 200;
    scoreCounter = 0; //Score resets
  }
  
  //Collision With Mob From The Down
  if (dist(mobDownX, mobDownY, shipOriginX, shipOriginY) <= 35) {
    shipOriginX = 200;
    shipOriginY = 200;
    scoreCounter = 0; //Score resets
  }
}

//Collisions Between Player's Ship and Collectibles (Collectibles Are Reset And Points Are Scored)

void drawCollectCollision() {
 
  //Collision With Collectible From The Left
  if (dist(collectLeftX, collectLeftY, shipOriginX, shipOriginY) <= 20) {
    collectLeftX = random(-100, 0);
    collectLeftY = random(25, 375);
    scoreCounter += 1; //Gain one score point
  }
  
  //Collision With Collectible From The Right
  if (dist(collectRightX, collectRightY, shipOriginX, shipOriginY) <= 20) {
    collectRightX = random(400, 500);
    collectRightY = random(25, 375);
    scoreCounter += 1; //Gain one score point
  }
}

  
//Scrolling Background

void scrollBackground() {
  
  //Background Shapes
  rectMode(CORNERS);
  fill(45);
  rect(0, 0 - scrollSpeed, 400, 100 - scrollSpeed);
  fill(50);
  rect(0, 100 - scrollSpeed, 400, 200 - scrollSpeed);
  fill(60);
  rect(0, 200 - scrollSpeed, 400, 300 - scrollSpeed);
  fill(70);
  rect(0, 300 - scrollSpeed, 400, 400 - scrollSpeed);
  fill(60);
  rect(0, 400 - scrollSpeed, 400, 500 - scrollSpeed);
  fill(50);
  rect(0, 500 - scrollSpeed, 400, 600 - scrollSpeed);
  fill(45);
  rect(0, 600 - scrollSpeed, 400, 700 - scrollSpeed);
  fill(50);
  rect(0, 700 - scrollSpeed, 400, 800 - scrollSpeed);
  fill(60);
  rect(0, 800 - scrollSpeed, 400, 900 - scrollSpeed);
  fill(70);
  rect(0, 900 - scrollSpeed, 400, 1000 - scrollSpeed);
  
  //Speed of Background Shapes
  scrollSpeed = scrollSpeed + 2;
  
  //Reset Background Shape Positions To Create Scrolling Effect
  if (scrollSpeed >= 600) {
    scrollSpeed = 0;
  }
}