Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
// Game name: Rocket       Designer: Zhuosheng Luo
// Move your spaceship to keep away from falling stone
// Press "A" to move left, press"D"to move right
// You will lose the game if the length of blood equal to zero



int playerX = 200;
int playerY = 350;
int playerspeed=3;
int ballX =0;
int ballY =0;
int ballx =0;
int bally =0;
int balla=0;
int ballb=0;
int ballYspeed = 8;
int ballyspeed = 8;
int ballbspeed = 8;
int bloodX=200;
float smokewidth=0.3;
float smoke=2;
boolean hit = false;
boolean right=false;
boolean left=false;


void setup(){            //set up basic environment
  size(400,400);
  rectMode(CENTER);
  frameRate(60);
}

void draw(){             //bakcground and game content
  background(0);
  gameplay();
}

void gameplay(){         // if blood is greater than zero, game start, if it equals to zero, game over
  if(bloodX>0){
  stars();
  spaceship();
  update();
  ballA();
  ballB();
  ballC();
  blood();
  } else if(bloodX==0)
  { gameover();
}
}

void stars(){         // draw stars, random numbers will make it look like moving
  noStroke();
  fill(random(200,255));
  ellipse(random(50,350),random(50,350),random(2,5),random(2,5));
}

void spaceship(){       // draw spaceship
  noStroke();
  fill(255,0,0);
  triangle(playerX,playerY,playerX-10,playerY+10,playerX+10,playerY+10);
  fill(235);
  rect(playerX,playerY+20,20,20);
  fill(99,221,255);
  ellipse(playerX,playerY+20,10,10);
  fill(random(200,255));
  rect(playerX-13,playerY+40,smoke,25);
  rect(playerX+13,playerY+40,smoke,25);
  smoke=smoke+smokewidth;
  if(smoke>7){
  smoke=2;
  }
  fill(255,0,0);
  triangle(playerX-20,playerY+30,playerX-10,playerY+30,playerX-10,playerY+20);
  triangle(playerX+20,playerY+30,playerX+10,playerY+30,playerX+10,playerY+20);
}



  void keyPressed(){   //player movement
 
  if (key == 'A' || key == 'a'){
   left=true;
  }

  else if (key == 'D' || key == 'd'){
  right=true;
  }
  
}

  void keyReleased() {
    if(key=='A' ||key =='a'){
    left=false;
    }
    else if(key=='D' ||key =='d'){
    right=false;
    }
  }
  
  void update(){
    if (left==true){
    playerX =playerX-playerspeed;
    }
    if(right==true){
      playerX =playerX+playerspeed;
    }
      if (playerX<=15){
    playerX=15;
  }
  if(playerX>=385){
  playerX=385;
  }
  }


void ballA(){                     //falling stone (left)
  ballY = ballY+ ballYspeed;
  if(ballY>random(430,470)){
    ballY=0;
    
    ballX=int(random(50,200));
  }
  noStroke();
  fill(255,230,120);
  triangle(ballX,ballY-random(40,50),ballX-14,ballY,ballX+14,ballY);
  fill(255,0,0);
  triangle(ballX,ballY-random(20,30),ballX-10,ballY-10,ballX+10,ballY-10);
  fill(120);
  ellipse(ballX,ballY,30,30);
}

void ballB(){                     // falling stone (right)
  bally = bally+ ballyspeed;
  if((bally>random(430,470))&& bally!=ballY){
    bally=0;
    
    ballx=int(random(210,385));
  }
  noStroke();
   fill(255,230,120);
  triangle(ballx,bally-random(40,50),ballx-14,bally,ballx+14,bally);
  fill(255,0,0);
  triangle(ballx,bally-random(20,30),ballx-10,bally-10,ballx+10,bally-10);
  fill(120);
  ellipse(ballx,bally,30,30);
}

void ballC(){                        // falling stone (mid)
  ballb = ballb+ ballbspeed;
  if((ballb>random(430,470))&& ballb!=ballY && ballb!=bally){
    ballb=0;
    
    balla=int(random(150,250));
  }
  noStroke();
   fill(255,230,120);
  triangle(balla,ballb-random(40,50),balla-14,ballb,balla+14,ballb);
  fill(255,0,0);
  triangle(balla,ballb-random(20,30),balla-10,ballb-10,balla+10,ballb-10);
  fill(120);
  ellipse(balla,ballb,30,30);
}
void blood(){                        // if falling stone's positions touch the spaceship, the length of blood reduces 20, and the x-axis position of falling stone will back to zero.
 fill(255,0,0);
 quad(0,0,0,20,bloodX,20,bloodX,0);
 
 if(ballX>playerX-20 && ballX<playerX+20 && ballY>350) {
   hit = true;
   ballY = 0;
   }
   
   if(ballx>playerX-20 && ballx<playerX+20 && bally>350) {
   hit = true;
   bally = 0;
   }
   
    if(balla>playerX-20 && balla<playerX+20 && ballb>350) {
   hit = true;
   ballb = 0;
   }
   
   if(hit==true){
   bloodX= bloodX-20;
   hit=false;
   }
}

void gameover(){                        // show up new game scene if blood equals to zero, and click "again" button can restart the game
  fill(70);
  rect(width/2,height/2,width,height);
  fill(0,255,0);
  ellipse(width/2,height/2,100,100);
  fill(255);
  textSize(25);
  stroke(2);
  text("again",width/2-33,height/2+8);
  
  if(mousePressed && mouseX>width/2-50 && mouseX<width/2+50&&mouseY<height/2+50&&
  mouseY>height/2-50){
    bloodX=200;
  }
}