-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCDDisplayRunnerGame.ino
More file actions
166 lines (136 loc) · 2.31 KB
/
LCDDisplayRunnerGame.ino
File metadata and controls
166 lines (136 loc) · 2.31 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
#include <LiquidCrystal.h>
#include <math.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int buttonPin = 3;
bool buttonReleased = true;
int playerY = 1;
int obstacle1X = 15;
int obstacle1Y = 1;
int obstacle2X = 7;
int obstacle2Y = 1;
bool gameOver = false;
bool started = false;
int scoreX = 15;
int distScore = 0;
byte playerChar[8] = {
0b01110,
0b01110,
0b01110,
0b00100,
0b01110,
0b10101,
0b00100,
0b01010
};
byte cactusChar[8] = {
0b00100,
0b00101,
0b00101,
0b10111,
0b10100,
0b11100,
0b00100,
0b00100
};
void jump()
{
lcd.setCursor(2, 0);
lcd.write(byte(0));
playerY = 0;
}
void ground()
{
lcd.setCursor(2, 1);
lcd.write(byte(0));
playerY = 1;
}
void obstacle(int x, int y)
{
if (obstacle1X < 0)
{
obstacle1X = 15;
}
if (obstacle2X < 0)
{
obstacle2X = 15;
}
lcd.setCursor(x, y);
lcd.write(byte(1));
}
void checkCollision()
{
if (obstacle1X == 2 && playerY == 1 || obstacle2X == 2 && playerY == 1)
{
gameOver = true;
}
}
void score()
{
lcd.setCursor(scoreX, 0);
lcd.print(distScore);
distScore++;
double d_distScore = (double)distScore;
double log10_distScore = log10(d_distScore);
if ((log10_distScore - floor(log10_distScore)) == 0.0 && distScore >= 10)
{
scoreX--;
}
}
void setup() {
lcd.begin(16, 2);
pinMode(buttonPin, INPUT_PULLUP);
// Create new characters:
lcd.createChar(0, playerChar);
lcd.createChar(1, cactusChar);
lcd.setCursor(0, 0);
lcd.print("Press to Start");
}
void loop() {
if (!started)
{
if (digitalRead(buttonPin) == LOW)
{
started = true;
}
return;
}
if (gameOver)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Over!");
lcd.setCursor(0, 1);
lcd.print("Press to Restart");
if (digitalRead(buttonPin) == LOW)
{
gameOver = false;
distScore = 0;
scoreX = 15;
obstacle1X = 15;
obstacle2X = 7;
}
return;
}
if (digitalRead(buttonPin) == LOW && buttonReleased) // jump
{
jump();
buttonReleased = false;
}
else if (digitalRead(buttonPin) == HIGH) // ground
{
ground();
buttonReleased = true;
}
else
{
ground();
}
obstacle(obstacle1X, obstacle1Y);
obstacle(obstacle2X, obstacle2Y);
checkCollision();
score();
delay(500);
lcd.clear();
obstacle1X--;
obstacle2X--;
}