#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int buttonPin = 15;
int score = 0;
int dogY = 50; // Dog's vertical position
int velocity = 0; // Jump velocity
bool isJumping = false;
int obstacleX = 128; // Obstacle starting position
int gameSpeed = 4;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for(;;);
}
display.clearDisplay();
}
void loop() {
display.clearDisplay();
// 1. Handle Input (Jump)
if (digitalRead(buttonPin) == LOW && !isJumping) {
velocity = -8; // Upward force
isJumping = true;
}
// 2. Physics (Gravity)
dogY += velocity;
if (dogY < 50) {
velocity += 1; // Falling down
} else {
dogY = 50;
velocity = 0;
isJumping = false;
}
// 3. Move Obstacle
obstacleX -= gameSpeed;
if (obstacleX < -10) {
obstacleX = 128;
score++;
if(score % 5 == 0) gameSpeed++; // Speed up every 5 points
}
// 4. Collision Detection
// If dog is low enough and obstacle is at the dog's X position
if (obstacleX < 25 && obstacleX > 5 && dogY > 40) {
gameOver();
}
// 5. Draw Everything
// Draw "Dog" (a simple rectangle)
display.fillRect(10, dogY, 15, 10, WHITE);
// Draw Obstacle
display.fillRect(obstacleX, 50, 10, 10, WHITE);
// Draw Score
display.setCursor(0,0);
display.setTextColor(WHITE);
display.print("Score: ");
display.print(score);
display.display();
delay(30);
}
void gameOver() {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(10, 20);
display.print("GAME OVER");
display.setTextSize(1);
display.setCursor(10, 45);
display.print("Final Score: ");
display.print(score);
display.display();
delay(2000);
// Reset Game
score = 0;
obstacleX = 128;
gameSpeed = 4;
}