Components and supplies
1
Jumper wires (generic)
1
small buttons
1
0.96 OLED display
1
Arduino UNO
Apps and platforms
1
Arduino IDE
Project description
Code
pong game for oled display
h
1 /* 2 A simple Pong game: 3 */ 4 5#include <SPI.h> 6#include <Wire.h> 7#include <Adafruit_GFX.h> 8#include <Adafruit_SSD1306.h> 9 10#define UP_BUTTON 2 11#define DOWN_BUTTON 3 12 13const unsigned long PADDLE_RATE = 33; 14const unsigned long BALL_RATE = 16; 15const uint8_t PADDLE_HEIGHT = 24; 16 17#define SCREEN_WIDTH 128 // OLED display width, in pixels 18#define SCREEN_HEIGHT 64 // OLED display height, in pixels 19 20// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 21#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) 22Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 23 24void drawCourt(); 25 26uint8_t ball_x = 64, ball_y = 32; 27uint8_t ball_dir_x = 1, ball_dir_y = 1; 28unsigned long ball_update; 29 30unsigned long paddle_update; 31const uint8_t CPU_X = 12; 32uint8_t cpu_y = 16; 33 34const uint8_t PLAYER_X = 115; 35uint8_t player_y = 16; 36 37void setup() { 38 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 39 40 41 display.display(); 42 unsigned long start = millis(); 43 44 pinMode(UP_BUTTON, INPUT); 45 pinMode(DOWN_BUTTON, INPUT); 46 digitalWrite(UP_BUTTON,1); 47 digitalWrite(DOWN_BUTTON,1); 48 display.clearDisplay(); 49 drawCourt(); 50 51 while(millis() - start < 2000); 52 53 display.display(); 54 55 ball_update = millis(); 56 paddle_update = ball_update; 57} 58 59void loop() { 60 bool update = false; 61 unsigned long time = millis(); 62 63 static bool up_state = false; 64 static bool down_state = false; 65 66 up_state |= (digitalRead(UP_BUTTON) == LOW); 67 down_state |= (digitalRead(DOWN_BUTTON) == LOW); 68 69 if(time > ball_update) { 70 uint8_t new_x = ball_x + ball_dir_x; 71 uint8_t new_y = ball_y + ball_dir_y; 72 73 // Check if we hit the vertical walls 74 if(new_x == 0 || new_x == 127) { 75 ball_dir_x = -ball_dir_x; 76 new_x += ball_dir_x + ball_dir_x; 77 } 78 79 // Check if we hit the horizontal walls. 80 if(new_y == 0 || new_y == 63) { 81 ball_dir_y = -ball_dir_y; 82 new_y += ball_dir_y + ball_dir_y; 83 } 84 85 // Check if we hit the CPU paddle 86 if(new_x == CPU_X && new_y >= cpu_y && new_y <= cpu_y + PADDLE_HEIGHT) { 87 ball_dir_x = -ball_dir_x; 88 new_x += ball_dir_x + ball_dir_x; 89 } 90 91 // Check if we hit the player paddle 92 if(new_x == PLAYER_X 93 && new_y >= player_y 94 && new_y <= player_y + PADDLE_HEIGHT) 95 { 96 ball_dir_x = -ball_dir_x; 97 new_x += ball_dir_x + ball_dir_x; 98 } 99 100 display.drawPixel(ball_x, ball_y, BLACK); 101 display.drawPixel(new_x, new_y, WHITE); 102 ball_x = new_x; 103 ball_y = new_y; 104 105 ball_update += BALL_RATE; 106 107 update = true; 108 } 109 110 if(time > paddle_update) { 111 paddle_update += PADDLE_RATE; 112 113 // CPU paddle 114 display.drawFastVLine(CPU_X, cpu_y, PADDLE_HEIGHT, BLACK); 115 const uint8_t half_paddle = PADDLE_HEIGHT >> 1; 116 if(cpu_y + half_paddle > ball_y) { 117 cpu_y -= 1; 118 } 119 if(cpu_y + half_paddle < ball_y) { 120 cpu_y += 1; 121 } 122 if(cpu_y < 1) cpu_y = 1; 123 if(cpu_y + PADDLE_HEIGHT > 63) cpu_y = 63 - PADDLE_HEIGHT; 124 display.drawFastVLine(CPU_X, cpu_y, PADDLE_HEIGHT, WHITE); 125 126 // Player paddle 127 display.drawFastVLine(PLAYER_X, player_y, PADDLE_HEIGHT, BLACK); 128 if(up_state) { 129 player_y -= 1; 130 } 131 if(down_state) { 132 player_y += 1; 133 } 134 up_state = down_state = false; 135 if(player_y < 1) player_y = 1; 136 if(player_y + PADDLE_HEIGHT > 63) player_y = 63 - PADDLE_HEIGHT; 137 display.drawFastVLine(PLAYER_X, player_y, PADDLE_HEIGHT, WHITE); 138 139 update = true; 140 } 141 142 if(update) 143 display.display(); 144} 145 146 147void drawCourt() { 148 display.drawRect(0, 0, 128, 64, WHITE); 149}
pong game for oled display
h
1 /* 2 A simple Pong game: 3 */ 4 5#include <SPI.h> 6#include 7 <Wire.h> 8#include <Adafruit_GFX.h> 9#include <Adafruit_SSD1306.h> 10 11#define 12 UP_BUTTON 2 13#define DOWN_BUTTON 3 14 15const unsigned long PADDLE_RATE = 33; 16const 17 unsigned long BALL_RATE = 16; 18const uint8_t PADDLE_HEIGHT = 24; 19 20#define 21 SCREEN_WIDTH 128 // OLED display width, in pixels 22#define SCREEN_HEIGHT 64 // 23 OLED display height, in pixels 24 25// Declaration for an SSD1306 display connected 26 to I2C (SDA, SCL pins) 27#define OLED_RESET 4 // Reset pin # (or -1 if sharing 28 Arduino reset pin) 29Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 30 OLED_RESET); 31 32void drawCourt(); 33 34uint8_t ball_x = 64, ball_y = 32; 35uint8_t 36 ball_dir_x = 1, ball_dir_y = 1; 37unsigned long ball_update; 38 39unsigned long 40 paddle_update; 41const uint8_t CPU_X = 12; 42uint8_t cpu_y = 16; 43 44const 45 uint8_t PLAYER_X = 115; 46uint8_t player_y = 16; 47 48void setup() { 49 display.begin(SSD1306_SWITCHCAPVCC, 50 0x3C); 51 52 53 display.display(); 54 unsigned long start = millis(); 55 56 57 pinMode(UP_BUTTON, INPUT); 58 pinMode(DOWN_BUTTON, INPUT); 59 digitalWrite(UP_BUTTON,1); 60 61 digitalWrite(DOWN_BUTTON,1); 62 display.clearDisplay(); 63 drawCourt(); 64 65 66 while(millis() - start < 2000); 67 68 display.display(); 69 70 ball_update 71 = millis(); 72 paddle_update = ball_update; 73} 74 75void loop() { 76 bool 77 update = false; 78 unsigned long time = millis(); 79 80 static bool up_state 81 = false; 82 static bool down_state = false; 83 84 up_state |= (digitalRead(UP_BUTTON) 85 == LOW); 86 down_state |= (digitalRead(DOWN_BUTTON) == LOW); 87 88 if(time 89 > ball_update) { 90 uint8_t new_x = ball_x + ball_dir_x; 91 uint8_t 92 new_y = ball_y + ball_dir_y; 93 94 // Check if we hit the vertical walls 95 96 if(new_x == 0 || new_x == 127) { 97 ball_dir_x = -ball_dir_x; 98 99 new_x += ball_dir_x + ball_dir_x; 100 } 101 102 // Check 103 if we hit the horizontal walls. 104 if(new_y == 0 || new_y == 63) { 105 ball_dir_y 106 = -ball_dir_y; 107 new_y += ball_dir_y + ball_dir_y; 108 } 109 110 111 // Check if we hit the CPU paddle 112 if(new_x == CPU_X && new_y 113 >= cpu_y && new_y <= cpu_y + PADDLE_HEIGHT) { 114 ball_dir_x = -ball_dir_x; 115 116 new_x += ball_dir_x + ball_dir_x; 117 } 118 119 // Check 120 if we hit the player paddle 121 if(new_x == PLAYER_X 122 && new_y 123 >= player_y 124 && new_y <= player_y + PADDLE_HEIGHT) 125 { 126 127 ball_dir_x = -ball_dir_x; 128 new_x += ball_dir_x + ball_dir_x; 129 130 } 131 132 display.drawPixel(ball_x, ball_y, BLACK); 133 display.drawPixel(new_x, 134 new_y, WHITE); 135 ball_x = new_x; 136 ball_y = new_y; 137 138 ball_update 139 += BALL_RATE; 140 141 update = true; 142 } 143 144 if(time > paddle_update) 145 { 146 paddle_update += PADDLE_RATE; 147 148 // CPU paddle 149 display.drawFastVLine(CPU_X, 150 cpu_y, PADDLE_HEIGHT, BLACK); 151 const uint8_t half_paddle = PADDLE_HEIGHT 152 >> 1; 153 if(cpu_y + half_paddle > ball_y) { 154 cpu_y -= 1; 155 156 } 157 if(cpu_y + half_paddle < ball_y) { 158 cpu_y += 159 1; 160 } 161 if(cpu_y < 1) cpu_y = 1; 162 if(cpu_y + PADDLE_HEIGHT 163 > 63) cpu_y = 63 - PADDLE_HEIGHT; 164 display.drawFastVLine(CPU_X, cpu_y, 165 PADDLE_HEIGHT, WHITE); 166 167 // Player paddle 168 display.drawFastVLine(PLAYER_X, 169 player_y, PADDLE_HEIGHT, BLACK); 170 if(up_state) { 171 player_y 172 -= 1; 173 } 174 if(down_state) { 175 player_y += 1; 176 177 } 178 up_state = down_state = false; 179 if(player_y < 1) 180 player_y = 1; 181 if(player_y + PADDLE_HEIGHT > 63) player_y = 63 - PADDLE_HEIGHT; 182 183 display.drawFastVLine(PLAYER_X, player_y, PADDLE_HEIGHT, WHITE); 184 185 186 update = true; 187 } 188 189 if(update) 190 display.display(); 191} 192 193 194void 195 drawCourt() { 196 display.drawRect(0, 0, 128, 64, WHITE); 197}
Documentation
untitled
untitled

Comments
Only logged in users can leave comments