Project tutorial
Ponguino Arduino Pong Game

Ponguino Arduino Pong Game

Build your own Pong game with an Arduino Uno.

  • 25 views
  • 0 comments
  • 0 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)
for soldering the pins on th sceen

Apps and online services

About this project

1. Introduction

This is a pong game made with an Arduino Uno and a SparkFun Micro OLED screen. It is fun to play and even has sound efects.

2. Materials

These are the materials needed:

3. Wiring

Screen:

Warning: Never connect this screen to 5V Connect to 3.3V.

Controls:

Code

Untitled fileC/C++
#include <Wire.h>  // Include Wire if you're using I2C
#include <SPI.h>  // Include SPI if you're using SPI
#include <SFE_MicroOLED.h>  // Include the SFE_MicroOLED library

//////////////////////////
// MicroOLED Definition //
//////////////////////////
#define PIN_RESET 9  // Connect RST to pin 9
#define PIN_DC    8  // Connect DC to pin 8
#define PIN_CS    10 // Connect CS to pin 10
#define DC_JUMPER 0

//////////////////////////////////
// MicroOLED Object Declaration //
//////////////////////////////////
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI declaration
//MicroOLED oled(PIN_RESET, DC_JUMPER);    // I2C declaration
int a= 0;
int b=0;
void setup(){
   digitalWrite(7, HIGH);
  delay(200);
  digitalWrite(7, LOW);
  delay(100);
   digitalWrite(7, HIGH);
  delay(200);
  digitalWrite(7, LOW);
  oled.clear(PAGE);
  oled.begin();    // Initialize the OLED
  oled.clear(ALL); // Clear the display's internal memory
  oled.setFontType(1);
  oled.setCursor(0, 0);
  oled.print("PONG :)");
  oled.display();  // Display what's in the buffer (splashscreen)
  delay(1000);     // Delay 1000 ms
  oled.clear(PAGE); // Clear the buffer.
  
  randomSeed(analogRead(A0) + analogRead(A1));
  pinMode(7,OUTPUT);
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
}


void loop() {
  // put your main code here, to run repeatedly:
  int paddleW = 3;  // Paddle width
  int paddleH = 15;  // Paddle height
  // Paddle 0 (left) position coordinates
  int paddle0_Y = (oled.getLCDHeight() / 2) - (paddleH / 2);
  int paddle0_X = 2;
  // Paddle 1 (right) position coordinates
  int paddle1_Y = (oled.getLCDHeight() / 2) - (paddleH / 2);
  int paddle1_X = oled.getLCDWidth() - 3 - paddleW;
  int ball_rad = 2;  // Ball radius
  // Ball position coordinates
  int ball_X = paddle0_X + paddleW + ball_rad;
  int ball_Y = random(1 + ball_rad, oled.getLCDHeight() - ball_rad);//paddle0_Y + ball_rad;
  int ballVelocityX = 1;  // Ball left/right velocity
  int ballVelocityY = 1;  // Ball up/down velocity
  int paddle0Velocity = -1;  // Paddle 0 velocity
  int paddle1Velocity = 1;  // Paddle 1 velocity
    
  //while(ball_X >= paddle0_X + paddleW - 1)
  while ((ball_X - ball_rad > 1) && 
         (ball_X + ball_rad < oled.getLCDWidth() - 2))
  {
    int aa= digitalRead(2);
    int ab= digitalRead(3);
    int ba= digitalRead(4);
    int bb= digitalRead(5);
    // Increment ball's position
    ball_X+=ballVelocityX;
    ball_Y+=ballVelocityY;
    // Check if the ball is colliding with the left paddle
    if(aa == HIGH){
      paddle1_Y++;
      paddle1_Y++;
    }
    if(ab == HIGH){
      paddle1_Y--;
       paddle1_Y--;
    }
    if(ba == HIGH){
      paddle0_Y++;
       paddle0_Y++;
    }
    if(bb == HIGH){
      paddle0_Y--;
       paddle0_Y--;
    }
    if (ball_X - ball_rad < paddle0_X + paddleW)
    {
      // Check if ball is within paddle's height
      if ((ball_Y > paddle0_Y) && (ball_Y < paddle0_Y + paddleH))
      {
        ball_X++;  // Move ball over one to the right
        ballVelocityX = -ballVelocityX; // Change velocity
      }
    }
    // Check if the ball hit the right paddle
    if (ball_X + ball_rad > paddle1_X)
    {
      // Check if ball is within paddle's height
      if ((ball_Y > paddle1_Y) && (ball_Y < paddle1_Y + paddleH))
      {
        ball_X--;  // Move ball over one to the left
        ballVelocityX = -ballVelocityX; // change velocity
      }
    }
    // Check if the ball hit the top or bottom
    if ((ball_Y <= ball_rad) || (ball_Y >= (oled.getLCDHeight() - ball_rad - 1)))
    {
      // Change up/down velocity direction
      ballVelocityY = -ballVelocityY;
    }
    // Move the paddles up and down
    // Change paddle 0's direction if it hit top/bottom
    if ((paddle0_Y <= 1) || (paddle0_Y > oled.getLCDHeight() - 2 - paddleH))
    {
      paddle0Velocity = -paddle0Velocity;
    }
    // Change paddle 1's direction if it hit top/bottom
    if ((paddle1_Y <= 1) || (paddle1_Y > oled.getLCDHeight() - 2 - paddleH))
    {
      paddle1Velocity = -paddle1Velocity;
    }
    
    // Draw the Pong Field
    oled.clear(PAGE);  // Clear the page
    // Draw an outline of the screen:
    oled.rect(0, 0, oled.getLCDWidth() - 1, oled.getLCDHeight());
    // Draw the center line
    oled.rectFill(oled.getLCDWidth()/2 - 1, 0, 2, oled.getLCDHeight());
    // Draw the Paddles:
    oled.rectFill(paddle0_X, paddle0_Y, paddleW, paddleH);
    oled.rectFill(paddle1_X, paddle1_Y, paddleW, paddleH);
    // Draw the ball:
    oled.circle(ball_X, ball_Y, ball_rad);

    oled.setFontType(1);
  oled.setCursor(20, 0);
  oled.print(b);
  oled.print("-");
  oled.print(a);
    // Actually draw everything on the screen:
    oled.display();
    delay(25);  // Delay for visibility
  }
  digitalWrite(7, HIGH);
  delay(200);
  digitalWrite(7, LOW);
  delay(100);
   digitalWrite(7, HIGH);
  delay(200);
  digitalWrite(7, LOW);
  delay(1000);
  if(ball_X + ball_rad < oled.getLCDWidth() - 2){
    a++;
  }else{
    b++;
  }
}

Schematics

untitled_sketch_bb_tEVX0vEdyE.png
Untitled sketch bb 5fbk8xsvax

Comments

Similar projects you might like

Arduino Pocket Game Console + A-Maze - Maze Game

Project tutorial by Alojz Jakob

  • 7,644 views
  • 8 comments
  • 22 respects

Arduino Game By LCD

Project tutorial by Mohammed Magdy

  • 45,411 views
  • 47 comments
  • 152 respects

Arduino Pong Game - OLED Display

Project tutorial by Nick Koumaris

  • 7,023 views
  • 1 comment
  • 13 respects

Arduino Pong Game on 24x16 Matrix with MAX7219

Project tutorial by Mirko Pavleski

  • 2,173 views
  • 3 comments
  • 17 respects

LCD Game

Project showcase by Team Iron_SalsaStudio

  • 10,021 views
  • 9 comments
  • 18 respects

Ninja Dollar - LCD game

Project tutorial by anime

  • 3,988 views
  • 6 comments
  • 7 respects
Add projectSign up / Login