GAME - Pong

This is a simple Pong game designed using an OLED display and an Arduino board. In the Pong game, players compete against the computer, controlling a vertical paddle to bounce back a bouncing ball. The goal is to prevent the ball from passing your paddle’s edge, or else the opponent scores.

The game mechanics can be divided into the following parts:

  1. Ball Movement - The ball moves along its current direction at a set speed. Whenever the ball collides with a paddle, its speed increases, making the game more challenging.

  2. Paddle Movement - Used to block the ball’s movement, the paddle can move up or down. Players control their own paddle using buttons, while the computer’s paddle automatically follows the ball’s position.

  3. Scoring - Whenever the ball goes beyond the left or right edge of the screen, the corresponding player or CPU scores.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Elite Explorer Kit

300+

Elite Explorer Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

Arduino Uno R4 WiFi

-

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

OLED Display Module

BUY

Button

BUY

Power Supply Module

-

Wiring

Note

To protect the Power Supply Module’s battery, please fully charge it before using it for the first time.

../_images/12_pong_bb.png

Schematic

../_images/12_pong_schematic.png

Code

Note

  • You can open the file 12_pong_oled.ino under the path of elite-explorer-kit-main\fun_project\12_pong_oled directly.

  • Or copy this code into Arduino IDE.

Note

To install the library, use the Arduino Library Manager and search for “Adafruit SSD1306” and “Adafruit GFX” and install them.

How it works?

The program structure can be divided into the following five parts:

  1. Import Necessary Libraries - Used to control the OLED screen and read button inputs.

  2. Define Constants and Global Variables:

    Definitions for OLED screen width and height. Definitions for buttons and OLED reset pins. Position, speed, size, and direction of the ball and paddles. Scores for player and CPU.

  3. Initialization:

    Initialize serial communication, the OLED screen, and display the initial interface. Set buttons as inputs and connect pull-up resistors. Draw the playing field.

  4. Main Loop:

    Read button states. Move the ball based on the set refresh rate. Detect collisions between the ball and paddles or walls, adjusting the ball’s direction and speed accordingly. Update the screen with scores based on scoring events. Refresh paddle positions.

  5. Additional Functions:

    crossesPlayerPaddle and crossesCpuPaddle - Used to detect whether the ball collides with the player’s or CPU’s paddle.

    drawCourt - Draws the playing field on the OLED screen.

    displayScore - Displays the player’s and CPU’s scores on the screen.