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:
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.
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
4. Add code to run when you Stop the game
e.g. 5. Add code to make the bird go up and down.
C. The entire code that goes on the card is below: Card Script: (this code goes on the card)
|
Game Programming > FlappyBird >








