Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//flappy spaceship.
//similar mechanic as flappy bird.
//player control the ship moving it up and down.
//avoid any incoming walls(pipes).
//pres LMB to keep the ship going upward.

//initiaize variables and classes.
    ship s = new ship();//player's ship.
    walls[] w = new walls[3];//walls,obstcles.
    boolean end=false;
    boolean intro=true;
    int score=0;//player's initial score.
    int Fscore;//player's final score(score x10)
    
    asteroid ast1;
    asteroid ast2;
    asteroid ast3;
    asteroid ast4;
    asteroid ast5;
    asteroid ast6;
    asteroid ast7;
    
    
    void setup(){
      size(500,800);
      for(int i = 0;i<3;i++){
      w[i]=new walls(i);
        ast1 = new asteroid(0,random(2,height),random(3,100)*7);
      ast2 = new asteroid(0,random(1,height),random(5,20)*-2);
      ast3 = new asteroid(0,random(3,height),random(2,10)*-6);
      ast4 = new asteroid(0,random(0,height),random(7,10)*-5);
      ast5 = new asteroid(0,random(2,height),random(3,10)*-4);
      ast6 = new asteroid(0,random(0,height),random(3,5)*-3);
      ast7 = new asteroid(0,random(3,height),random(3,10)*-12);
      
}
}
    void draw(){
     background(0);
  if(end){
        
 //display the background asteroids.
  ast1.update();
  ast2.update();
  ast3.update();
  ast4.update();
  ast5.update();
  ast6.update();
  ast7.update();
        
 //ship movement.
  s.move();
}
 //display ship's shape
 s.display();
if(end){
 s.gravity();
}
 //check if ship crashed on sth.
 s.checkCollisions();
for(int i = 0;i<3;i++){
 //generate walls
 w[i].display();
 //collision check.
 w[i].collision();
 //calculate scores.
 Fscore = score*100;
}
 fill(0);
 stroke(255);
 textSize(32);
 if(end){
   //show scores durign gameplay.
 fill(255);
 text("Score:"+score,30,58);
 fill(255,255,255);
 //in game tutorial hint.
 textSize(20);
 text("Pres LMB or any keys to boost your ship!", 65,640);
}else{
 fill(255,0,0);
 if(intro){
   //titlescreen
 text("Press LMB to Start",105,320);
 text("Prepare for Challenges",85,380);
}else{
  //game over page. with you died and score.
 textSize(30);
 text("-You died!-",170,340);
 text("Your Score"+Fscore,160,390);
 textSize(15);
 text("You need more practise, never give it up!", 120,480);
 text("Press Alt+F4 to restart the game.", 140,520);
}
}
}
 
//reset the game.
  void restart(){
  end=true;
  score=0;
  s.Py=400;
  for(int i = 0;i<3;i++){
  w[i].Posx+=550;
  w[i].cashed = false;
}
}
class asteroid{
//background asteroids.moving from right to left.
float astx;
float asty;
float astspeedx;

asteroid(float astposx, float astposy, float astvelox){
astx = astposx;
asty = astposy;
astspeedx = astvelox;
}

void update(){
move();
display();
}

void move(){
//asteroids movements.
astx = astx + astspeedx;
if(astx <0){
  astx = 800;
  asty = random (120,height);
  astspeedx =random(2,10)*-2;}
}

//shape of asteroids in the background.
void display(){
  strokeWeight(2);
  ellipse(astx,asty,20,20);
  }
}
class ship{
//player's ship.
  float Px,Py,Vy;
  ship(){
  Px = 150;
  Py = 500;
  }
  void display(){
   stroke(255);
   fill(20,20,230);
   strokeWeight(2);
   rectMode(CENTER);
   rect(Px,Py,40,20);
   rect(Px-15,Py+15,30,10);
   rect(Px-15,Py-15,30,10);
      
}
//jump in flappybird.
 void boost(){
  Vy=-15; 
}
 void gravity(){
  Vy+=0.8; 
}
 void move(){
  Py+=Vy; 
   for(int i = 0;i<3;i++){
   w[i].Posx-=3;
}
}
//check collision between alls and space ship
  void checkCollisions(){
   if(Py>900){
    end=false;
}
  for(int i = 0;i<3;i++){
  if((Px<w[i].Posx+60&&Px>w[i].Posx-30)&&(Py<w[i].gap-100||Py>w[i].gap+100)){
   end=false; 
}
}
} 
}

//functions when mouse is pressed
  void mousePressed(){
  s.boost();
  intro=false;
  if(end==false){
  restart();
}
}
//functions when a key is pressed.
 void keyPressed(){
  s.boost(); 
  intro=false;
  if(end==false){
  restart();
}
}  
class walls{
 float Posx, gap;
 boolean cashed = false;
 walls(int i){
 Posx = 100+(i*200);
 gap = random(600)+100;
}
  void display(){
  strokeWeight(30);
  stroke(120,0,0);
  line(Posx-10,0,Posx-10,gap-100);
  line(Posx-10,gap+100,Posx-10,800);
       
  strokeWeight(30);
  stroke(220,120,0);
  line(Posx,0,Posx,gap-100);  
  line(Posx,gap+100,Posx,800);
}
  void collision(){
  if(Posx<0){
  Posx+=(200*3);
  gap = random(600)+100;
  cashed=false;
} 
  if(Posx<250&&cashed==false){
  cashed=true;
  score++; 
}
}
}