-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMicroPong.ino
240 lines (206 loc) · 4.29 KB
/
MicroPong.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <MicroView.h>
const int sensorPin = A1;
const float sensorMaxValue = 1024.0;
const int renderDelay = 16; // About 60hz
const int startDelay = 4000;
const int gameOverDelay = 6000;
const int scoreToWin = 10;
int playerScore = 0;
int enemyScore = 0;
const float paddleWidth = LCDWIDTH/16.0;
const float paddleHeight = LCDHEIGHT/3.0;
const float halfPaddleWidth = paddleWidth/2.0;
const float halfPaddleHeight = paddleHeight/2.0;
float playerPosX = 1.0 + halfPaddleWidth;
float playerPosY = 0.0;
float enemyPosX = LCDWIDTH - 1.0 - halfPaddleWidth;
float enemyPosY = 0.0;
float enemyVelY = 0.5;
const float ballRadius = 2.0;
const float ballSpeedX = 1.0;
float ballPosX = LCDWIDTH/2.0;
float ballPosY = LCDHEIGHT/2.0;
float ballVelX = -1.0 * ballSpeedX;
float ballVelY = 0;
void setup()
{
initializeGraphics();
initializeInput();
displayGameStart();
}
void resetGame()
{
enemyScore = 0;
playerScore = 0;
enemyPosY = 0.0;
ballPosX = LCDWIDTH/2.0;
ballPosY = LCDHEIGHT/2.0;
ballVelX = -1.0 * ballSpeedX;
ballVelY = 0.0;
}
void initializeGraphics()
{
uView.begin();
uView.setFontType(1);
}
void initializeInput()
{
digitalWrite(sensorPin, HIGH);
pinMode(sensorPin, INPUT);
}
void displayGameStart()
{
uView.clear(PAGE);
renderString(20,10, "Get");
renderString(10,30, "Ready!");
uView.display();
delay(startDelay);
}
void loop()
{
updateGame();
renderGame();
if (playerScore >= scoreToWin)
{
gameOver(true);
}
else if (enemyScore >= scoreToWin)
{
gameOver(false);
}
}
void updateGame()
{
updatePlayer();
updateEnemy();
updateBall();
}
float clampPaddlePosY(float paddlePosY)
{
float newPaddlePosY = paddlePosY;
if (paddlePosY - halfPaddleHeight < 0)
{
newPaddlePosY = halfPaddleHeight;
}
else if (paddlePosY + halfPaddleHeight > LCDHEIGHT)
{
newPaddlePosY = LCDHEIGHT - halfPaddleHeight;
}
return newPaddlePosY;
}
void updatePlayer()
{
float knobValue = analogRead(sensorPin) / sensorMaxValue;
playerPosY = clampPaddlePosY(knobValue * LCDHEIGHT);
}
void updateEnemy()
{
// Follow the ball at a set speed
if (enemyPosY < ballPosY)
{
enemyPosY += enemyVelY;
}
else if(enemyPosY > ballPosY)
{
enemyPosY -= enemyVelY;
}
enemyPosY = clampPaddlePosY(enemyPosY);
}
void updateBall()
{
ballPosY += ballVelY;
ballPosX += ballVelX;
// Top and bottom wall collisions
if (ballPosY < ballRadius)
{
ballPosY = ballRadius;
ballVelY *= -1.0;
}
else if (ballPosY > LCDHEIGHT - ballRadius)
{
ballPosY = LCDHEIGHT - ballRadius;
ballVelY *= -1.0;
}
// Left and right wall collisions
if (ballPosX < ballRadius)
{
ballPosX = ballRadius;
ballVelX = ballSpeedX;
enemyScore++;
}
else if (ballPosX > LCDWIDTH - ballRadius)
{
ballPosX = LCDWIDTH - ballRadius;
ballVelX *= -1.0 * ballSpeedX;
playerScore++;
}
// Paddle collisions
if (ballPosX < playerPosX + ballRadius + halfPaddleWidth)
{
if (ballPosY > playerPosY - halfPaddleHeight - ballRadius &&
ballPosY < playerPosY + halfPaddleHeight + ballRadius)
{
ballVelX = ballSpeedX;
ballVelY = 2.0 * (ballPosY - playerPosY) / halfPaddleHeight;
}
}
else if (ballPosX > enemyPosX - ballRadius - halfPaddleWidth)
{
if (ballPosY > enemyPosY - halfPaddleHeight - ballRadius &&
ballPosY < enemyPosY + halfPaddleHeight + ballRadius)
{
ballVelX = -1.0 * ballSpeedX;
ballVelY = 2.0 * (ballPosY - enemyPosY) / halfPaddleHeight;
}
}
}
void renderGame()
{
uView.clear(PAGE);
renderScores(playerScore, enemyScore);
renderPaddle(playerPosX, playerPosY);
renderPaddle(enemyPosX, enemyPosY);
renderBall(ballPosX, ballPosY);
uView.display();
delay(renderDelay);
}
void renderString(int x, int y, String string)
{
uView.setCursor(x,y);
uView.print(string);
}
void renderPaddle(int x, int y)
{
uView.rect(
x - halfPaddleWidth,
y - halfPaddleHeight,
paddleWidth,
paddleHeight);
}
void renderBall(int x, int y)
{
uView.circle(x, y, 2);
}
void renderScores(int firstScore, int secondScore)
{
renderString(10, 0, String(firstScore));
renderString(LCDWIDTH - 14, 0, String(secondScore));
}
void gameOver(bool didWin)
{
if (didWin)
{
renderString(20,10, "You");
renderString(20,30, "Win!");
}
else
{
renderString(20,10, "You");
renderString(15,30, "Lose!");
}
uView.display();
delay(gameOverDelay);
// Get ready to start the game again.
resetGame();
displayGameStart();
}