-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpongCode.ino
204 lines (164 loc) · 4.99 KB
/
pongCode.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#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 = 33;
const unsigned long BALL_RATE = 35;
const uint8_t PADDLE_HEIGHT = 10;
int MAX_SCORE = 2;
int CPU_SCORE = 0;
int PLAYER_SCORE = 0;
// On MEGA, MOSI is 51, CLK is 52
// MOSI is Data pin on display breakout
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
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 the splash screen (we're legally required to do so)
unsigned long start = millis();
pinMode(UP_BUTTON, INPUT);
pinMode(DOWN_BUTTON, INPUT);
display.clearDisplay();
drawCourt();
while(millis() - start < 2000);
display.display();
ball_update = millis();
paddle_update = ball_update;
}
void drawGame() {
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(23,0);
display.println("-Score-");
display.setCursor(115,0);
display.print(PLAYER_SCORE);
display.setCursor(5,0);
display.print(CPU_SCORE);
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) {
PLAYER_SCORE++;
display.clearDisplay();
drawCourt();
ball_dir_x = -ball_dir_x;
new_x =30;
}
if(new_x == 127) {
CPU_SCORE++;
display.clearDisplay();
drawCourt();
ball_dir_x = -ball_dir_x;
new_x =90;
}
// Check if we hit the horizontal walls.
if(new_y == 17 || new_y == 63) {
ball_dir_y = -ball_dir_y;
new_y += ball_dir_y + ball_dir_y;
}
// Check if we hit the CPU paddle
if(new_x == CPU_X && new_y >= cpu_y && new_y <= cpu_y + PADDLE_HEIGHT) {
ball_dir_x = -ball_dir_x;
new_x += ball_dir_x + ball_dir_x;
}
// Check if we hit the player paddle
if(new_x == PLAYER_X
&& new_y >= player_y
&& new_y <= player_y + PADDLE_HEIGHT)
{
ball_dir_x = -ball_dir_x;
new_x += ball_dir_x + ball_dir_x;
}
display.drawPixel(ball_x, ball_y, BLACK);
display.drawPixel(new_x, new_y, WHITE);
ball_x = new_x;
ball_y = new_y;
ball_update += BALL_RATE;
update = true;
}
if(time > paddle_update) {
paddle_update += PADDLE_RATE;
// CPU paddle
display.drawFastVLine(CPU_X, cpu_y, PADDLE_HEIGHT, BLACK);
const uint8_t half_paddle = PADDLE_HEIGHT >> 1;
if(cpu_y + half_paddle > ball_y) {
cpu_y -= 1;
}
if(cpu_y + half_paddle < ball_y) {
cpu_y += 1;
}
if(cpu_y < 18) cpu_y = 18;
if(cpu_y + PADDLE_HEIGHT > 63) cpu_y = 63 - PADDLE_HEIGHT;
display.drawFastVLine(CPU_X, cpu_y, PADDLE_HEIGHT, WHITE);
// Player paddle
display.drawFastVLine(PLAYER_X, player_y, PADDLE_HEIGHT, BLACK);
if(up_state) {
player_y -= 1;
}
if(down_state) {
player_y += 1;
}
up_state = down_state = false;
if(player_y < 18) player_y = 18;
if(player_y + PADDLE_HEIGHT > 63) player_y = 63 - PADDLE_HEIGHT;
display.drawFastVLine(PLAYER_X, player_y, PADDLE_HEIGHT, WHITE);
update = true;
}
if(update)
display.display();
}
void drawCourt() {
display.drawRect(0, 16, 128, 48, WHITE);
}
void loop(){
drawGame();
if (PLAYER_SCORE >= MAX_SCORE){
display.clearDisplay();
display.setCursor(15,30);
display.println("YOU WIN!");
display.setCursor(40,19);
display.setTextSize(1);
display.println("YOUR SCORE:");
display.setCursor(95,19);
display.print(PLAYER_SCORE);
display.setCursor(18,50);
display.setTextSize(1);
display.println("Wait for a moment");
PLAYER_SCORE = 0;
CPU_SCORE = -1;
delay(1000);
}
if (CPU_SCORE >= MAX_SCORE){
display.clearDisplay();
display.setCursor(15,30);
display.println("YOU LOSE!");
display.setCursor(30,19);
display.setTextSize(1);
display.println("YOUR SCORE:");
display.setCursor(95,19);
display.print(PLAYER_SCORE);
display.setCursor(18,50);
display.setTextSize(1);
display.println("Wait for a moment");
PLAYER_SCORE = 0;
CPU_SCORE = -1;
delay(1000);
}
}