Chrome Dino Game
A simple arduino game using and OLED display
Components and supplies
1
Push button
1
Arduino Uno Rev3
1
0.96 Inch OLED I2C Display Module (white)
6
Jumper wires (generic)
1
Breadboard - 400 contacts
Apps and platforms
1
Arduino IDE
Project description
Code
Dino game code
code
1#include <Adafruit_SSD1306.h> 2 3// made by ultramegabombastiucfuze on https://projecthub.arduino.cc/ 4 5#define BUTTON_PIN 3 // button pin 6 7// Game state variables 8bool isJumping = false; 9bool gameOver = false; 10int dinoY = 40; 11int velocity = 0; 12const int gravity = 2; 13const int groundY = 40; 14int cactusX1 = 128; 15int cactusX2 = 180; 16int gameSpeed = 3; 17unsigned long lastSpeedIncrease = 0; 18unsigned long lastFrame = 0; 19unsigned long score = 0; 20 21// Button debounce state 22bool buttonPressed = false; 23 24Adafruit_SSD1306 display(128, 64, &Wire, -1); 25 26void setup() { 27 pinMode(BUTTON_PIN, INPUT_PULLUP); // Enable internal pull-up 28 29 if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 30 while (1); 31 } 32 33 display.clearDisplay(); 34 display.display(); 35 randomSeed(analogRead(0)); // Randomize cactus spawn 36} 37 38void loop() { 39 unsigned long currentTime = millis(); 40 if (currentTime - lastFrame < 50) return; 41 lastFrame = currentTime; 42 43 int buttonState = digitalRead(BUTTON_PIN); 44 45 // --- GAME OVER & RESTART HANDLING --- 46 if (gameOver) { 47 if (buttonState == HIGH && !buttonPressed) { 48 buttonPressed = true; 49 } 50 51 if (buttonState == LOW && buttonPressed) { 52 buttonPressed = false; 53 resetGame(); 54 } 55 56 return; 57 } 58 59 // --- JUMP HANDLING --- 60 if (buttonState == HIGH && !buttonPressed && dinoY == groundY) { 61 buttonPressed = true; 62 isJumping = true; 63 velocity = -13; 64 } 65 if (buttonState == LOW) { 66 buttonPressed = false; 67 } 68 69 // --- PHYSICS --- 70 if (isJumping) { 71 dinoY += velocity; 72 velocity += gravity; 73 if (dinoY >= groundY) { 74 dinoY = groundY; 75 isJumping = false; 76 } 77 } 78 79 // --- MOVE CACTI --- 80 cactusX1 -= gameSpeed; 81 cactusX2 -= gameSpeed; 82 83 if (cactusX1 < -10) { 84 cactusX1 = 128 + random(0, 40); 85 if (random(0, 10) > 6) cactusX2 = cactusX1 + random(15, 30); 86 } 87 if (cactusX2 < -10) { 88 cactusX2 = 128 + random(30, 60); 89 } 90 91 // --- SPEED UP --- 92 if (currentTime - lastSpeedIncrease > 3000) { 93 gameSpeed++; 94 lastSpeedIncrease = currentTime; 95 } 96 97 // --- SCORE --- 98 score++; 99 100 // --- DRAW SCENE --- 101 display.clearDisplay(); 102 display.fillRect(10, dinoY, 10, 10, SSD1306_WHITE); // Dino 103 display.fillRect(cactusX1, groundY, 10, 15, SSD1306_WHITE); // Cactus 1 104 display.fillRect(cactusX2, groundY, 10, 15, SSD1306_WHITE); // Cactus 2 105 display.drawLine(0, 58, 128, 58, SSD1306_WHITE); // Ground 106 display.setTextSize(1); 107 display.setTextColor(SSD1306_WHITE); 108 display.setCursor(0, 0); 109 display.print("Score: "); 110 display.print(score / 10); 111 display.display(); 112 113 // --- COLLISION DETECTION --- 114 bool hitCactus1 = (cactusX1 < 20 && cactusX1 > 5 && dinoY == groundY); 115 bool hitCactus2 = (cactusX2 < 20 && cactusX2 > 5 && dinoY == groundY); 116 117 if (hitCactus1 || hitCactus2) { 118 gameOver = true; 119 display.clearDisplay(); 120 display.setTextSize(2); 121 display.setCursor(20, 20); 122 display.print("Game Over"); 123 display.setTextSize(1); 124 display.setCursor(10, 45); 125 display.print("Press to restart"); 126 display.display(); 127 } 128} 129 130// --- RESTART GAME FUNCTION --- 131void resetGame() { 132 isJumping = false; 133 dinoY = groundY; 134 velocity = 0; 135 cactusX1 = 128; 136 cactusX2 = 180; 137 gameSpeed = 3; 138 score = 0; 139 gameOver = false; 140 lastSpeedIncrease = millis(); 141 lastFrame = millis(); 142 display.clearDisplay(); 143 display.display(); 144}
Comments
Only logged in users can leave comments