· 

Flappy Birdもどき

(著)山たー

Flappy Birdというゲームが欧米の子供たちの間で人気だそうなので、それっぽいものを作ってみた。メニュー画面では、スペースキーでレベル設定。Xで「ノーマルモード」か「サドンデスモード」を選択できる。Enterでゲーム開始。ゲーム開始後はスペースキーを押してジャンプ。右から来る障害物に当たらないようにしよう。

ソースコード

int game_seq=0;
int game_stage=1;
int game_mode=0;
float player_x;
float player_y;
float player_w=10;
float player_h=10;
float g=9.8;
float dt=0.01;
float t;
float t2=0;
float hue;
float object1_x;
float object1_y;
float object1_w;
float object1_h;
float object2_x;
float object2_y;
float object2_w;
float object2_h;
float object1_speed=6;
float object2_speed=6;
float score;
float tmp_object1_h;
float tmp_object2_y;
void setup(){
 size(500,500);
 background(255);
 colorMode(HSB);
 gameInit();
}

void draw(){
  background(255);
  if(game_seq==0){
    gameTitle();
  }else if(game_seq==1){
    gamePlay();
  }else if(game_seq==2){
    gameOver();
  }else if(game_seq==3){
    gameClear();
  }
}

void gameInit(){
  game_seq = 0;
  player_x=width/2;
  player_y=height/2;
  object1_x=width;
  object1_y=0;
  object1_w=100;
  object1_h=200;
  object2_x=width;
  object2_y=object1_h+150;
  object2_w=100;
  object2_h=height-object2_y;
  tmp_object1_h=object1_h;
  tmp_object2_y=object2_y;
  hue=0;
  t=0.5;
  t2=0;
  score=0;
}

void gameTitle(){
  background(255);
  playerDisp();
  textAlign(CENTER);
  textSize(40);
  fill(80,255,150);
  text("Flappy-Bird-like game",width/2,50);
  textSize(20);
  fill(10,255,255);
  text("You can fly when you press SPACE key",width/2,100);
  text("Select STAGE with SPACE key",width/2,height/2+50);
  text("Select Game Mode with TAB key",width/2,height/2+120);
  textSize(30);
  fill(0);
  text("STAGE : "+game_stage,width/2,height/2+80);
  if(game_mode==0){
  text("Game Mode : Normal",width/2,height/2+150);
  }else if(game_mode==1){
  text("Game Mode : Sudden Death",width/2,height/2+150);
  }
  textSize(20);
  fill(150,255,255);
  text("Press ENTER key to Start Game",width/2,height-50);
}

void gamePlay(){
  playerDisp();  
  playerMove();
  if(game_stage==1){
    objectMove1();
    objectDisp1();
  }else if(game_stage==2){
    objectMove2();
    objectDisp1();
  }else if(game_stage==3){
    objectMove3();
    objectDisp2();
  }else if(game_stage==4){
    objectMove4();
    objectDisp2();
  }else if(game_stage==5){
    objectMove5();
    objectDisp2();
  }
  scoreDisp();
  if(game_mode==0 && score>=10){
    game_seq=3;
  }
}

void playerDisp(){
  fill(0);
  rect(player_x,player_y,player_w,player_h);
}

void playerMove(){
  t+=dt;
  player_y+=g*sq(t)/2;
  if(player_y>height){
    game_seq=2;
  }  
}


void objectDisp1(){
  hue+=0.2;
  fill(hue,255,255);
  if(hue>255){
    hue=0;
  }
  rect(object1_x,object1_y,object1_w,object1_h);
}

void objectDisp2(){
  hue+=0.2;
  fill(hue,255,255);
  if(hue>255){
    hue=0;
  }
  rect(object1_x,object1_y,object1_w,object1_h);
  rect(object2_x,object2_y,object2_w,object2_h);
}

void objectMove1(){
  object1_x+=-object1_speed;
  if(object1_x<0){
    score+=1;
    object1_x=width;
    object1_y=height*int(random(2));
    if(object1_y==0){
      object1_h=random(200,350);
    }else{
      object1_h=random(-200,-350);
    }
  }
  
  if(object1_y==0){
    if(player_x+player_w/2>object1_x && player_x+player_w/2<object1_x+object1_w 
    && player_y+player_h/2<object1_y+object1_h){
      game_seq=2;
    }
  }else{
    if(player_x+player_w/2>object1_x && player_x+player_w/2<object1_x+object1_w 
    && player_y+player_h/2>object1_y+object1_h){
      game_seq=2;
    }
  }
}

void objectMove2(){
  object1_x+=-object1_speed;
  t2+=0.02;
  if(object1_x<0){
    score+=1;
    object1_x=width;
    object1_y=height*int(random(2));
    if(object1_y==0){
      object1_h=random(200,300);
    }else{
      object1_h=random(-200,-300);
    }
    tmp_object1_h=object1_h;
  }
  if(object1_y==0){
      object1_h=tmp_object1_h+50*sin(t2);
    }else{
      object1_h=tmp_object1_h-50*sin(t2);
    }
  
  
  if(object1_y==0){
    if(player_x+player_w/2>object1_x && player_x+player_w/2<object1_x+object1_w 
    && player_y+player_h/2<object1_y+object1_h){
      game_seq=2;
    }
  }else{
    if(player_x+player_w/2>object1_x && player_x+player_w/2<object1_x+object1_w 
    && player_y+player_h/2>object1_y+object1_h){
      game_seq=2;
    }
  }
}

void objectMove3(){
  object1_x+=-object1_speed;
  object2_x+=-object2_speed;
  t2+=0.02;
  if(object1_x<0){
    score+=1;
    object1_x=width;
    object2_x=width;
    object1_h=random(10,height-200);
    object2_y=object1_h+150;
    object2_h=height-object2_y;
  }
  
  if(player_x+player_w/2>object1_x && player_x+player_w/2<object1_x+object1_w 
    && player_y+player_h/2<object1_y+object1_h){
      game_seq=2;
  }else{
    if(player_x+player_w/2>object2_x && player_x+player_w/2<object2_x+object2_w 
    && player_y+player_h/2>object2_y){
      game_seq=2;
    }
  }
}

void objectMove4(){
  object1_x+=-object1_speed;
  object2_x+=-object2_speed;
  t2+=0.01;
  if(object1_x<0){
    score+=1;
    object1_x=width;
    object2_x=width;
    object1_h=random(100,300);
    object2_y=object1_h+150;
    tmp_object1_h=object1_h;
    tmp_object2_y=object2_y;
  }
  object1_h=tmp_object1_h+100*sin(t2);
  object2_y=tmp_object2_y+100*sin(t2);
  object2_h=height-object2_y;
  if(player_x+player_w/2>object1_x && player_x+player_w/2<object1_x+object1_w 
    && player_y+player_h/2<object1_y+object1_h){
      game_seq=2;
  }else{
    if(player_x+player_w/2>object2_x && player_x+player_w/2<object2_x+object2_w 
    && player_y+player_h/2>object2_y){
      game_seq=2;
    }
  }
}

void objectMove5(){
  object1_x+=-object1_speed;
  object2_x+=-object2_speed;
  t2+=0.05;
  if(object1_x<0){
    score+=1;
    object1_x=width;
    object2_x=width;
    object1_h=random(100,300);
    object2_y=object1_h+150;
    tmp_object1_h=object1_h;
    tmp_object2_y=object2_y;
  }
  object1_h=tmp_object1_h+100*sin(t2);
  object2_y=tmp_object2_y+100*sin(t2);
  object2_h=height-object2_y;
  if(player_x+player_w/2>object1_x && player_x+player_w/2<object1_x+object1_w 
    && player_y+player_h/2<object1_y+object1_h){
      game_seq=2;
  }else{
    if(player_x+player_w/2>object2_x && player_x+player_w/2<object2_x+object2_w 
    && player_y+player_h/2>object2_y){
      game_seq=2;
    }
  }
}

void gameOver(){
  background(255);
  scoreDisp();
  textSize(40);
  fill(0,255,255);
  textAlign(CENTER); 
  text("Game Over",width/2,height/2);
  textSize(20);
  fill(20,100,100);
  text("Press ENTER key to Retry",width/2,height/2+40);
}

void gameClear(){
  background(255);
  textSize(40);
  fill(22,245,243);
  textAlign(CENTER); 
  text("STAGE "+game_stage+"  Clear!",width/2,height/2);
  textSize(20);
  fill(20,100,100);
  text("Press ENTER key to New Game",width/2,height/2+40);
  score=0;
}

void scoreDisp(){
  textSize(24);
  fill(0,0,0);
  textAlign(CENTER); 
  text("Score: "+int(score),width/2,30);
}

void keyPressed() {
  if (key == ENTER){
    if(game_seq==0){
      game_seq=1;
    }else if(game_seq==2){
      gameInit();
    }else if(game_seq==3){
      gameInit();
    }
  }else if (key == ' '){
    if(game_seq==0){
      game_stage+=1;
      if(game_stage>5){
        game_stage=1;
      }
    }else if(game_seq==1){
      t=0.5;
      player_y+=-100;
      if(player_y<0){
        player_y=0;
      }
    }
  }else if (key == TAB){
    if(game_seq==0){
      game_mode+=1;
      if(game_mode>1){
        game_mode=0;
      }
    }
  } 
}