forked from slawa19/IKEA-LED-Table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.ino
219 lines (201 loc) · 4.87 KB
/
pong.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
/* LedTable
*
* Written by: Ing. David Hrbaty
*
*
* Main code for Pong game
*/
#include "pongCommon.h"
unsigned long curTime;
unsigned long prevUpdateTime = 0;
void pongInit(){
scorePlayerLeft = 0;
scorePlayerRight = 0;
FIELD_WIDTH * FIELD_HEIGHT;
positionPlayerLeft = FIELD_HEIGHT/2;
positionPlayerRight = FIELD_HEIGHT/2;
ballx = FIELD_WIDTH/2;
bally = FIELD_WIDTH/2;
velocityx = 1;
velocityy = 0;
ballBounces = 0;
gameSpeed = 180;
lastAutoPlayerMoveTime = 0;
rumbleUntil = 0;
waitUntil = 0;
}
void runPong(){
pongInit();
boolean pongRunning = true;
while(pongRunning){
if (scorePlayerLeft == MAX_SCORE || scorePlayerRight == MAX_SCORE){
pongRunning = false;
break;
}
checkBallHitByPlayer();
if((curTime - lastAutoPlayerMoveTime) > AUTO_PLAYER_SPEED) {
if(moveAutoPlayer()) {
lastAutoPlayerMoveTime = curTime;
}
}
ballx += velocityx;
bally += velocityy;
checkBallOutOfBounds();
clearTablePixels();
// Draw ball
setTablePixel(ballx,bally,WHITE);
// Draw player left
for (int y=positionPlayerLeft-PLAYER_HEIGHT/2; y<=positionPlayerLeft+PLAYER_HEIGHT/2; ++y){
setTablePixel(0, y, BLUE);
}
// Draw player right
for (int y=positionPlayerRight-PLAYER_HEIGHT/2; y<=positionPlayerRight+PLAYER_HEIGHT/2; ++y){
setTablePixel(FIELD_WIDTH-1, y, YELLOW);
}
showPixels();
boolean dirChanged = false;
do{
readInput();
if (curControl == BTN_EXIT){
pongRunning = false;
break;
}
if (curControl != BTN_NONE && !dirChanged){//Can only change direction once per loop
dirChanged = true;
setPosition();
}
curTime = millis();
}
while ((curTime - prevUpdateTime) <250);//Once enough time has passed, proceed. The lower this number, the faster the game is
prevUpdateTime = curTime;
}
fadeOut();
}
void setPosition(){
switch(curControl){
case BTN_DOWN:
if(positionPlayerLeft + (PLAYER_HEIGHT-1) / 2 < FIELD_HEIGHT-1){
++positionPlayerLeft;
}
break;
case BTN_UP:
if(positionPlayerLeft - PLAYER_HEIGHT / 2 > 0) {
--positionPlayerLeft;
}
break;
case BTN_START:
break;
case BTN_LEFT:
break;
case BTN_RIGHT:
break;
}
}
void checkBallHitByPlayer() {
if(ballx == 1)
{
if(bally == positionPlayerLeft)
{
velocityx = 1;
ballx = 1;
++ballBounces;
rumbleUntil = curTime + 200;
}
else if(bally < positionPlayerLeft && bally >= positionPlayerLeft - PLAYER_HEIGHT / 2)
{
velocityx = 1;
velocityy = max(-1,velocityy-1);
ballx = 1;
bally = positionPlayerLeft - PLAYER_HEIGHT / 2-1;
++ballBounces;
rumbleUntil = curTime + 200;
}
else if(bally > positionPlayerLeft && bally <= positionPlayerLeft + (PLAYER_HEIGHT-1) / 2)
{
velocityx = 1;
velocityy = min(1,velocityy+1);
ballx = 1;
bally = positionPlayerLeft + (PLAYER_HEIGHT-1) / 2+1;
++ballBounces;
rumbleUntil = curTime + 200;
}
}
else if(ballx == FIELD_WIDTH-2)
{
if(bally == positionPlayerRight)
{
velocityx = -1;
ballx = FIELD_WIDTH-2;
++ballBounces;
}
else if(bally < positionPlayerRight && bally >= positionPlayerRight - PLAYER_HEIGHT / 2)
{
velocityx = -1;
velocityy = max(-1,velocityy-1);
ballx = FIELD_WIDTH-2;
bally = positionPlayerRight - PLAYER_HEIGHT / 2-1;
++ballBounces;
}
else if(bally > positionPlayerRight && bally <= positionPlayerRight + (PLAYER_HEIGHT-1) / 2)
{
velocityx = -1;
velocityy = min(1,velocityy+1);
ballx = FIELD_WIDTH-2;
bally = positionPlayerRight + (PLAYER_HEIGHT-1) / 2+1;
++ballBounces;
}
}
}
void checkBallOutOfBounds() {
if(bally < 0)
{
velocityy = - velocityy;
//bally = 0;
bally = 1;
} else if(bally > FIELD_HEIGHT-1)
{
velocityy = - velocityy;
bally = FIELD_HEIGHT-2;
//bally = FIELD_HEIGHT-1;
}
if(ballx < 0)
{
velocityx = - velocityx;
velocityy = 0;
ballx = FIELD_WIDTH/2;
bally = FIELD_HEIGHT/2;
++scorePlayerRight;
ballBounces = 0;
waitUntil = curTime + 2000;
}
else if(ballx > FIELD_WIDTH-1)
{
velocityx = - velocityx;
velocityy = 0;
ballx = FIELD_WIDTH/2;
bally = FIELD_HEIGHT/2;
++scorePlayerLeft;
ballBounces = 0;
waitUntil = curTime + 2000;
}
}
boolean moveAutoPlayer()
{
if(bally < positionPlayerRight)
{
if(positionPlayerRight - PLAYER_HEIGHT / 2>0)
{
--positionPlayerRight;
return true;
}
}
else if(bally > positionPlayerRight)
{
if(positionPlayerRight + (PLAYER_HEIGHT-1) / 2 < FIELD_HEIGHT -1)
{
++positionPlayerRight;
return true;
}
}
return false;
}