#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int joyXPin = A0; // Joystick X-axis pin
const int joyYPin = A1; // Joystick Y-axis pin
const int characterWidth = 8; // Character width in pixels
const int characterHeight = 8; // Character height in pixels
const int obstacleWidth = 8; // Obstacle width in pixels
const int obstacleHeight = 8; // Obstacle height in pixels
int characterX = SCREEN_WIDTH / 2; // Initial character X position
int characterY = SCREEN_HEIGHT - characterHeight; // Character Y position
int obstacleX[5]; // Array to store X position of obstacles
int obstacleY[5]; // Array to store Y position of obstacles
int obstacleSpeed = 1; // Initial obstacle speed
int numObstacles = 1; // Initial number of obstacles
int level = 1; // Initial level
void setup() {
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Clear the buffer
display.clearDisplay();
// Set text size, color, and position
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Avoid the obstacles!");
// Display text on OLED
display.display();
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Read joystick values
int joyXValue = analogRead(joyXPin);
// Map joystick value to character position
characterX = map(joyXValue, 0, 1023, 0, SCREEN_WIDTH - characterWidth);
// Clear the display buffer
display.clearDisplay();
// Draw character
display.fillRect(characterX, characterY, characterWidth, characterHeight, SSD1306_WHITE);
// Move obstacles
moveObstacles();
// Draw obstacles
drawObstacles();
// Display buffer on OLED
display.display();
// Check for collision
if (checkCollision()) {
display.clearDisplay();
display.setCursor(0,0);
display.println("Game Over!");
display.display();
while(true); // Stop the game
}
// Delay for smooth animation
delay(50);
}
void moveObstacles() {
for (int i = 0; i < numObstacles; i++) {
// Move obstacle
obstacleY[i] += obstacleSpeed;
// If obstacle reaches bottom, reset its position
if (obstacleY[i] >= SCREEN_HEIGHT) {
obstacleX[i] = random(0, SCREEN_WIDTH - obstacleWidth);
obstacleY[i] = 0 - obstacleHeight;
}
}
// Increase speed and number of obstacles every 10 seconds (adjust as needed)
if (millis() % 5000 == 0) {
level++;
obstacleSpeed++;
if (level <= 5) {
numObstacles++;
}
}
}
void drawObstacles() {
for (int i = 0; i < numObstacles; i++) {
// Draw obstacle
display.fillRect(obstacleX[i], obstacleY[i], obstacleWidth, obstacleHeight, SSD1306_WHITE);
}
}
bool checkCollision() {
// Check if character overlaps with any obstacle
for (int i = 0; i < numObstacles; i++) {
if (characterX + characterWidth >= obstacleX[i] &&
characterX <= obstacleX[i] + obstacleWidth &&
characterY + characterHeight >= obstacleY[i] &&
characterY <= obstacleY[i] + obstacleHeight) {
return true; // Collision detected
}
}
return false; // No collision
}