Game Programming‎ > ‎FlappyBird‎ > ‎

Flappy Bird 1

Moving the Bird

A. Setting up the Screens/Objects/Graphics

   Steps:
     1. Open New Mainstack, Click on Inspector and name the stack -  "Flappy Bird"

                     

    2. Add a Substack

                   

     3. Click on the Inspector icon, name it "Graphics

                        


          and move it to the side. We will be saving our graphics on it.

    4. Import the bird graphic onto the Graphics substack

                 

       5.  Note it's ID#  (mine is 1003)
                                                            


    6. Add a button on the "Flappy Bird" stack, name it "bird" and uncheck the boxes as shown below
                                      

                     

    7. Set it's icon to the ID# of the bird graphic, and uncheck the checkboxes as shown below (no borders or 3D)


                                        



       8. Click both buttons - "Fit Content" to fit the button to the graphic

                                



    9. Add Start and Stop Buttons, set their code to be "startGame" and "stopGame"

                                   
                            


B. Writing the Code To Make it Work...

    We are going to put most of the code on the Card Script:


    1. Add the variables that we will need:

        We are going to make 3 variables to control the game and the bird:
                       
1. We need a variable to tell if the game is still going. When the game is over or if the player presses the "pause" button, we want to stop all action. 

                            global gameisRunning      It will be set to either "true" or "false"
                    
                    
2. We need a variable to tell which direction to move the bird. Normally, it will be falling (gravity) When you press the "space" bar, it will fly/more up against gravity. 

                            global birdDirection    - It will be set to either "+1" for Down or "+1" for Up
                    
                    
3. We need a variable to set the bird's speed. By using a variable, we can easily change the speed by changing this one variable. We could make each level harder as the bird goes faster.
  

                            global birdSpeed    -  It will be set to a number usually between 1 and 20
                    
    

                e.g. 
  Card Script:    (this code goes on the card)
        
                        global
 birdDirection                                     // +1 is down, -1 is up
                        global birdSpeed                                        // speed the bird falls
                        global gameisRunning


    2. Add the code to move the bird

        To move the bird, we add or subtract from it's location. We use birdDirection and birdSpeed.
        Then we check if the game is still running, and if it is, we send a message to move again in 20 millisecs (0.02 secs)

            e.g.

                                on moveBird
                                set the bottom of btn "bird" to the bottom of btn "bird" + birdDirection * birdSpeed
                                if gameisRunning then
                                      send "moveBird" to me in 20 millisecs
                                end if
                           end moveBird

    3. Code to setup the initial variables and start the game

When we start the game, we want to
  • Set gameisRunning to true
  • Set the bird direction to down (+1)
  • Set the bird's speed of falling to 3
  • Start the bird moving

e.g.

    on startGame
        put true into gameisRunning
        put +1 into birdDirection                      // bird will be falling
        put 3 into birdSpeed
        moveBird
    end startGame


    4. Add code to run when you Stop the game

  When stopping the Game, we want to:
  • Set gameisRunning to false
  • Return the bird to its starting position
            e.g.

    on stopGame
          put false into gameisRunning
          set the bottom of btn "bird" to 400
    end stopGame


    5. Add code to make the bird go up and down.  

Normally, the bird will go down (falling because of gravity). Pressing the space bar  (keyDown) causes the bird to rise. Taking the finger off the space bard (keyUp) lets the bird falling again

  e.g.

      on keyDown    theKey                            // The keyDown message tells you what key was pressed,
          if theKey is space then                     //       we called the variable "theKey"
                put -1 into birdDirection              // move the bird up
          end if
     end keyDown

    on keyUp
          put +1 into birdDirection                 // let the bird start falling again
    end keyUp




C. The entire code that goes on the card is below:

         Card Script:    (this code goes on the card)

global birdDirection                                     // +1 is down, -1 is up
global birdSpeed                                        // speed the bird falls
global gameisRunning

on moveBird
        set the bottom of btn "bird" to the bottom of btn "bird" + birdDirection * birdSpeed
        if gameisRunning then
                send "moveBird" to me in 20 millisecs
        end if
end moveBird

on startGame
        put true into gameisRunning
        put +1 into birdDirection                      // bird will be falling
        put 3 into birdSpeed
        moveBird
end startGame


on stopGame
        put false into gameisRunning
        set the bottom of btn "bird" to 400
end stopGame


on keyDown    theKey
        if theKey is space then
                put -1 into birdDirection              // move the bird up
        end if
end keyDown

on keyUp
        put +1 into birdDirection                 // let the bird start falling again
end keyUp

Comments