Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Start streaming on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!
5vdc
Published

Ponguino Arduino Pong Game

Build your own Pong game with an Arduino Uno.

IntermediateFull instructions provided4,590
Ponguino Arduino Pong Game

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
sparkfun micro oled breakout
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

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

Story

Read more

Schematics

untitled_sketch_bb_tEVX0vEdyE.png

Code

Untitled file

C/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++;
  }
}

Credits

5vdc

5vdc

1 project • 1 follower

Comments