This is a simple Pong Game (Player with CPU) for an Arduino Due and a Adafruit display oled connected to 3.3v vcc gnd to gnd and sda to pin 20 and sdl to pin 21 i2c. To have more fluid key pressing use a terminal like tera term in windows. So you dont need to press letter than return but only letter.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // Larghezza dello schermo OLED, in pixel
#define SCREEN_HEIGHT 64 // Altezza dello schermo OLED, in pixel
#define PADDLE_WIDTH 6 // Larghezza del paddle, in pixel
#define PADDLE_HEIGHT 20 // Altezza del paddle, in pixel
#define BALL_SIZE 3 // Dimensione della palla, in pixel
#define PADDLE_SPEED 1 // VelocitĂ di spostamento del paddle, in pixel
// Dichiarazione di un oggetto display OLED con l'indirizzo I2C predefinito (0x3C)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int paddlePositionY; // Posizione Y del paddle del giocatore
int computerPaddlePositionY; // Posizione Y del paddle del computer
int ballPositionX, ballPositionY; // Posizione della palla
int ballVelocityX, ballVelocityY; // VelocitĂ della palla
void setup() {
Serial.begin(9600); // Inizializza la comunicazione seriale
Wire.begin(); // Inizializza la comunicazione I2C
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Inizializza il display OLED
// Imposta le posizioni iniziali
paddlePositionY = (SCREEN_HEIGHT - PADDLE_HEIGHT) / 2;
computerPaddlePositionY = (SCREEN_HEIGHT - PADDLE_HEIGHT) / 2;
ballPositionX = SCREEN_WIDTH / 2;
ballPositionY = SCREEN_HEIGHT / 2;
// Imposta le velocitĂ iniziali della palla
randomSeed(analogRead(0));
ballVelocityX = random(1, 3);
ballVelocityY = random(1, 3);
}
void loop() {
// Leggi l'input del giocatore
if (Serial.available() > 0) {
char input = Serial.read();
if (input == 'w' && paddlePositionY > 0) {
paddlePositionY -= PADDLE_SPEED;
} else if (input == 's' && paddlePositionY < SCREEN_HEIGHT - PADDLE_HEIGHT) {
paddlePositionY += PADDLE_SPEED;
}
}
// Movimento del paddle del computer
int computerPaddleTargetY = ballPositionY - PADDLE_HEIGHT / 2;
if (computerPaddleTargetY > computerPaddlePositionY) {
computerPaddlePositionY += PADDLE_SPEED;
} else if (computerPaddleTargetY < computerPaddlePositionY) {
computerPaddlePositionY -= PADDLE_SPEED;
}
// Movimento della palla
ballPositionX += ballVelocityX;
ballPositionY += ballVelocityY;
// Collisione con il paddle del giocatore
if (ballPositionX <= PADDLE_WIDTH && ballPositionY >= paddlePositionY && ballPositionY <= paddlePositionY + PADDLE_HEIGHT) {
ballVelocityX = -ballVelocityX;
}
// Collisione con il paddle del computer
if (ballPositionX >= SCREEN_WIDTH - PADDLE_WIDTH && ballPositionY >= computerPaddlePositionY && ballPositionY <= computerPaddlePositionY + PADDLE_HEIGHT) {
ballVelocityX = -ballVelocityX;
}
// Collisione con i bordi superiori o inferiori
if (ballPositionY <= 0 || ballPositionY >= SCREEN_HEIGHT - BALL_SIZE) {
ballVelocityY = -ballVelocityY;
}
// Fine gioco se la palla esce dai bordi laterali
if (ballPositionX <= 0 || ballPositionX >= SCREEN_WIDTH - BALL_SIZE) {
ballPositionX = SCREEN_WIDTH / 2;
ballPositionY = SCREEN_HEIGHT / 2;
ballVelocityX = random(1, 3);
ballVelocityY = random(1, 3);
}
// Disegna lo schermo di gioco
display.clearDisplay(); // Cancella il display
// Disegna i paddles
display.fillRect(0, paddlePositionY, PADDLE_WIDTH, PADDLE_HEIGHT, SSD1306_WHITE); // Paddle del giocatore
display.fillRect(SCREEN_WIDTH - PADDLE_WIDTH, computerPaddlePositionY, PADDLE_WIDTH, PADDLE_HEIGHT, SSD1306_WHITE); // Paddle del computer
// Disegna la palla
display.fillRect(ballPositionX, ballPositionY, BALL_SIZE, BALL_SIZE, SSD1306_WHITE);
display.display(); // Aggiorna il display per visualizzare le modifiche
}
There are a lot of tool to translate now a day use it if the italian language of Massimo Banzi and Arduino give you noise! The author is the author of the post it is so clear!