How To Make A Flappy Game With Arduino and LCD
The Flappy Bird game took the internet by storm for a while. Everyone from old to young had installed this game on their phones and played this game for hours. Now in this article I’m going to show you how you can make a simple flappy bird game using Arduino, a few buttons and LCD screen. First we need this equipments:
1xArduino Uno
1xLCD Screeen
1xButton
Enough Jumper Cable.
In the beginning you have to connect LCD screen and button to Arduino Uno as shown below.
After making connections as shown diagram now you have to put code below to Arduino IDE and upload it to Arduino Uno:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int buttonPin = 6;
const int ledPin = 13;
int buttonState = 0;
int flappyPosition = 1;
int obstaclePosition = 15;
int score = 0;
bool gameOver = false;
void setup() {
lcd.begin(16, 2);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
lcd.print("Flappy Bird");
delay(2000);
lcd.clear();
lcd.print("Score: ");
lcd.setCursor(0, 1);
lcd.print(score);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && !gameOver) {
flappyPosition += 1;
lcd.setCursor(0, flappyPosition - 1);
lcd.print(" ");
lcd.setCursor(0, flappyPosition);
lcd.print("*");
if (flappyPosition == obstaclePosition && obstaclePosition != 0) {
gameOver = true;
}
if (flappyPosition == 2) {
score++;
lcd.setCursor(7, 1);
lcd.print(score);
}
}
obstaclePosition -= 1;
lcd.setCursor(obstaclePosition, 0);
lcd.print("#");
if (obstaclePosition == 0) {
obstaclePosition = 15;
lcd.setCursor(obstaclePosition, 0);
lcd.print(" ");
}
if (gameOver) {
lcd.clear();
lcd.print("Game Over!");
lcd.setCursor(0, 1);
lcd.print("Score: ");
lcd.print(score);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
resetGame();
}
delay(500);
}
void resetGame() {
flappyPosition = 1;
obstaclePosition = 15;
score = 0;
gameOver = false;
lcd.clear();
lcd.print("Score: ");
lcd.setCursor(0, 1);
lcd.print(score);
}
This code simulates a simple flappy bird game on your Arduino Uno. In the game, a “bird” moves upwards using the button and escapes from obstacles. The LCD screen is used to show the playing field and the score. When the game starts, the text “Flappy Bird” appears, then the game starts and the text “Score: 0” appears. As the bird moves upwards, the obstacles move to the left. If the bird is aligned with the height of the obstacles, the game ends and the score is displayed with the text “Game Over!” Then, the game starts again and the score is reset.
If you have some errors make sure you make your connections correctly. One end of the button should be connected to the GND and the other end should be connected to the digital pin 6 of the Arduino. Be sure to set the appropriate pin numbers for the LCD screen. Pins RS, E, D4, D5, D6, and D7 of the LCD must be connected to pins 12, 11, 5, 4, 3, and 2, respectively. Also note that pin 13 is connected to an LED to indicate that an LED is blinking.
If you want, you can make fewer connections using an I2C lcd. You can try out this code and make customizations if you want. Have fun!
If you like my article, you can subscribe to my page via RSS and review my other articles.