-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpong.h
202 lines (162 loc) · 5.84 KB
/
pong.h
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
/*
A simple "1972 Atari Pong" game. Handy, if you crashed your RC car ;-)
This code is based on: https://github.com/eholk/Arduino-Pong/blob/master/pong.ino
Modified to use it with the u8glib and my "Micro RC" transmitter by TheDIYGuy999
*/
#ifndef pong_h
#define pong_h
#include "Arduino.h"
const uint8_t PADDLE_RATE = 15; // 15
const uint8_t BALL_RATE = 7; // 7
const uint8_t FRAME_RATE = 70; // Refresh display every 70ms = 14.2Hz (less slows the ball update down on an 8MHz MPU...)
const uint8_t PADDLE_HEIGHT = 14; // 14
const uint8_t half_paddle = PADDLE_HEIGHT / 2;
uint8_t ball_x = 64, ball_y = 32; // 64, 32
uint8_t ball_dir_x = -1, ball_dir_y = 1; // 1, 1
uint8_t new_x;
uint8_t new_y;
unsigned long ball_update;
unsigned long paddle_update;
const uint8_t CPU_X = 12; // 12
uint8_t cpu_y = 16;
const uint8_t PLAYER_X = 115; // 115
uint8_t player_y = 16;
uint8_t game_over_difference = 10; // The game is over after this point difference is reached!
uint8_t cpu_points = 0;
uint8_t player_points = 0;
boolean cpu_won = false;
boolean player_won = false;
//
// =======================================================================================================
// DISPLAY UPDATE SUBFUNCTION
// =======================================================================================================
//
void displayUpdate() {
static unsigned long lastDisplay;
if (millis() - lastDisplay >= FRAME_RATE) {
lastDisplay = millis();
// clear screen ----
u8g.firstPage();
do {
//u8g.drawFrame(0, 0, 128, 64); // only for screen offset test!
u8g.drawCircle(new_x, new_y, 1); // Ball
u8g.drawVLine(CPU_X, cpu_y, PADDLE_HEIGHT); // CPU paddle
u8g.drawVLine(PLAYER_X, player_y, PADDLE_HEIGHT); // Player paddle
u8g.drawVLine(64, 0, 3); // Vertical dashed line segments
u8g.drawVLine(64, 6, 3);
u8g.drawVLine(64, 12, 3);
u8g.drawVLine(64, 18, 3);
u8g.drawVLine(64, 24, 3);
u8g.drawVLine(64, 30, 3);
u8g.drawVLine(64, 36, 3);
u8g.drawVLine(64, 42, 3);
u8g.drawVLine(64, 48, 3);
u8g.drawVLine(64, 54, 3);
u8g.drawVLine(64, 60, 3);
u8g.setPrintPos(40, 10); // CPU points counter
u8g.print(cpu_points);
u8g.setPrintPos(80, 10); // Player points counter
u8g.print(player_points);
// Game over window
if (cpu_won || player_won) {
u8g.setColorIndex(0);
u8g.drawBox(22, 12, 84, 45); // Clear area behind window
u8g.setColorIndex(1);
u8g.drawFrame(22, 12, 84, 45); // Draw window frame
u8g.setPrintPos(36, 28);
u8g.print("GAME OVER"); // Game over
u8g.setPrintPos(31, 48);
u8g.print("Press BACK!"); // Press button "Back" to restart
}
if (cpu_won ) {
u8g.setPrintPos(38, 38);
u8g.print("YOU LOST"); // You lost
}
if (player_won ) {
u8g.setPrintPos(40, 38);
u8g.print("YOU WON"); // You won
}
// show display queue ----
} while ( u8g.nextPage() );
}
}
//
// =======================================================================================================
// PONG GAME
// =======================================================================================================
//
void pong() {
unsigned long time = millis();
static boolean center;
// Restart game ----------------------------------------------------------
if (digitalRead(BUTTON_BACK) == LOW) {
cpu_won = false;
player_won = false;
cpu_points = 0;
player_points = 0;
}
// Ball update -----------------------------------------------------------
static unsigned long lastBall;
if (millis() - lastBall >= BALL_RATE && !cpu_won && !player_won) {
lastBall = millis();
new_x = ball_x + ball_dir_x;
new_y = ball_y + ball_dir_y;
// Counter
if (new_x > 54 && new_x < 74) center = true;
if (center && new_x < CPU_X) player_points++, center = false; // Count CPU points
if (center && new_x > PLAYER_X) cpu_points++, center = false; // Count Player points
if (cpu_points - player_points >= game_over_difference) cpu_won = true; // Game over, you lost
if (player_points - cpu_points >= game_over_difference) player_won = true; // Game over, you won
// Check if we hit the vertical walls
if (new_x == 1 || new_x == 126) {
ball_dir_x = -ball_dir_x;
new_x += ball_dir_x + ball_dir_x;
}
// Check if we hit the horizontal walls
if (new_y == 1 || new_y == 62) {
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;
}
ball_x = new_x;
ball_y = new_y;
}
// Paddle update -----------------------------------------------------------
static unsigned long lastPaddle;
if (millis() - lastPaddle >= PADDLE_RATE && !cpu_won && !player_won) {
lastPaddle = millis();
// CPU paddle control----
if (cpu_y + half_paddle > ball_y) {
cpu_y -= 1;
}
if (cpu_y + half_paddle < ball_y) {
cpu_y += 1;
}
if (cpu_y < 1) cpu_y = 1;
if (cpu_y + PADDLE_HEIGHT > 63) cpu_y = 63 - PADDLE_HEIGHT;
// Player paddle control----
#ifdef CONFIG_MICRO_RC // If we have a channel 4 (= 4 channel joystick transmitter)
player_y = map(data.axis2, 100, 0, 0, (63 - PADDLE_HEIGHT));
#else // Else (2 channel car style transmitter)
player_y = map(data.axis1, 100, 0, 0, (63 - PADDLE_HEIGHT));
#endif
}
// Display update ------------------------------------------------------------
displayUpdate();
}
#endif