Flappy mbed - Flappy Bird game for mbed using uLCD-144-G2

Overview

This project uses the uLCD-144-G2 smart LCD along with a push button and speaker to create a Flappy Bird-esque game for mbed. The game begins with a start screen prompting the user to hit the pushbutton to begin the game. Once the game has begun, the user pushes the pushbutton to control the "bird" in a similar manner to tapping the screen when playing Flappy Bird. The game plays the same score sound when the user successfully passes the obstacles and also the same hit sound when the player hits one of the pipes and the game ends. The game takes advantage of the mbed RTOS to allow these sounds to play from a .wav file in the MicroSd card file system with minimal impact on LCD graphics performance.

/media/uploads/jmelliadis3/flappy_mbed1-2.jpg /media/uploads/jmelliadis3/flappy_mbed2-2.jpg

Components

Circuit

uLCD Pinmbed Pin
+5vVU
RXp28
TXp27
GNDGND
RESp29
Pushbutton Pinmbed Pin
Top Pinsp26
Bottom PinsGND
Speaker Transistor Pinmbed Pin
Collector (Transistor)(Speaker)
Emitter (Transistor)GND
Base (Transistor)p18 (through 1 kOhm R)
(Speaker)VU
MicroSD breakout Pinmbed Pin
DOp6
GNDGND
SCKp7
VCCVOUT
DIp5
CSp8

Sound Files

Troubleshooting

Problems encountered and attempts to mitigate them:

  • Playing .wav files impedes uLCD updating - Solution: used the mbed RTOS to create a separate thread just for playing the point and player dying sounds so that uLCD performance was only slowed and not completely stopped

RTOS threads for WAV playing

void playPointSound(void const *argument) {
    while(1) {
    Thread::signal_wait(0x1);

    waver.play(wave_file);
    
    fclose(wave_file);
    }
}

void playDeadSound(void const *argument) {
    while(1) {
    Thread::signal_wait(0x1);

    FILE *wave_file;
    wave_file=fopen("/sd/sfx_die.wav","r");
    
    waver.play(wave_file);
    
    fclose(wave_file);
    }
}

int main() {
    
    state = begin;
    Thread thread(playPointSound);
    Thread thread2(playDeadSound);

     if (ballxpos == (wall1x1 + 10))
        {
            thread.signal_set(0x1);
            score++;
            wave_file=fopen("/sd/sfx_point.wav","r");
            uLCD.locate(14,0);
            uLCD.printf("%04d", score);   
        }

     if (ballypos < 125)
        {
                thread2.signal_set(0x1);
        }
}
  • Score is erased when the top pipe passes it on the uLCD - Solution: Rewrite the score once the pipe has passed the score position (rewriting the score continuously seemed to affect uLCD performance for the other graphics)

Checking the wall position to rewrite the score

if(wall1x2 < 95 && scoreWrite == 1)
        {
            uLCD.locate(14,0);
            uLCD.printf("%04d", score);
            scoreWrite = 0;
        }

Code

Import programFlappy_mbed

Initial Flappy mbed Game


Please log in to post comments.