How to Add Keyboard Controls to Your Flappy Bat Game

By Derek Breen

Your Flappy Bat game project might seem just like a simple animation, but these next steps will transform your static project into an interactive game. Start by allowing the player to control the bat. Like that other flappy game, you will tap one key to make the bat move up a bit; use the spacebar because it is the largest key and can easily be tapped by righties or lefties.

  1. Click the Player sprite and then click the Scripts tab.

  2. Drag the following new blocks to the right of the other blocks already in the Scripts Area:

    image0.jpg

  3. Click the Green Flag button to test your code.

Each time you tap the spacebar, the bat should move a bit higher, so the CHANGE Y block must have something to do with vertical movement. In Scratch, the vertical position of a sprite is represented by the Y value and the horizontal position is represented by the X value.

image1.jpg

See how the shape of the WHEN KEY PRESSED block is the same as the WHEN GREEN FLAG CLICKED block? Each hat-shaped block represents an action, such as clicking the Green Flag button or pressing the spacebar. When the action occurs, the code snapped beneath its related hat-shaped block runs. If you want to use a different key to control the flapping, click Spacebar and drag down to a new keyboard key inside WHEN KEY PRESSED.