-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip_Shooter.ino
346 lines (302 loc) · 9.12 KB
/
Ship_Shooter.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
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;
#define TEST 0 //1 - disable player death and shows free memory
#define CLOUDS 0 //0 - disable clouds
//84x48
const int PLAYER = 1;
const int ALIEN = 2;
#include "Ship_Shooter.h"
//ship
const int SHIP_W = 3;
const int SHIP_H = 5;
int ship_x = 0;
int ship_y = 21;
int ship_v = 1;
int ship_lives = 3;
int score = 0;
int record = 0;
int time_after_death = 20 * 4; //time to wait after death 20 * seconds to wait;
//player bullets
const int PLAYER_BULLETS_COUNT = 10;
bullet *player_bullets[PLAYER_BULLETS_COUNT];
const int BULLET_DELAY_DURATION = (1 * 20) / 4; //ship will be able to shot every x seconds; seconds * 20; seconds * (1000ms / 50ms); 50 because main loop works every 50ms
int bullet_delay_count = BULLET_DELAY_DURATION; //it will be better if player will be able to shoot right after the game starts; later in the code game checks if bulet_dellay_count is equals to BULLET_DELAY_DURATION and allows player to fire
//aliens
const int ALIENS_COUNT = 10;
aliens *aliens_tab[ALIENS_COUNT];
//aliens bullets
const int ALIEN_BULLETS_COUNT = 30;
bullet *alien_bullets[ALIEN_BULLETS_COUNT];
const int ALIEN_FIRE_SPEED = 30;
#if(CLOUDS)
//clouds
const int CLOUDS_COUNT = 5;
clouds *clouds_tab[CLOUDS_COUNT];
#endif
//__________MAIN__________
void setup(){
gb.begin();
gb.titleScreen(F("Ship shooter"));
gb.pickRandomSeed();
gb.display.setFont(font3x5);
for (int i = 0; i < PLAYER_BULLETS_COUNT; i++){
player_bullets[i] = nullptr;
}
#if(CLOUDS)
for (int i = 0; i < CLOUDS_COUNT; i++){
clouds_tab[i] = nullptr;
}
#endif
for (int i = 0; i < ALIENS_COUNT; i++){
aliens_tab[i] = nullptr;
}
for (int i = 0; i < ALIEN_BULLETS_COUNT; i++){
alien_bullets[i] = nullptr;
}
}
void loop(){
while (gb.update()){ //returns true every 50ms; 20fps
//INPUT
//go to menu
if(gb.buttons.repeat(BTN_C, 0))
gb.titleScreen(F("Space shooter"));
if(ship_lives){
//move ship
if (gb.buttons.repeat(BTN_RIGHT, 0) && ship_x < LCDWIDTH - SHIP_W && gb.buttons.repeat(BTN_DOWN, 0) && ship_y < LCDHEIGHT - SHIP_H){
ship_x += ship_v;
ship_y += ship_v;
} else if (gb.buttons.repeat(BTN_RIGHT, 0) && ship_x < LCDWIDTH - SHIP_W && gb.buttons.repeat(BTN_UP, 0) && ship_y > 0){
ship_x += ship_v;
ship_y -= ship_v;
} else if (gb.buttons.repeat(BTN_LEFT, 0) && ship_x > 0 && gb.buttons.repeat(BTN_DOWN, 0) && ship_y < LCDHEIGHT - SHIP_H){
ship_x -= ship_v;
ship_y += ship_v;
} else if (gb.buttons.repeat(BTN_LEFT, 0) && ship_x > 0 && gb.buttons.repeat(BTN_UP, 0) && ship_y > 0){
ship_x -= ship_v;
ship_y -= ship_v;
} else if (gb.buttons.repeat(BTN_UP, 0) && ship_y > 0){
ship_y -= ship_v;
} else if (gb.buttons.repeat(BTN_DOWN, 0) && ship_y < LCDHEIGHT - SHIP_H){
ship_y += ship_v;
} else if (gb.buttons.repeat(BTN_LEFT, 0) && ship_x > 0){
ship_x -= ship_v;
} else if (gb.buttons.repeat(BTN_RIGHT, 0) && ship_x < LCDWIDTH - SHIP_W){
ship_x += ship_v;
}
//fire; create player bullets
if(bullet_delay_count < BULLET_DELAY_DURATION) bullet_delay_count++;
if (gb.buttons.repeat(BTN_A, 0) && bullet_delay_count == BULLET_DELAY_DURATION){
for (int i = 0; i < PLAYER_BULLETS_COUNT; i++){
if (player_bullets[i] == nullptr){
player_bullets[i] = new bullet(ship_x, ship_y, PLAYER);
bullet_delay_count = 0;
break;
}
}
}
//LOGIC
//__________PLAYER__________
//move bullets and/or delete bullets
for (int i = 0; i < PLAYER_BULLETS_COUNT; i++){
if (player_bullets[i] != nullptr)
player_bullets[i] -> move_bullet();
if (player_bullets[i] != nullptr && player_bullets[i] -> bullet_out()){
delete player_bullets[i];
player_bullets[i] = nullptr;
}
}
//player alien collision
#if(!TEST)
for (int i = 0; i < ALIENS_COUNT; i++){
if (aliens_tab[i] != nullptr && aliens_tab[i] -> alien_collision(ship_x, ship_y)){
ship_lives--;
ship_x = 0;
ship_y = 21;
for (int i = 0; i < PLAYER_BULLETS_COUNT; i++){
delete player_bullets[i];
player_bullets[i] = nullptr;
}
for (int i = 0; i < ALIENS_COUNT; i++){
delete aliens_tab[i];
aliens_tab[i] = nullptr;
}
for (int i = 0; i < ALIEN_BULLETS_COUNT; i++){
delete alien_bullets[i];
alien_bullets[i] = nullptr;
}
break;
}
}
#endif
//player - alien bullet collision
#if(!TEST)
for (int i = 0; i < ALIEN_BULLETS_COUNT; i++){
if (alien_bullets[i] != nullptr && gb.collideBitmapBitmap(ship_x, ship_y, SHIP, alien_bullets[i] -> get_x(), alien_bullets[i] -> get_y(), BULLET)){
ship_lives--;
ship_x = 0;
ship_y = 21;
for (int i = 0; i < PLAYER_BULLETS_COUNT; i++){
delete player_bullets[i];
player_bullets[i] = nullptr;
}
for (int i = 0; i < ALIENS_COUNT; i++){
delete aliens_tab[i];
aliens_tab[i] = nullptr;
}
for (int i = 0; i < ALIEN_BULLETS_COUNT; i++){
delete alien_bullets[i];
alien_bullets[i] = nullptr;
}
break;
}
}
#endif
// __________ALIENS__________
//create alien
if(!(gb.frameCount % 10)){ //This prevents aliens from forming over other aliens and sending one huge wave of them
for (int i = 0; i < ALIENS_COUNT; i++){
if (aliens_tab[i] == nullptr){
aliens_tab[i] = new aliens(random(1, 11));
break;
}
}
}
//move alien
for (int i = 0; i < ALIENS_COUNT; i++){
if (aliens_tab[i] != nullptr)
aliens_tab[i] -> move_alien();
if (aliens_tab[i] != nullptr && aliens_tab[i] -> alien_out()){
delete aliens_tab[i];
aliens_tab[i] = nullptr;
}
}
//alien fire
for (int i = 0; i < ALIENS_COUNT; i++){
for (int j = 0; j < ALIEN_BULLETS_COUNT; j++){
if(alien_bullets[j] == nullptr && aliens_tab[i] != nullptr && !(gb.frameCount % ALIEN_FIRE_SPEED)){
alien_bullets[j] = aliens_tab[i] -> alien_fire();
break;
}
}
}
//move alien bullets and/or delete bullets
for (int i = 0; i < ALIEN_BULLETS_COUNT; i++){
if (alien_bullets[i] != nullptr)
alien_bullets[i] -> move_bullet();
if (alien_bullets[i] != nullptr && alien_bullets[i] -> bullet_out()){
delete alien_bullets[i];
alien_bullets[i] = nullptr;
}
}
//alien - bullet collision
for (int i = 0; i < ALIENS_COUNT; i++){
for (int j = 0; j < PLAYER_BULLETS_COUNT; j++){
if ((aliens_tab[i] != nullptr && aliens_tab[i] -> alien_collision(player_bullets[j] -> get_x(), player_bullets[j] -> get_y(), &player_bullets[j], score))){ //check if &player_bullets[j] can be replaced with player_bullets[j]
delete aliens_tab[i];
aliens_tab[i] = nullptr;
}
}
}
#if(CLOUDS)
//__________CLOUDS__________
//create clouds
if(!(gb.frameCount % 10)){
for (int i = 0; i < CLOUDS_COUNT; i++){
if (clouds_tab[i] == nullptr){
clouds_tab[i] = new clouds(random(1,4));
break;
}
}
}
//move and/or delete clouds
for (int i = 0; i < CLOUDS_COUNT; i++){
if (clouds_tab[i] != nullptr)
clouds_tab[i] -> move_cloud();
if (clouds_tab[i] != nullptr && clouds_tab[i] -> cloud_out()){
delete clouds_tab[i];
clouds_tab[i] = nullptr;
}
}
#endif
if (record < score) record = score;
} //if(ship_lives) END
//reset game
if(ship_lives == 0){
while(time_after_death-- > 8){ //after death pause game for some time
while(!gb.update());
gb.display.clear();
gb.display.cursorX = 27;
gb.display.cursorY = 18;
gb.display.println(F("You lost"));
gb.display.cursorX = 40;
gb.display.println(time_after_death / 20);
}
gb.display.cursorX = 5;
gb.display.cursorY = 12;
gb.display.println(F("Do you want to"));
gb.display.cursorX = 5;
gb.display.println(F("play again?"));
gb.display.cursorX = 5;
gb.display.println(F(" UP - yes"));
gb.display.cursorX = 5;
gb.display.println(F(" DOWN - no"));
if(gb.buttons.repeat(BTN_UP, 0)){
ship_lives = 3;
score = 0;
time_after_death = 20 * 4;
}
if(gb.buttons.repeat(BTN_DOWN, 0)){
ship_lives = 3;
score = 0;
time_after_death = 20 * 4;
gb.titleScreen(F("Ship shooter"));
}
} //reset game END
//DRAW
if(ship_lives){
gb.display.clear();
#if(TEST)
gb.display.println(gb.getFreeRam());
#endif
//display lives and score
if (gb.frameCount % 2){
//display ship lives
gb.display.cursorX = 0;
gb.display.cursorY = 0;
gb.display.print("Lives: ");
gb.display.print(ship_lives-1);
//display score
gb.display.cursorX = 0;
gb.display.cursorY = 37;
gb.display.print("Score: ");
gb.display.println(score);
gb.display.print("Record: ");
gb.display.println(record);
}
gb.display.drawBitmap(ship_x, ship_y, SHIP);
//draw player bullets
for (int i = 0; i < PLAYER_BULLETS_COUNT; i++){
if (player_bullets[i] != nullptr)
player_bullets[i] -> draw_bullet();
}
//draw alien
for (int i = 0; i < ALIENS_COUNT; i++){
if (aliens_tab[i] != nullptr)
aliens_tab[i] -> display_alien();
}
//draw alien bullets
for (int i = 0; i < ALIEN_BULLETS_COUNT; i++){
if (alien_bullets[i] != nullptr)
alien_bullets[i] -> draw_bullet();
}
#if(CLOUDS)
//draw clouds
for (int i = 0; i < CLOUDS_COUNT; i++){
if (clouds_tab[i] != nullptr)
clouds_tab[i] -> draw_cloud();
}
#endif
} //if(ship_lives) END in DRAW
}//gb.update END
}//loop() EN