Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Survive 60 seconds   Created by yulin Wang
//Rules: Move mouse to controll the blue ball, avoid red balls as long as possible
//yellow bar shows the time, when it extends fully to the edge. It is 60 seconds.
//Dark red ball is the mother ball, mother ball doesn't change speed. 
//Light red balls are children, children can increase the speed by hitting left and right side wall and
// hit mother ball, the direction will change as well.


//Set up windows size, and shape mode and color mode.
void setup() {
  size(600, 600);
  ellipseMode(CENTER);
  smooth();
  noCursor();
  colorMode(RGB);
}

// Set up different variables
// variable for enemy1
float e1X=0;
float e1Y=random(120, 480);
float x1Direction=1;
float y1Direction=1;
float e1Speed=4;
//variable for enemy2(mother ball)
float e2X=10;
float e2Y=random(150, 450);
float x2Direction=1;
float y2Direction=1;
float e2Speed=4;
//variable for enemy3
float e3X=600;
float e3Y=random(220, 380);
float x3Direction=1;
float y3Direction=1;
float e3Speed=3.5;
//variable for mouse moving area
int areaDown=500;
int areaUp=100;
float narrowSpeed=0.01;
//variable for ball size
int sizeAmount=30;
//variable for game over
boolean gameOver=false; 

//Draw background, scorebar, wall, player
//Draw enemy2 at the begining, show enemy1 10 seconds, show enemy3 at 20 second
void draw() {

  back();
  scoreBar();
  wall();
  player();
  if (frameCount/60>10) {
    enemy1();
  }
  enemy2();
  if (frameCount/60>20) {
    enemy3();
  }
  doGameOver();
  overScreen();
}

//Game Over If player hit any of the enemy, game over. Show black screen and text
void doGameOver() {
  //Game over
  if (frameCount/60>10 && mouseX<e1X+20 && mouseX>e1X-20 && mouseY<e1Y+20 && mouseY>e1Y-20) {
    gameOver=true;
  }
  if (mouseX<e2X+20 && mouseX>e2X-20 && mouseY<e2Y+20 && mouseY>e2Y-20) {
    gameOver=true;
  }
  if (frameCount/60>30 && mouseX<e3X+20 && mouseX>e3X-20 && mouseY<e3Y+20 && mouseY>e3Y-20) {
    gameOver=true;
  }
}
//If game is over, show the over screen
void overScreen() {
  if (gameOver==true) {
    background(0);
    textSize(50);
    text("GameOver", 185, 300);
  } else {
    //println(frameCount/60);
  }
}

//Player, mouse location is limited within the wall. 

void player() {
  while ( mouseY>areaDown) {
    mouseY=areaDown;
  }
  while ( mouseY<areaUp) {
    mouseY=areaUp;
  }

  fill(50, 50, 50, 150);
  ellipse(pmouseX, pmouseY, sizeAmount+2, sizeAmount+2);

  noStroke();
  fill(68, 123, 216, 150);
  ellipse(mouseX, mouseY, sizeAmount+5, sizeAmount+5);
  fill(68, 123, 216);
  ellipse(mouseX, mouseY, sizeAmount-3, sizeAmount-3);

  stroke(255);
  strokeWeight(5);
  line(mouseX, mouseY, pmouseX, pmouseY);
}

//Enemy1 ,speed increase and change direction when hit side walls or mother ball. When the hit player, game over.
void enemy1() {
  noStroke();
  fill(230, 62, 62);
  ellipse(e1X, e1Y, sizeAmount-5, sizeAmount-5);

  e1X=e1X+(e1Speed*x1Direction);
  e1Y=e1Y+(e1Speed*y1Direction);

  if (e1Y>areaDown) {
    e1Y=areaDown;
    y1Direction*=-1;
  }
  if (e1Y<areaUp) {
    e1Y=areaUp;
    y1Direction*=-1;
  }

  if (e1X>600 || e1X<0 ) {
    x1Direction*=-1;
  }

  if ( abs(e1Y-e2Y)<15 && abs(e1X-e2X)<15) {
    y1Direction*=-1;
    x1Direction*=-1;
  }
  if (abs(e1Y-e2Y)<15 && abs(e1X-e2X)<15 || e1X>600 || e1X<0  ) {
    e1Speed=e1Speed+0.2;
  }
  if (e1Speed>=13) {
    e1Speed=13;
  }
}
//Enemy2 Mother ball   show at the beginning of the game Speed doesn't change
void enemy2() {
  noStroke();
  fill(120, 12, 12);
  ellipse(e2X, e2Y, sizeAmount-2, sizeAmount-2);

  e2X=e2X+(e2Speed*x2Direction);
  e2Y=e2Y-(e2Speed*y2Direction);

  if (e2Y>areaDown) {
    e2Y=areaDown;
    y2Direction*=-1;
  }
  if (e2Y<areaUp) {
    e2Y=areaUp;
    y2Direction*=-1;
  }
  if (e2X>600 || e2X<0) {
    x2Direction*=-1;
  }
}
//Enemy3 ,speed increase and change direction when hit side walls or mother ball. When the hit player, game over.
void enemy3() {
  noStroke();
  fill(230, 62, 62);
  ellipse(e3X, e3Y, sizeAmount-5, sizeAmount-5);
  e3X=e3X+(e3Speed*x3Direction);
  e3Y=e3Y+(e3Speed*y3Direction);

    if (e3Y>areaDown) {
      e3Y=areaDown;
      y3Direction*=-1;
    }
    if (e3Y<areaUp) {
      e3Y=areaUp;
      y3Direction*=-1;
    }
    if (e3X>600 || e3X<0 ) {
      x3Direction*=-1;
    }

    if ( abs(e3Y-e2Y)<15 && abs(e3X-e2X)<15) {
      y3Direction*=-1;
      x3Direction*=-1;
    }
    if (abs(e3Y-e2Y)<150 && abs(e3X-e2X)<15 || e3X>600 || e3X<0  ) {
      e3Speed=e3Speed+0.2;
    }
    if (e3Speed>=13) {
      e3Speed=13;
    }
  }
// set background
  void back(){
    background(100, 116, 100);
    rectMode(CORNER);
  }

//Set function wall 
  void wall() {
    rectMode(CORNER);
    fill(72, 90, 118);  
    rect(0, 0, 600, areaUp-sizeAmount+10);

    rectMode(CORNER);
    fill(72, 90, 118);  
    rect(0, areaDown+sizeAmount-10, 600, 600);

    if (frameCount/60>=10) {
      areaUp=120;
      areaDown=480;
    }
    if (frameCount/60>=20) {
      areaUp=150;
      areaDown=450;
    }
    if (frameCount/60>=30) {
      areaUp=175;
      areaDown=425;
    }
    if (frameCount/60>=40) {
      areaUp=190;
      areaDown=410;
    }
  }
//Set function score bar, it extend when time moves on
  void scoreBar() {

    rectMode(CENTER);
    fill(250, 222, 60);
    rect(300, 300, frameCount/6, 500);
  }