Devices & Components
10 jumper wires 150mm male
Arduino Uno Rev3
Button
Adafruit OLED 128x32 i2c
Software & Tools
Arduino IDE
Project description
Code
The flappy square code
cpp
The code for Arduino UNO R3
1#include <Adafruit_GFX.h> 2#include <Adafruit_SSD1306.h> 3#include <Wire.h> 4#include <EEPROM.h> 5 6#define SCREEN_WIDTH 128 7#define SCREEN_HEIGHT 64 8 9Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); 10 11const int buttonPin = 2; 12 13//==== Bird physics ====// 14float birdY = 32; 15float velocity = 0; 16float gravity = 0.8; 17float jumpForce = -3.5; 18 19//==== Pipes ====// 20int pipeX = 128; 21int pipeGap = 20; // The gap size bird flies through 22int pipeTopHeight; 23 24//==== Game state ====// 25bool gameOver = false; 26int score = 0; 27 28//==== High score ====// 29int highScore = 0; 30 31void resetGame() { 32 birdY = 32; 33 velocity = 0; 34 pipeX = 128; 35 pipeTopHeight = random(10, 40); 36 score = 0; 37 gameOver = false; 38} 39 40void setup() { 41 pinMode(buttonPin, INPUT_PULLUP); 42 43 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 44 display.clearDisplay(); 45 display.display(); 46 47 randomSeed(analogRead(0)); 48 49 // Load the high score from EEPROM 50 highScore = EEPROM.read(0); 51 52 resetGame(); 53} 54 55void saveHighScore() { 56 if (score > highScore) { 57 highScore = score; 58 EEPROM.write(0, highScore); // Saves the high score to the memory 59 } 60} 61 62void showGameOver() { 63 display.clearDisplay(); 64 65 display.setTextSize(2); 66 display.setTextColor(WHITE); 67 display.setCursor(10, 5); 68 display.print("GAME OVER"); 69 70 display.setTextSize(1); 71 display.setCursor(20, 35); 72 display.print("Score: "); 73 display.print(score); 74 75 display.setCursor(20, 50); 76 display.print("High: "); 77 display.print(highScore); 78 79 display.display(); 80} 81 82void loop() { 83 84 if (gameOver) { 85 showGameOver(); 86 87 if (digitalRead(buttonPin) == LOW) { 88 delay(200); 89 resetGame(); 90 } 91 return; 92 } 93 94 //==== Jump ====// 95 if (digitalRead(buttonPin) == LOW) { 96 velocity = jumpForce; 97 } 98 99 //==== Bird physics ====// 100 velocity += gravity; 101 birdY += velocity; 102 103 //==== Pipe movement ====// 104 pipeX -= 2; 105 106 if (pipeX < -20) { 107 pipeX = 128; 108 pipeTopHeight = random(10, 40); 109 score++; 110 } 111 112 //==== Collisions ====// 113 if (birdY < 0 || birdY > SCREEN_HEIGHT - 8) { 114 saveHighScore(); 115 gameOver = true; 116 } 117 118 //==== Collisions pipes ====// 119 if (pipeX < 20 && pipeX > 0) { 120 if (birdY < pipeTopHeight || birdY > pipeTopHeight + pipeGap) { 121 saveHighScore(); 122 gameOver = true; 123 } 124 } 125 126 127 display.clearDisplay(); 128 129 //==== Bird sprite ====// 130 display.fillRect(10, birdY, 8, 8, WHITE); 131 132 //==== Pipes ====// 133 display.fillRect(pipeX, 0, 7, pipeTopHeight, WHITE); 134 display.fillRect(pipeX, pipeTopHeight + pipeGap, 7, SCREEN_HEIGHT, WHITE); 135 136 //==== Score display ====// 137 display.setTextSize(1); 138 display.setCursor(0, 0); 139 display.print("Score: "); 140 display.print(score); 141 142 //==== Draw high score ====// 143 display.setCursor(65, 0); 144 display.print("Hi: "); 145 display.print(highScore); 146 147 display.display(); 148 149 delay(20); 150}
Downloadable files
Photo Gameplay 2
Photo Gameplay 2
IMG_6438.jpeg

Photo Game Over
Photo Game Over
IMG_6436.jpeg

Photo Game Over 2
Photo Game Over 2
IMG_6435.jpeg

Photo Gameplay
Photo Gameplay
IMG_6444.jpeg

Photo Off
Photo Off
IMG_6434.jpeg

Comments
Only logged in users can leave comments