SHARE
TWEET

Flappy Bird Game Tutorial Code

a guest Nov 11th, 2016 7,440 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package{
  2.     import flash.display.MovieClip;
  3.     import flash.events.KeyboardEvent;
  4.     import flash.ui.Keyboard;
  5.     import flash.events.Event; //used for ENTER_FRAME event
  6.    
  7.     public class Main extends MovieClip{
  8.        
  9.         //constants
  10.         const gravity:Number = 1.5;            //gravity of the game
  11.         const dist_btw_obstacles:Number = 300; //distance between two obstacles
  12.         const ob_speed:Number = 8;             //speed of the obstacle
  13.         const jump_force:Number = 15;          //force with which it jumps
  14.        
  15.         //variables
  16.         var player:Player = new Player();      
  17.         var lastob:Obstacle = new Obstacle();  //varible to store the last obstacle in the obstacle array
  18.         var obstacles:Array = new Array();     //an array to store all the obstacles
  19.         var yspeed:Number = 0;                 //A variable representing the vertical speed of the bird
  20.         var score:Number = 0;                  //A variable representing the score
  21.        
  22.         public function Main(){
  23.             init();
  24.         }
  25.        
  26.         function init():void {
  27.             //initialize all the variables
  28.             player = new Player();
  29.             lastob = new Obstacle();
  30.             obstacles = new Array();
  31.             yspeed = 0;
  32.             score = 0;
  33.            
  34.             //add player to center of the stage the stage
  35.             player.x = stage.stageWidth/2;
  36.             player.y = stage.stageHeight/2;
  37.             addChild(player);
  38.            
  39.             //create 3 obstacles ()
  40.             createObstacle();
  41.             createObstacle();
  42.             createObstacle();
  43.            
  44.             //Add EnterFrame EventListeners (which is called every frame) and KeyBoard EventListeners
  45.             addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
  46.             stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
  47.         }
  48.        
  49.         private function key_up(event:KeyboardEvent){
  50.             if(event.keyCode == Keyboard.SPACE){
  51.                 //If space is pressed then make the bird
  52.                 yspeed = -jump_force;
  53.             }
  54.         }
  55.        
  56.         function restart(){
  57.             if(contains(player))
  58.                 removeChild(player);
  59.                 for(var i:int = 0; i < obstacles.length; ++i){
  60.                     if(contains(obstacles[i]) && obstacles[i] != null)
  61.                     removeChild(obstacles[i]);
  62.                     obstacles[i] = null;
  63.                 }
  64.                 obstacles.slice(0);
  65.                 init();
  66.         }
  67.        
  68.         function onEnterFrameHandler(event:Event){
  69.             //update player
  70.             yspeed += gravity;
  71.             player.y += yspeed;
  72.            
  73.             //restart if the player touches the ground
  74.             if(player.y + player.height/2 > stage.stageHeight){
  75.                 restart();
  76.             }
  77.            
  78.             //Don't allow the bird to go above the screen
  79.             if(player.y - player.height/2 < 0){
  80.                 player.y = player.height/2;
  81.             }
  82.            
  83.             //update obstacles
  84.             for(var i:int = 0;i<obstacles.length;++i){
  85.                 updateObstacle(i);
  86.             }
  87.            
  88.             //display the score
  89.             scoretxt.text = String(score);
  90.         }
  91.        
  92.         //This functions update the obstacle
  93.         function updateObstacle(i:int){
  94.             var ob:Obstacle = obstacles[i];
  95.            
  96.             if(ob == null)
  97.             return;
  98.             ob.x -= ob_speed;
  99.            
  100.             if(ob.x < -ob.width){
  101.                 //if an obstacle reaches left of the stage then change its position to the back of the last obstacle
  102.                 changeObstacle(ob);
  103.             }
  104.            
  105.             //If the bird hits an obstacle then restart the game
  106.             if(ob.hitTestPoint(player.x + player.width/2,player.y + player.height/2,true)
  107.                || ob.hitTestPoint(player.x + player.width/2,player.y - player.height/2,true)
  108.                || ob.hitTestPoint(player.x - player.width/2,player.y + player.height/2,true)
  109.                || ob.hitTestPoint(player.x - player.width/2,player.y - player.height/2,true)){
  110.                 restart();
  111.             }
  112.            
  113.             //If the bird got through the obstacle without hitting it then increase the score
  114.             if((player.x - player.width/2 > ob.x + ob.width/2) && !ob.covered){
  115.                 ++score;
  116.                 ob.covered = true;
  117.             }
  118.         }
  119.        
  120.         //This function changes the position of the obstacle such that it will be the last obstacle and it also randomizes its y position
  121.         function changeObstacle(ob:Obstacle){
  122.             ob.x = lastob.x + dist_btw_obstacles;
  123.             ob.y = 100+Math.random()*(stage.stageHeight-200);
  124.             lastob = ob;
  125.             ob.covered = false;
  126.         }
  127.        
  128.         //this function creates an obstacle
  129.         function createObstacle(){
  130.             var ob:Obstacle = new Obstacle();
  131.             if(lastob.x == 0)
  132.             ob.x = 800;
  133.             else
  134.             ob.x = lastob.x + dist_btw_obstacles;
  135.             ob.y = 100+Math.random()*(stage.stageHeight-200);
  136.             addChild(ob);
  137.             obstacles.push(ob);
  138.             lastob = ob;
  139.         }
  140.        
  141.        
  142.     }
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top