#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);
int ballX = SCREEN_WIDTH / 2;
int ballY = SCREEN_HEIGHT / 2;
int ballSpeedX = 1;
int ballSpeedY = 1;
int playerY = SCREEN_HEIGHT / 2 - 10;
int playerSpeed = 2;
int score = 0;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Football Game");
display.display();
delay(2000);
}
void loop() {
display.clearDisplay();
// Move the player up and down
if (digitalRead(2) == HIGH && playerY > 0) {
playerY -= playerSpeed;
}
if (digitalRead(3) == HIGH && playerY < SCREEN_HEIGHT - 20) {
playerY += playerSpeed;
}
// Move the ball
ballX += ballSpeedX;
ballY += ballSpeedY;
// Check ball collision with top and bottom walls
if (ballY <= 0 || ballY >= SCREEN_HEIGHT - 4) {
ballSpeedY = -ballSpeedY;
}
// Check ball collision with player
if (ballX <= 5 && ballY >= playerY && ballY <= playerY + 20) {
ballSpeedX = -ballSpeedX;
score++;
}
// Check ball out of bounds
if (ballX >= SCREEN_WIDTH) {
ballX = SCREEN_WIDTH / 2;
ballY = SCREEN_HEIGHT / 2;
ballSpeedX = -ballSpeedX;
score = 0;
}
// Draw player
display.fillRect(0, playerY, 5, 20, SSD1306_WHITE);
// Draw ball
display.fillCircle(ballX, ballY, 4, SSD1306_WHITE);
// Display score
display.setCursor(SCREEN_WIDTH - 30, 0);
display.print("Score: ");
display.println(score);
display.display();
delay(10);
}