-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace.cpp
More file actions
199 lines (167 loc) · 6.05 KB
/
space.cpp
File metadata and controls
199 lines (167 loc) · 6.05 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
#include "space.h"
#include "sprites.h"
#include <string>
int speed = 1;
int density = 15;
int life = 3;
int scoreSpace = 0;
int NextScore = 500;
bool vulnrable = true;
Timeout invinibilityTimeout;
std::vector<Rock> rocks;
void spaceGame() {
while (1)
{
lcd.clear();
lcd.drawRect(0, 0, 84, 48, FILL_TRANSPARENT); // Draw screen border
if (scoreSpace > NextScore) {
NextScore += 500;
density -= 2;
speed++;
life++;
}
if (g_timer_flag) { //triggers every 150ms
g_timer_flag = 0;
RockGen(rocks, density);
RockFall(rocks,speed);
scoreSpace += 1;
}
lcd.drawSprite(x_pos,y_pos,8,11,(int *)alien); //draws player sprite
DisplayLife(life);
std::string scoreString = std::to_string(scoreSpace); //these 3 lines print the score on the top right
const char* cstr = scoreString.c_str();
lcd.printString(cstr , 60, 0);
RemoveOldRocks(rocks); //removes rocks that are out of bounds
for (const auto &rock : rocks) { //draws all rocks
lcd.drawRect(rock.x,rock.y + 5,rock.width,rock.height, FILL_BLACK);
}
for (const auto& rock : rocks) { //checks colision with rocks and if the player is vulnerable
if (checkCollision(rock) && vulnrable) {
vulnrable = false;
life--;
invinibilityTimeout.attach(&invincibility_isr,1000ms);
if (life==-1) {
destroyed();
}
}
}
if(joystick.get_direction() == N){
y_pos--;
printf(" Direction: N\n");
}else if(joystick.get_direction() == S){
y_pos++;
printf(" Direction: S\n");
}else if(joystick.get_direction() == E){
x_pos++;
printf(" Direction: E\n");
}else if(joystick.get_direction() == W){
x_pos--;
printf(" Direction: W\n");
}else if(joystick.get_direction() == NE){
y_pos--;
x_pos++;
printf(" Direction: NE\n");
}else if(joystick.get_direction() == NW){
y_pos--;
x_pos--;
printf(" Direction: NW\n");
}else if(joystick.get_direction() == SE){
y_pos++;
x_pos++;
printf(" Direction: SE\n");
}else if(joystick.get_direction() == SW){
y_pos++;
x_pos--;
printf(" Direction: SW\n");
//printf(" Direction: Centre\n");
}
if (g_buttonA_flag) { // check if menu button pressed
ThisThread::sleep_for(500ms);
g_buttonA_flag = 0;
printf(" exiting space game \n");
break;
}
boundary(x_pos, y_pos); // prevent player from going out of bounds out of bounds
lcd.refresh();
ThisThread::sleep_for(50ms);
}
}
void RockGen(std::vector<Rock> &rocks, int density) {
if (rand() % density == 0) { // Random chance to generate a new rock chance: (1 in density)
rocks.push_back({rand() % WIDTH, 0}); // Random x position and y position 0
}
}
void RockFall(std::vector<Rock> &rocks, int speed) {
for (auto &rock : rocks) { // Iterate through each rock in the vector
if (rock.y < HEIGHT + 10) {
rock.y += speed; // Move the rock down by speed pixels
}
}
}
bool checkCollision(const Rock& rock) { // player is 11x8
return (x_pos < rock.x + rock.width &&
x_pos + 11 > rock.x &&
y_pos < rock.y + rock.height &&
y_pos + 8 > rock.y);
}
void invincibility_isr() {
g_timer_flag = 1;
vulnrable = true;
}
void DisplayLife(int Life) {
switch (Life) {
case 0: {
lcd.drawSprite(2, 2, 7, 7, (int* )heartEmpty);
lcd.drawSprite(11, 2, 7, 7, (int* )heartEmpty);
lcd.drawSprite(20, 2, 7, 7, (int* )heartEmpty);
break;
}
case 1: {
lcd.drawSprite(2, 2, 7, 7, (int* )heartFull);
lcd.drawSprite(11, 2, 7, 7, (int* )heartEmpty);
lcd.drawSprite(20, 2, 7, 7, (int* )heartEmpty);
break;
}
case 2: {
lcd.drawSprite(2, 2, 7, 7, (int* )heartFull);
lcd.drawSprite(11, 2, 7, 7, (int* )heartFull);
lcd.drawSprite(20, 2, 7, 7, (int* )heartEmpty);
break;
}
case 3: {
lcd.drawSprite(2, 2, 7, 7, (int* )heartFull);
lcd.drawSprite(11, 2, 7, 7, (int* )heartFull);
lcd.drawSprite(20, 2, 7, 7, (int* )heartFull);
break;
}
default: {
char buffer[15];
sprintf(buffer, "LIFE:%d", life);
lcd.printString(buffer, 2, 0);
break;
}
}
}
void RemoveOldRocks(std::vector<Rock>& rocks) {
rocks.erase(std::remove_if(rocks.begin(), rocks.end(),
[](const Rock& r) { return r.y > 48; }), rocks.end());
}
void destroyed() {
lcd.clear();
lcd.drawRect(0, 0, 84, 48, FILL_TRANSPARENT); // Draw screen border
char buffer[32];
sprintf(buffer, "SCORE: %d", scoreSpace);
lcd.printString("GAME OVER", 1, 2);
lcd.printString(buffer, 1, 3);
lcd.refresh();
ThisThread::sleep_for(5000ms);
// Reset variables
scoreSpace = 0;
life = 3;
density = 15;
speed = 1;
x_pos = 42;
y_pos = 24;
rocks.clear(); // Clear the rocks vector
lcd.clear(); // Clear the LCD screen
}