Giter VIP home page Giter VIP logo

arduinopong's Introduction

OLED Pong Game

Untitled.design.mp4

This project guides you through creating a Pong game using an Arduino microcontroller, an OLED display, and two potentiometers for paddle control. This classic game pits two players against each other, each controlling a paddle to hit a ball back and forth. Points are scored when one player fails to return the ball, and the game ends when a player reaches the maximum points.

Components Required

  • Arduino Uno (or any compatible microcontroller)
  • OLED display (128x64 pixels, I2C communication)
  • 2x Potentiometers (for player controls)
  • Breadboard and jumper wires
  • Optional: Resistors for potentiometers (depending on your setup)

Wiring the Components

  1. Connect the OLED display to the Arduino's I2C pins:
    • SDA to A4 (on Uno) or the SDA pin of your microcontroller
    • SCL to A5 (on Uno) or the SCL pin of your microcontroller
  2. Connect the OLED's VCC and GND to the Arduino's 5V and GND, respectively.
  3. Connect each potentiometer's outer pins to 5V and GND for power.
  4. Connect the middle pin of the potentiometers to A0 and A1 on the Arduino for player controls.

Libraries Needed

Before compiling the code, ensure you have the following libraries installed in your Arduino IDE:

  • Adafruit_GFX.h
  • Adafruit_SSD1306.h

These libraries provide the necessary functions to control the OLED display.

The Code

The provided code initializes the display and runs the main game loop, handling:

  • Drawing the paddles and ball
  • Detecting collisions and updating scores
  • Ending the game when a player reaches the maximum points

Key Variables and Defines

  • SCREEN_WIDTH and SCREEN_HEIGHT set the dimensions of the OLED display.
  • OLED_RESET, OLED_I2C_ADDRESS for display initialization.
  • Game mechanics variables such as PLAYER_PADDLE_HEIGHT, BALL_RADIUS, GAMESPEED, etc.

Setup and Loop

The setup() function initializes serial communication and the display. The loop() continuously runs the game, updating the display and game state.

Game Functions

  • playGame(): Checks if the game should continue or display the end game message.
  • updateGame(): Reads players' paddle positions and updates the game state.
  • drawPaddlesAndBall(): Draws the paddles and ball on the display.
  • updateScoreAndPosition(): Updates the scores and positions of the paddles and ball.
  • displayScore(): Displays the current score.
  • displayEndGame(): Displays the end game message with the winner.

Compiling and Running

After connecting your Arduino to your computer:

  1. Open the Arduino IDE.
  2. Select your Arduino model and port.
  3. Compile and upload the code.

Enjoy your OLED Pong game! Players use the potentiometers to move their paddles up and down, trying to hit the ball back to the other player. The first to reach the maximum points wins the game.

Customization

Feel free to tweak the game settings such as GAMESPEED, MAX_POINTS, or paddle and ball sizes to customize your gameplay experience.

arduinopong's People

Contributors

s5y-ux avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.