Play a PONG Game

by Silent_bower in Circuits > Arduino

52 Views, 0 Favorites, 0 Comments

Play a PONG Game

4ea3044f93674a5f67d070ff56b84bc.jpg
h.jpg
j.jpg
k.jpg
l.jpg

Recently, my partner and I succeeded in making a small game console. In this game console, you can push buttons to control the bezel up and down against the computer.

 

Okay, so let's get started.

 

Supplies

d.png

You need to prepare the following materials:

1x Arduino uno

2x push buttons

1x oled display 0.96 ssd1306 i2c1x

1x breadboard

Few jumpers

Connection

f.png
f.png

1.You need to connect the display to Arduino.

VCC - 5v

GND - GND pin

SDA of LED - A4 on Arduino

SCK of display - A5 on Arduino

(as shown in the picture.)


2.You need to connect the buttons,

One end of the buttons - GND

One end of UP button - pin 2 on Arduino

One end of DOWN button - pin 3 on Arduino

(as shown in the picture.)

Code

z.png
x.png

First, you need to check if the required libraries are installed. Here are some tips.(Download GFX,SSDZ,1306 Library)



This is our code:毕嘉帅:

/*
  A simple Pong game:
 */
 
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#define UP_BUTTON 2
#define DOWN_BUTTON 3
 
const unsigned long PADDLE_RATE = 25;
const unsigned long BALL_RATE = 12;
const uint8_t PADDLE_HEIGHT = 30;
 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
 
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET    4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
void drawCourt();
 
uint8_t ball_x = 64, ball_y = 32;
uint8_t ball_dir_x = 1, ball_dir_y = 1;
unsigned long ball_update;
 
unsigned long paddle_update;
const uint8_t CPU_X = 12;
uint8_t cpu_y = 16;
 
const uint8_t PLAYER_X = 115;
uint8_t player_y = 16;
 
void setup() {
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 
   
   display.display();
   unsigned long start = millis();
 
   pinMode(UP_BUTTON, INPUT);
   pinMode(DOWN_BUTTON, INPUT);
   digitalWrite(UP_BUTTON,1);
   digitalWrite(DOWN_BUTTON,1);
   display.clearDisplay();
   drawCourt();
 
   while(millis() - start < 2000);
 
   display.display();
 
   ball_update = millis();
   paddle_update = ball_update;
}
 
void loop() {
   bool update = false;
   unsigned long time = millis();
 
   static bool up_state = false;
   static bool down_state = false;
   
   up_state |= (digitalRead(UP_BUTTON) == LOW);
   down_state |= (digitalRead(DOWN_BUTTON) == LOW);
 
   if(time > ball_update) {
       uint8_t new_x = ball_x + ball_dir_x;
       uint8_t new_y = ball_y + ball_dir_y;
 
       // Check if we hit the vertical walls
       if(new_x == 0 || new_x == 127) {
           ball_dir_x = -ball_dir_x;
           new_x += ball_dir_x + ball_dir_x;
       }
 
       // Check if we hit the horizontal walls.
       if(new_y == 0 || new_y == 63) {
           ball_dir_y = -ball_

 



3-D Printing

v.jpg
z.jpg

If you want to make it beautiful,you can rely on 3-D printing. Measure the data and plan the model to print it.At last,assemble it.

Supplement

Once you've connected everything, just enjoy the game!

Here is a video showing the process and some highlights of our game.

【Play a PONG Game】 https://www.bilibili.com/video/BV1oD421M7gU/?share_source=copy_web&vd_source=7790ccc762ab02f933a0e33cd84a6490

We also provide free shell model download:

https://pan.baidu.com/s/1pDIZEDN6bGw_8n265eRGKQ    Code: 1sbk