Repository navigation
Expand file tree
/
Copy pathRacing_Arduino_Game.ino
More file actions
264 lines (227 loc) · 3.87 KB
/
Copy pathRacing_Arduino_Game.ino
File metadata and controls
264 lines (227 loc) · 3.87 KB
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
//v.1.0.3 itcooky@mail.ru
//you need library LiquidCrystal_V1.2.1.zip
//edit after add LiquidCrystal_V1.2.1.zip file ...\LiquidCrystal\I2CIO.cpp
//str
//#include <../Wire/Wire.h>
//edit to
//#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
//it will be 0x3f or maybe 0x27
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
volatile unsigned long start = 0;
volatile unsigned long stop = 0;
unsigned long m = 0;
unsigned long l = 0;
unsigned long p = 0;
unsigned long s = 0;
unsigned long smax = 0;
int sp = 2;
int b = 0;
int c = 0;
int x = 16;
int x2 = 18;
int x3 = 20;
int x4 = 23;
int x12 = 0;
int x13 = 0;
int x14 = 0;
int d = 0;
int t = 250;
boolean sw = false;
boolean game = false;
int pb = 0;
void setup() {
pinMode(sp, INPUT);
digitalWrite(sp, HIGH);
lcd.begin(16,2);
lcd.setCursor(0, 0);
lcd.print(" Racing the Game");
lcd.setCursor(0, 1);
lcd.print(">>Press to start");
}
void loop() {
if ((digitalRead(sp) == HIGH) && (game == false))
{
p = p + 1;
m = millis();
}
if ((millis() - m > 40) && (p > 0) && (game == false)) {
if ( sw == true) {
b = 0;
sw = false;
}
b = b + 1;
p = 0;
}
if ((millis() - m > 450) && (b > 0) && (sw == false)&& (game == false)) {
sw = true;
c = b;
lcd.clear();
if (c == 2) {
lcd.print("1 press-Start");
lcd.setCursor(0, 1);
lcd.print("3 press-DeletRec");
}
}
if (c == 3) {
EEPROM.write(0, 0);
EEPROM.write(1, 0);
lcd.setCursor(0, 0);
lcd.print("Deleted Record");
lcd.setCursor(0, 1);
lcd.print(" Statistics");
}
if (c == 1 ) {
game = true;
if ((millis() - l > t)) {
if (((x == 16) && (x2 == 16)) || ((x2 == 16) && (x3 == 16))) {
x2= x2+2;
}
if (((x == 16) && (x4 == 16)) || ((x4 == 16) && (x3 == 16))) {
x4= x4+2;
}
if (pb == 0) {
lcd.setCursor(0, 0);
lcd.print("Z");
lcd.setCursor(0, 1);
lcd.print(" ");
}
if (pb == 1) {
lcd.setCursor(0, 1);
lcd.print("Z");
lcd.setCursor(0, 0);
lcd.print(" ");
}
x = x - 1;
lcd.setCursor(x, 0);
lcd.print("BB ");
x2 = x2 - 1;
lcd.setCursor(x2, 1);
lcd.print("BB ");
if (d > 0 ) {
x3 = x3 -1;
lcd.setCursor(x3, 0);
lcd.print("EE ");
}
if (d > 1 ) {
x4 = x4 - 1;
lcd.setCursor(x4, 1);
lcd.print("EE ");
}
lcd.setCursor(12, 1);
lcd.print(s);
if (((x == 0) && (pb == 0)) || ((x2 == 0) && (pb == 1)) || ((x3 == 0) && (pb == 0)) || ((x4 == 0) && (pb == 1))) {
game = false;
}
l = millis();
s = s + 1;
//t = t - 1;
switch (s) {
case 50:
t = t - 20;
break;
case 100:
d = d + 1;
break;
case 250:
t = t - 10;
break;
case 400:
d = d + 1;
break;
case 500:
t = t - 10;
break;
case 600:
t = t - 10;
break;
}
}
if (x == 0) {
x = 16 + random(0,1);
lcd.setCursor(1, 0);
lcd.print(" ");
x12= x + random(2,3)+2;
x13 = x12 + random(1,3)+2;
x14 = x13 + random(2,3)+3;
}
if (x2 == 0) {
x2 = x12;
lcd.setCursor(1, 1);
lcd.print(" ");
}
if (x3 == 0) {
x3 = x13;
lcd.setCursor(1, 0);
lcd.print(" ");
}
if (x4 == 0) {
x4 = x14;
lcd.setCursor(1, 1);
lcd.print(" ");
}
}
if ((digitalRead(sp) == HIGH) && (game == true))
{
p = p + 1;
m = millis();
}
if ((millis() - m > 40) && (p > 0) && (game == true)) {
if (pb == 0) {
pb = 1;
}
else {
pb = 0;
}
p = 0;
}
if ((s > 0) && (game == false)) {
byte xx[] = {EEPROM.read(0), EEPROM.read(1)};
int *y = (int *)&xx;
smax = y[0];
if (s > smax) {
byte *x = (byte *)&s;
EEPROM.write(0, x[0]);
EEPROM.write(1, x[1]);
//EEPROM.write(addr, s);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("!!!NEW RECORD!!!");
lcd.setCursor(0, 1);
lcd.print(" Best Score:");
lcd.setCursor(12, 1);
lcd.print(s);
delay(3000);
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("###GAME##OVER###");
lcd.setCursor(0, 1);
lcd.print("YScore:");
lcd.setCursor(7, 1);
lcd.print(s);
lcd.setCursor(11, 1);
lcd.print("<");
lcd.setCursor(12, 1);
lcd.print(smax);
delay(3000);
}
c = 0;
s = 0;
t = 250;
d = 0;
x = 16;
x2 = 18;
x3 = 20;
x4 = 23;
}
}
void begin() {
start = millis();
}
void end() {
stop = millis() - start;
}