forked from Andymann/1D-Pong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1DPong_002.ino
384 lines (327 loc) · 8.02 KB
/
1DPong_002.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <Adafruit_NeoPixel.h>
#define PIXELS 42
#define SPEEDUP 100
#define LED_PIN 2
#define BUTTON1_PIN 3
#define BUTTON2_PIN 5
#define BUZZER_PIN 11
#define DEBUG
#define INITIALSPEED 13
#define INITIAL_LIVES 5
#define ZONEPLAYER1 8-1
#define ZONEPLAYER2 PIXELS-8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
typedef enum {LEFT, RIGHT} side_type;
bool quit = false;
bool buzzer = false;
struct Button {
int pin;
bool down; // button has been released just now
bool up; // button has been pressed just now
bool isPressed; // button is being pressed
};
struct Player {
uint32_t color;
uint16_t lives;
side_type side;
};
struct Ball {
uint32_t color;
float position;
side_type direction;
float speed; // pixels/s
};
struct Button button1;
struct Button button2;
struct Player p1;
struct Player p2;
struct Ball ball;
void setup() {
#ifdef DEBUG
Serial.begin(9600);
#endif
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
button1 = (Button) {
.pin = BUTTON1_PIN
};
button2 = (Button) {
.pin = BUTTON2_PIN
};
strip.begin();
// Range from 0-255, so 100 is a bit less than 50% brightness
strip.setBrightness(40);
strip.show();
ball = (Ball) {
.color = strip.Color(255, 255, 255),
.position = PIXELS / 2,
.direction = LEFT,
.speed = INITIALSPEED
};
p1 = (Player) {
.color = strip.Color(255, 0, 0),
.lives = INITIAL_LIVES,
.side = LEFT,
};
p2 = (Player) {
.color = strip.Color(0, 0, 255),
.lives = INITIAL_LIVES,
.side = RIGHT
};
setAllTo(strip.Color(0, 0, 0));
}
// Fill the dots one after the other with a color
void setAllTo(uint32_t color) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
}
void renderPlayer(Player *p) {
int maxRight = PIXELS - 1;
if (p->side == RIGHT) {
for (int i = 0; i < p->lives; i++) {
strip.setPixelColor((maxRight - i), p->color);
}
return;
}
if (p->side == LEFT) {
for (int i = 0; i < p->lives; i++) {
strip.setPixelColor(i, p->color);
}
return;
}
Serial.println("Player1:");
}
void renderBall(Ball *ball) {
strip.setPixelColor((ball->position), ball->color);
}
// Refresh interval at which to set our game loop
// To avoid having the game run at different speeds depending on hardware
const int refreshInterval = 16; // 60 FPS
// Used to calculate the delta between loops for a steady frame-rate
unsigned long lastRefreshTime = 0;
void processButtonInput(Button *button) {
bool prevPressed = button->isPressed;
int state = digitalRead(button->pin);
bool newPressed = state == LOW;
if (prevPressed && !newPressed) { // just released
button->up = true;
button->down = false;
} else if (!prevPressed && newPressed) { // just pressed
button->up = false;
button->down = true;
} else {
button->up = false;
button->down = false;
}
button->isPressed = newPressed;
/*
if (button->pin == 10) {
if (state) {
strip.setPixelColor(54, strip.Color(255, 255, 255));
} else {
strip.setPixelColor(54, strip.Color(0, 0, 0));
}
} else if (button->pin == 12) {
if (state) {
strip.setPixelColor(55, strip.Color(255, 255, 255));
} else {
strip.setPixelColor(55, strip.Color(0, 0, 0));
}
}
*/
}
void processInput() {
processButtonInput(&button1);
processButtonInput(&button2);
}
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void drawWinnerR() {
if (p2.lives == 1) {
tone(BUZZER_PIN, 150);
colorWipe(p1.color , 100);
noTone(BUZZER_PIN);
}
}
void drawWinnerL() {
if (p1.lives == 1) {
tone(BUZZER_PIN, 150);
colorWipe(p2.color , 100);
noTone(BUZZER_PIN);
}
}
void drawGame()
{
setAllTo(strip.Color(0, 0, 0));
renderPlayer(&p1);
renderPlayer(&p2);
renderBall(&ball);
strip.setPixelColor(ZONEPLAYER1, 25, 0, 0);
strip.setPixelColor(ZONEPLAYER2, 0, 0, 25);
strip.show();
Serial.println("Player1:");
Serial.println(p1.lives);
Serial.println("Player2:");
Serial.println(p2.lives);
}
void drawLostAnimation()
{
tone(BUZZER_PIN, 250);
setAllTo(strip.Color(255, 255, 255));
strip.show();
delay(200);
setAllTo(strip.Color(0, 0, 0));
strip.show();
noTone(BUZZER_PIN);
}
void drawStrobo()
{
static uint8_t pause = 0;
if (pause < 1)
{
setAllTo(strip.Color(255, 0, 0));
pause++;
}
else if (pause < 2)
{
setAllTo(strip.Color(0, 255, 0));
pause++;
}
else if (pause < 3)
{
setAllTo(strip.Color(0, 0, 255));
pause = 0;
}
strip.show();
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void drawStandby()
{
colorWipe(strip.Color(random(0, 255), random(0, 255), random(0, 255)), 10);
}
void updateBall(Ball *ball, unsigned int td) {
float moveBy = ball->speed * (td / (float) 1000);
if ((button1.down || button1.up) && ball->direction == LEFT) {
//----Man kann den Ball erst zurueckschicken, wenn er sich bis auf 1/4 des Stripes der eigenen Zone genaehert hat.
//----Sonst wird es langweilig, weil man sich durch "Dauerfeuer" gegenseitig im Mittelfeld halten kann.
if(ball->position<ZONEPLAYER1){
ball->speed = SPEEDUP / ball->position ;
ball->direction = RIGHT;
tone(BUZZER_PIN, 1000);
}else{
/*
drawLostAnimation();
drawWinnerL();
p1.lives = p1.lives - 1;
ball->direction = LEFT;
ball->position = PIXELS / 2; //ball->position * -1;
ball->speed = INITIALSPEED;
*/
}
}
if ((button2.down || button2.up) && ball->direction == RIGHT) {
if(ball->position>ZONEPLAYER2){
ball->speed = SPEEDUP / (PIXELS - ball->position);
ball->direction = LEFT;
tone(BUZZER_PIN, 1000);
}else{
/*
drawLostAnimation();
drawWinnerR();
p2.lives = p2.lives - 1;
ball->direction = RIGHT;
ball->position = PIXELS / 2;//(float) (PIXELS - 1) - ((float) (PIXELS - 1) - ball->position);
ball->speed = INITIALSPEED;
*/
}
}
switch (ball->direction) {
case LEFT:
ball->position = ball->position - moveBy;
if (ball->position < 0)
{
drawLostAnimation();
drawWinnerL();
p1.lives = p1.lives - 1;
ball->direction = LEFT;
ball->position = PIXELS / 2; //ball->position * -1;
ball->speed = INITIALSPEED;
}
break;
case RIGHT:
ball->position = ball->position + moveBy;
if (ball->position > PIXELS - 1)
{
drawLostAnimation();
drawWinnerR();
p2.lives = p2.lives - 1;
ball->direction = RIGHT;
ball->position = PIXELS / 2;//(float) (PIXELS - 1) - ((float) (PIXELS - 1) - ball->position);
ball->speed = INITIALSPEED;
}
break;
}
}
void update(unsigned int td)
{
noTone(BUZZER_PIN);
updateBall(&ball, td);
if (0 == p1.lives)
{
p1.lives = INITIAL_LIVES;
p2.lives = INITIAL_LIVES;
quit = true;
}
if (0 == p2.lives)
{
p1.lives = INITIAL_LIVES;
p2.lives = INITIAL_LIVES;
quit = true;
}
}
void loop() {
unsigned long now = millis();
if (lastRefreshTime == 0) {
lastRefreshTime = now;
return;
}
unsigned int td = now - lastRefreshTime;
if (td > refreshInterval)
{
lastRefreshTime = now;
static unsigned long lastTimeNotStandby;
processInput();
if (!quit)
{
update(td);
drawGame();
lastTimeNotStandby = millis();
}
else
{
if ((button1.down || button1.up) || (button2.down || button2.up))
{
quit = false;
}
drawStandby();
}
}
}