// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// #define SCREEN_WIDTH 128
// #define SCREEN_HEIGHT 64
// #define OLED_RESET -1
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// #define PADDLE_WIDTH 20
// #define PADDLE_HEIGHT 4
// #define BALL_SIZE 3
// int paddleX = (SCREEN_WIDTH - PADDLE_WIDTH) / 2;
// int paddleY = SCREEN_HEIGHT - 10;
// int ballX = SCREEN_WIDTH / 2;
// int ballY = SCREEN_HEIGHT / 2;
// int ballDirX = 1;
// int ballDirY = 1;
// int joystickX = A0;
// int buttonPin = 2;
// void setup() {
// pinMode(joystickX, INPUT);
// pinMode(buttonPin, INPUT_PULLUP);
// display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// display.clearDisplay();
// display.display();
// }
// void loop() {
// // Read joystick position
// int xValue = analogRead(joystickX);
// paddleX = map(xValue, 0, 1023, 0, SCREEN_WIDTH - PADDLE_WIDTH);
// // Read button state
// if (digitalRead(buttonPin) == LOW) {
// // Reset the game if the button is pressed
// ballX = SCREEN_WIDTH / 2;
// ballY = SCREEN_HEIGHT / 2;
// ballDirX = 1;
// ballDirY = 1;
// }
// // Update ball position
// ballX += ballDirX;
// ballY += ballDirY;
// // Ball collision with walls
// if (ballX <= 0 || ballX >= SCREEN_WIDTH - BALL_SIZE) {
// ballDirX = -ballDirX;
// }
// if (ballY <= 0) {
// ballDirY = -ballDirY;
// }
// // Ball collision with paddle
// if (ballY >= paddleY - BALL_SIZE && ballX >= paddleX && ballX <= paddleX + PADDLE_WIDTH) {
// ballDirY = -ballDirY;
// }
// // Ball missed the paddle
// if (ballY > SCREEN_HEIGHT) {
// ballX = SCREEN_WIDTH / 2;
// ballY = SCREEN_HEIGHT / 2;
// ballDirY = -ballDirY;
// }
// // Draw paddle, ball, and clear the previous frame
// display.clearDisplay();
// display.fillRect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT, SSD1306_WHITE);
// display.fillRect(ballX, ballY, BALL_SIZE, BALL_SIZE, SSD1306_WHITE);
// display.display();
// // Small delay for smoother gameplay
// delay(10);
// }
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// #include <EEPROM.h>
// #define SCREEN_WIDTH 128
// #define SCREEN_HEIGHT 64
// #define OLED_RESET -1
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// // Paddle and Ball properties
// int paddleX = 60;
// int paddleY = 56;
// int paddleWidth = 20;
// int paddleHeight = 4;
// int ballX = 64;
// int ballY = 32;
// int ballSpeedX = 2;
// int ballSpeedY = 2;
// int ballSize = 2;
// int score = 0;
// int highScore = 0;
// enum GameState { START, PLAY, END };
// GameState gameState = START;
// void setup() {
// pinMode(A0, INPUT); // Joystick X-axis
// display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// display.clearDisplay();
// // Load high score from EEPROM
// highScore = EEPROM.read(0);
// }
// void loop() {
// switch (gameState) {
// case START:
// showStartScreen();
// if (analogRead(A0) > 512) { // Start game when joystick is moved
// score = 0;
// ballX = 64;
// ballY = 32;
// ballSpeedX = 2;
// ballSpeedY = 2;
// gameState = PLAY;
// }
// break;
// case PLAY:
// playGame();
// break;
// case END:
// showEndScreen();
// if (analogRead(A0) > 512) { // Restart game when joystick is moved
// gameState = START;
// }
// break;
// }
// }
// void playGame() {
// // Read Joystick
// int joystickX = analogRead(A0);
// paddleX = map(joystickX, 0, 1023, 0, SCREEN_WIDTH - paddleWidth);
// // Ball Movement
// ballX += ballSpeedX;
// ballY += ballSpeedY;
// // Collision with walls
// if (ballX <= 0 || ballX >= SCREEN_WIDTH - ballSize) ballSpeedX = -ballSpeedX;
// if (ballY <= 0) ballSpeedY = -ballSpeedY;
// // Collision with paddle
// if (ballY + ballSize >= paddleY && ballX + ballSize >= paddleX && ballX <= paddleX + paddleWidth) {
// ballSpeedY = -ballSpeedY;
// score++;
// }
// // Ball out of bounds (Game Over)
// if (ballY > SCREEN_HEIGHT) {
// if (score > highScore) {
// highScore = score;
// EEPROM.write(0, highScore); // Save high score to EEPROM
// }
// gameState = END;
// }
// // Display game elements
// display.clearDisplay();
// display.fillRect(paddleX, paddleY, paddleWidth, paddleHeight, SSD1306_WHITE);
// display.fillRect(ballX, ballY, ballSize, ballSize, SSD1306_WHITE);
// display.setCursor(0, 0);
// display.print("Score: ");
// display.print(score);
// display.print(" High: ");
// display.print(highScore);
// display.display();
// delay(30); // Adjust for game speed
// }
// void showStartScreen() {
// display.clearDisplay();
// display.setTextSize(1);
// display.setCursor(20, 20);
// display.print("PONG GAME");
// display.setCursor(15, 40);
// display.print("Move joystick to start");
// display.display();
// }
// void showEndScreen() {
// display.clearDisplay();
// display.setTextSize(1);
// display.setCursor(20, 20);
// display.print("GAME OVER");
// display.setCursor(20, 35);
// display.print("Score: ");
// display.print(score);
// display.setCursor(20, 45);
// display.print("High Score: ");
// display.print(highScore);
// display.setCursor(15, 55);
// display.print("Move joystick to restart");
// display.display();
// }
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // This reset pin might not be needed on your display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Game parameters
int paddleX = 60;
int paddleY = 56;
const int paddleWidth = 20;
const int paddleHeight = 4;
int ballX = 64;
int ballY = 32;
int ballSpeedX = 2;
int ballSpeedY = 2;
const int ballSize = 2;
int score = 0;
int highScore = 0;
enum GameState { START, PLAY, END };
GameState gameState = START;
void setup() {
pinMode(A0, INPUT); // Joystick X-axis
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// Load high score from EEPROM
highScore = EEPROM.read(0);
}
void loop() {
switch (gameState) {
case START:
showStartScreen();
if (analogRead(A0) > 600 || analogRead(A0) < 400) { // Start game when joystick is moved
resetGame();
gameState = PLAY;
}
break;
case PLAY:
playGame();
break;
case END:
showEndScreen();
if (analogRead(A0) > 600 || analogRead(A0) < 400) { // Restart game when joystick is moved
gameState = START;
}
break;
}
}
void playGame() {
// Read Joystick
int joystickX = analogRead(A0);
paddleX = map(joystickX, 0, 1023, 0, SCREEN_WIDTH - paddleWidth);
// Ball Movement
ballX += ballSpeedX;
ballY += ballSpeedY;
// Collision with walls
if (ballX <= 0 || ballX >= SCREEN_WIDTH - ballSize) ballSpeedX = -ballSpeedX;
if (ballY <= 0) ballSpeedY = -ballSpeedY;
// Collision with paddle
if (ballY + ballSize >= paddleY && ballX + ballSize >= paddleX && ballX <= paddleX + paddleWidth) {
ballSpeedY = -ballSpeedY;
score++;
}
// Ball out of bounds (Game Over)
if (ballY > SCREEN_HEIGHT) {
if (score > highScore) {
highScore = score;
EEPROM.write(0, highScore); // Save high score to EEPROM
}
gameState = END;
}
// Display game elements
display.clearDisplay();
display.fillRect(paddleX, paddleY, paddleWidth, paddleHeight, SSD1306_WHITE);
display.fillRect(ballX, ballY, ballSize, ballSize, SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Score: ");
display.print(score);
display.setCursor(80, 0);
display.print("High: ");
display.print(highScore);
display.display();
delay(30); // Adjust for game speed
}
void resetGame() {
score = 0;
ballX = 64;
ballY = 32;
ballSpeedX = 2;
ballSpeedY = 2;
}
void showStartScreen() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(20, 20);
display.print("PONG GAME");
display.setCursor(15, 40);
display.print("Move joystick to start");
display.display();
}
void showEndScreen() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(20, 20);
display.print("GAME OVER");
display.setCursor(20, 35);
display.print("Score: ");
display.print(score);
display.setCursor(20, 45);
display.print("High Score: ");
display.print(highScore);
display.setCursor(15, 55);
display.print("Move joystick to restart");
display.display();
}
Loading
ssd1306
ssd1306