Pong game with Arduino Due and oled adafruit display with serial terminal keyboard

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
}

Pong game with score.

#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
int playerScore, computerScore; // Punteggio del giocatore e del computer

void resetBall() {
  ballPositionX = SCREEN_WIDTH / 2;
  ballPositionY = SCREEN_HEIGHT / 2;
  ballVelocityX = random(1, 3);
  ballVelocityY = random(1, 3);
}

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;
  resetBall();
  playerScore = 0;
  computerScore = 0;
}

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 i paddles
  if (ballPositionX <= PADDLE_WIDTH && ballPositionY >= paddlePositionY && ballPositionY <= paddlePositionY + PADDLE_HEIGHT) {
    ballVelocityX = -ballVelocityX;
  }
  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;
  }
  
  // Incrementa il punteggio del computer se la palla esce dai bordi sinistri
  if (ballPositionX <= 0) {
    computerScore++;
    resetBall();
  }
  
  // Incrementa il punteggio del giocatore se la palla esce dai bordi destri
  if (ballPositionX >= SCREEN_WIDTH - BALL_SIZE) {
    playerScore++;
    resetBall();
  }
  
  // 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);
  // Disegna il punteggio
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(SCREEN_WIDTH / 2 - 20, 5);
  display.print(playerScore);
  display.print(" - ");
  display.print(computerScore);
  display.display(); // Aggiorna il display per visualizzare le modifiche
  delay(10); // Aggiorna ogni 10 millisecondi
}

Why do you code in English but comment in Italian? Is there no word for "ball" or "player?"
If you are copy/pasting, why no author acknowledgement?

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!

Do you need help learning how to use the language translator?

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.