-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.ino
407 lines (324 loc) · 8.74 KB
/
Game.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);
char block = 219; // first level obstacles' symbol by ASCII code (rectangle empty inside, some Chinese symbol)
const char gamer = '*'; // gamer's symbol
int blockCounter = 0; // counts how cells to skip after obstacle has been created
int counter = 0; // how many cycles gamer survived
const int baseDelay = 100;
int _delay; // game's speed, the lesser the faster
char line0[25]; // print buffer for lcd, line 0
char line1[25]; // print buffer for lcd, line 1
int gamerPos = 0; // what the gamer is located (line 0 or 1)
int gamerLastPos = 0; // where the gamer was located before he moved (line 0 or 1)
// set pin numbers:
const int button1Pin = 15; // the number of the pushbutton pin
const int button2Pin = 13; // the number of the pushbutton pin
const int led1Pin = 2; // the number of LED pin for button 1
const int led2Pin = 0; // the unmber of LED pin for button 2
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton status
int button1LastState = 0; // variable for reading the pushbutton status
int button2LastState = 0; // variable for reading the pushbutton status
String matrix1Str = String(16); // string for matrix effect intro
String matrix2Str = String(16); // string for matrix effect intro
char matrix1[17]; // char array for matrix effect intro
char matrix2[17]; // char array for matrix effect intro
// creates obstacles on lines
// draws checkpoints every 50 symbols
void GenBlocks(int current, char line0[], char line1[]) {
// checkpoint
if (counter > 0 && (counter % 50 == 0)) {
line0[current] = '|';
line1[current] = '|';
}
if (blockCounter > 0){
blockCounter--;
return;
}
// Lets try to create block on line 0
if (line0[current]!=block){
int factor = random(1000);
if (factor > 500) { // success
line0[current]=block;
}
}
// if there was a block created on line 0 we should then create also on line 1
if (line0[current] == block) {
if (random(1000) < 500) {
line1[current + 2] = block;
blockCounter = 3;
}
}
}
// New game init procedure
void NewGame() {
counter = 0;
_delay = baseDelay;
block = 219;
strcpy(line0," ");
strcpy(line1," ");
// generate some blocks for start
for (int i = 7; i < 16; i++) {
GenBlocks(i, line0, line1);
}
gamerPos = 0;
gamerLastPos = 0;
line0[4]=gamer;
lcd.clear();
lcd.print("Hello gamer :)");
lcd.setCursor(0,1);
lcd.print("Get ready!");
delay(1000);
Serial.println("Print game field");
Serial.println(line0);
Serial.println(line1);
lcd.setCursor(0,0);
lcd.print(line0);
lcd.setCursor(0,1);
lcd.print(line1);
Serial.println("Start game cycle");
}
void FillStringWithChars(char str[], int strLength) {
for (int i = 0; i< strLength; i++) {
str[i] = random(34,255);
}
}
// procedure shows "Matrix" effect when symbols fall down in random manner
void ShowMatrix(){
// init of char arrays with 16 space symbols (lcd is 16 symbols in a row)
strcpy(matrix1," ");
strcpy(matrix2," ");
matrix1[17]='\0';
matrix2[17]='\0';
// fill first string with random symbols from ASCII
FillStringWithChars(matrix1, 16);
Serial.println("matrix1");
Serial.println(matrix1);
// main cycle
for (int j = 0; j < 8; j++) {
// fill second string with random symbols
FillStringWithChars(matrix2, 16);
Serial.println("matrix2");
Serial.println(matrix2);
matrix1Str = String(matrix1);
matrix2Str = String(matrix2);
// override second string with some symbols from first string
for (int i = 0; i< 6; i++) {
int element = random(16);
matrix2Str[element]=matrix1Str[element];
}
//convert for printing
matrix1Str.toCharArray(matrix1,17);
matrix2Str.toCharArray(matrix2,17);
Serial.println("matrix1");
Serial.println(matrix1);
Serial.println("matrix2");
Serial.println(matrix2);
// print
lcd.setCursor(0,1);
lcd.print(matrix1);
lcd.setCursor(0,0);
lcd.print(matrix2);
delay(500);
FillStringWithChars(matrix1, 16);
Serial.println("matrix1");
Serial.println(matrix1);
for (int i = 0; i< 6; i++) {
int element = random(16);
matrix1Str[element]=matrix2Str[element];
}
//convert for printing
matrix1Str.toCharArray(matrix1,17);
matrix2Str.toCharArray(matrix2,17);
lcd.setCursor(0,1);
lcd.print(matrix2);
lcd.setCursor(0,0);
lcd.print(matrix1);
delay(500);
}
lcd.setCursor(0,0);
lcd.print("---+ Runner +---");
delay(3000);
}
// one time start-up procedure
void setup(void){
// init serial port and lcd
Serial.begin(115200);
lcd.begin(16, 2);
// initialize the LED pin as an output:
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// read the state of the pushbutton value:
button1LastState = digitalRead(button1Pin);
button2LastState = digitalRead(button2Pin);
// show intro
ShowMatrix();
// start new game
NewGame();
}
void RefreshPic(char line0[], char line1[]){
// we do not want to copy gamer's symbol from 4th column
for (int i = 0; i < 18; i++) {
if (i == 3) {
if (gamerLastPos == 0) {
line0[i] = '_';
line1[i] = line1[i+1];
}
else {
line0[i] = line0[i+1];
line1[i] = '_';
}
}
else
{ // transfer each symbol for 1 cell left creating movement
line0[i] = line0[i+1];
line1[i] = line1[i+1];
}
}
}
void ReadButtons(int *positn) {
// read the state of the pushbutton value:
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (button1State == HIGH) {
// turn LED on:
digitalWrite(led1Pin, LOW);
if (button1State != button1LastState) {
*positn = 0;
button1LastState = button1State;
}
} else {
button1LastState = 0;
// turn LED off:
digitalWrite(led1Pin, HIGH);
}
if (button2State == HIGH) {
// turn LED on:
digitalWrite(led2Pin, LOW);
if (button2State != button2LastState) {
*positn = 1;
button2LastState = button2State;
}
} else {
button2LastState = 0;
// turn LED off:
digitalWrite(led2Pin, HIGH);
}
}
void PrintScore(){
lcd.setCursor(0,1);
lcd.print("U did:");
lcd.print(counter);
lcd.print(" cycles");
}
// procedure for game over
void GameOver(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Game over TT");
PrintScore();
delay(5000);
// restart the game
NewGame();
}
void GameFinish() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("-+-+- You won!");
lcd.setCursor(0,1);
PrintScore();
delay(4000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Next player... ");
lcd.setCursor(0,1);
lcd.print(" >>> EXIT >>> ");
delay(3000);
ShowMatrix();
NewGame();
}
// change obstacles' view and increase speed
void UpdateGameLevel(int *_counter){
int level = *_counter / 50;
Serial.print("current counter ");
Serial.println(*_counter);
Serial.print("current level ");
Serial.println(level);
if (level == 1) {
block = '#';
return;
}
if (level == 2) {
block = ':';
_delay = baseDelay - level*5;
return;
}
if (level == 3) {
block = '.';
_delay = baseDelay - level*5;
return;
}
if (level == 4) {
block = '_';
_delay = baseDelay - level*5;
return;
}
if (level == 5) {
block = 255;
_delay = baseDelay - level*5;
return;
}
if (level == 6) {
GameFinish();
}
}
//main loop
void loop(void){
ReadButtons(&gamerPos);
Serial.print("Gamer sits on line ");
Serial.println(gamerPos);
RefreshPic(line0, line1);
gamerLastPos = gamerPos;
// check whether the gamer is in the same cell where the obstacle is
if (gamerPos == 0) {
if (line0[4] == block) {
GameOver();
}
}
else {
if (line1[4] == block) {
GameOver();
}
}
// print gamer's symbol
if (gamerPos == 0) {
line0[4] = gamer;
}
else {
line1[4] = gamer;
}
// show current game field to user
lcd.setCursor(0,0);
lcd.print(line0);
lcd.setCursor(0,1);
lcd.print(line1);
Serial.println(line0);
Serial.println(line1);
// generate new obstacles
GenBlocks(15, line0, line1);
counter++;
UpdateGameLevel(&counter);
// wait for user's input 5ms*_delay
for (int i = 0; i < _delay; i++)
{
delay(5);
ReadButtons(&gamerPos);
}
}