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

**URL:** <https://forum.arduino.cc/t/pong-game-with-arduino-due-and-oled-adafruit-display-with-serial-terminal-keyboard/1224164>\
**Category:** General Guidance\
**Created:** [February 15, 2024, 8:01am UTC](https://forum.arduino.cc/t/pong-game-with-arduino-due-and-oled-adafruit-display-with-serial-terminal-keyboard/1224164 "2024-02-15T08:01:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![toolkitman](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toolkitman/32/1039924_2.png) [@toolkitman](https://forum.arduino.cc/u/toolkitman)\
**Post date:** [February 15, 2024, 8:01am UTC](https://forum.arduino.cc/t/pong-game-with-arduino-due-and-oled-adafruit-display-with-serial-terminal-keyboard/1224164/1 "2024-02-15T08:01:36Z")

</div>

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.

```arduino
#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
}

```

---

<div class="post-metadata">

**Author:** ![toolkitman](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toolkitman/32/1039924_2.png) [@toolkitman](https://forum.arduino.cc/u/toolkitman)\
**Post date:** [February 15, 2024, 9:51am UTC](https://forum.arduino.cc/t/pong-game-with-arduino-due-and-oled-adafruit-display-with-serial-terminal-keyboard/1224164/2 "2024-02-15T09:51:51Z")

</div>

Pong game with score.

```arduino
#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
}

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [February 15, 2024, 2:53pm UTC](https://forum.arduino.cc/t/pong-game-with-arduino-due-and-oled-adafruit-display-with-serial-terminal-keyboard/1224164/3 "2024-02-15T14:53:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![toolkitman](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toolkitman/32/1039924_2.png) [@toolkitman](https://forum.arduino.cc/u/toolkitman)\
**Post date:** [February 16, 2024, 6:47am UTC](https://forum.arduino.cc/t/pong-game-with-arduino-due-and-oled-adafruit-display-with-serial-terminal-keyboard/1224164/4 "2024-02-16T06:47:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [February 16, 2024, 2:52pm UTC](https://forum.arduino.cc/t/pong-game-with-arduino-due-and-oled-adafruit-display-with-serial-terminal-keyboard/1224164/5 "2024-02-16T14:52:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 14, 2024, 2:52pm UTC](https://forum.arduino.cc/t/pong-game-with-arduino-due-and-oled-adafruit-display-with-serial-terminal-keyboard/1224164/6 "2024-08-14T14:52:29Z")

</div>

Closed on August 14, 2024, 2:52pm UTC

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