-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpace_Invaders.ino
326 lines (274 loc) · 9.92 KB
/
Space_Invaders.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
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;
#define TEST 0 //if 1 game is in test mode - death is disabled
//I'm really sorry for this pointers, I know what I have created ... but I wanted to play with them a bit, at least it's faster :d
//84x48 resolution
//SHIP SETTINGS
const int SHIP_W = 5;
const int SHIP_H = 3;
const int SHIP_V = 1;
int ship_x = 38;
const int SHIP_y = LCDHEIGHT - 3;
int lifes = 3;
int gm = 0;
const int GM_DURATION = 2 * 20; //god mode counter, after 2s ship will be vulnerable; seconds * 20; seconds * (1000ms / 50ms); 50 because main loop works every 50ms
const byte ship[] PROGMEM = {8, 3,
B00100000,
B01110000,
B11111000,
};
//SHIP BULLET SETTINGS
const int BULLET_V = 2;
const int BULLETS_AMOUNT = 15;
int bullets[BULLETS_AMOUNT][2];
const int BULLET_DELAY_DURATION = 1 * 20; //ship will be able to shot every 1s; 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
//ALIEN SETTINGS
int alien_w = 8;
int alien_h = 8;
int alien_1_h = 8;
int alien_2_h = 8;
int alien_3_h = 8;
int alien_v = 1;
const int LEFT = 1; //alien movement
const int RIGHT = 2;
int direction = RIGHT;
const int ALIENS_AMOUNT = 7;
const int ALIENS_ROWS = 3;
int aliens_x[ALIENS_AMOUNT]; //set up in settings()
// int aliens_X[8] = {0, 9, 18, 27, 36, 45, 54, 63};
const int ALIENS_Y[3] = {1, 10, 19};
bool aliens[ALIENS_ROWS][ALIENS_AMOUNT]; //it will be true if alien exist or fale if alien is dead
int aliens_left = ALIENS_AMOUNT * ALIENS_ROWS;
//correction in aliens move - while first or last column is destroyed aliens will move further - to the edge of the screen
int x_correction_l = 0; //correction for the left side of the formation
int correction_l_count = 0; //how many columns of aliens from the left was destroyed
int x_correction_r = 0; //correction for the right side of the formation
int correction_r_count = 0; //how many columns of aliens from the left was destroyed
const byte ALIEN_1[] PROGMEM = {
8, 8,
B00011000,
B10111101,
B11111111,
B11111111,
B10011001,
B00011000,
B00111100,
B00011000,
};
const byte ALIEN_2[] PROGMEM = {8, 8,
B00011000,
B11011011,
B10011001,
B10111101,
B11111111,
B00111100,
B00111100,
B00011000,
};
const byte ALIEN_3[] PROGMEM = {8, 8,
B00111100,
B01111110,
B11111111,
B01111110,
B00111100,
B00111100,
B00011000,
B00011000,
};
//ALIEN BULLETS SETTINGS
const int A_BULLET_V = 2;
const int A_BULLETS_AMOUNT = 4;
int a_bullets[A_BULLETS_AMOUNT][2];
//OTHER
const int RESET_BULLET = -7;
void draw_bullets(const int &BULLETS_AMOUNT, int bullets_table[][2]);
void (*fun)(const int &BULLETS_AMOUNT, int bullets_table[][2]);
int col = 0; //it will be used in shooting section, to randomize which alien collumn supposed to shoot
void setup(){
gb.begin();
gb.titleScreen(F("Space_Invaders"));
gb.display.persistence = false;
for(int i = 0; i < ALIENS_ROWS; i++) //set up 3 rows of aliens; it will be true if alien exist or fale if alien is dead
for(int j = 0; j < ALIENS_AMOUNT; j++)
*(*(aliens + i) + j) = true;
for (int i = 0; i < ALIENS_AMOUNT; i++) //set up aliens starting x position if alien_w = 8 -> 0, 9, 18, 27
*(aliens_x + i) = i * (alien_w + 1);
for (int i = 0; i < BULLETS_AMOUNT; i++) // reset ship bulets
*(*(bullets + i)) = RESET_BULLET;
for (int i = 0; i < A_BULLETS_AMOUNT; i++) // reset alien bulets
*(*(a_bullets + i)) = RESET_BULLET;
fun = draw_bullets;
}
void loop(){
while (gb.update()){ //returns true every 50ms; 20fps
//INPUT
//go to title screen
if(gb.buttons.repeat(BTN_C, 0)) gb.titleScreen(F("Space Invaders"));
//move ship left/right
if(gb.buttons.repeat(BTN_LEFT, 0) && ship_x > 0) //move ship
ship_x -= SHIP_V;
else if(gb.buttons.repeat(BTN_RIGHT, 0) && ship_x < LCDWIDTH - SHIP_W)
ship_x += SHIP_V;
//ship fire
if(bullet_delay_count < BULLET_DELAY_DURATION) bullet_delay_count++;
if(gb.buttons.pressed(BTN_A)){ // create bullet start coords
if(bullet_delay_count == BULLET_DELAY_DURATION){
bullet_delay_count = 0;
for (int i = 0; i < BULLETS_AMOUNT; i++){
if (*(*(bullets + i)) == RESET_BULLET){
*(*(bullets + i)) = ship_x + 3;
*(*(bullets + i) + 1) = LCDHEIGHT - 2;
break;
}
}
}
}
//LOGIC
//win / lose; check if win if so go to title screen and reset some values
if (aliens_left <= 0 || lifes <= 0){
delay(1000); //wait 1 seconds to give some time to see what happened and then reset the game
ship_x = 38;
lifes = 3;
gm = 0;
direction = RIGHT;
aliens_left = ALIENS_AMOUNT * ALIENS_ROWS;
x_correction_l = 0;
correction_l_count = 0;
x_correction_r = 0;
correction_r_count = 0;
for(int i = 0; i < ALIENS_ROWS; i++) //set up 3 rows of aliens; it will be true if alien exist or fale if alien is dead
for(int j = 0; j < ALIENS_AMOUNT; j++)
*(*(aliens + i) + j) = true;
for (int i = 0; i < ALIENS_AMOUNT; i++) //set up aliens starting x position if alien_w = 8 -> 0, 9, 18, 27
*(aliens_x + i) = i * (alien_w + 1);
for (int i = 0; i < BULLETS_AMOUNT; i++) //reset ship bullets
*(*(bullets + i)) = RESET_BULLET;
for (int i = 0; i < A_BULLETS_AMOUNT; i++) // reset alien bulets
*(*(a_bullets + i)) = RESET_BULLET;
gb.titleScreen(F("Space Invaders"));
}
//move aliens left/right
//section 1 - if first or last coumn of aliens is destroyed this will add correction to aliens movement to move aliens formation to the edge of the screen
if ( *(*(aliens) + correction_l_count) == false && *(*(aliens + 1) + correction_l_count) == false && *(*(aliens + 2) + correction_l_count) == false){
x_correction_l = (correction_l_count + 1) * (alien_w + 1);
correction_l_count += 1;
}
if ( *(*(aliens) + (ALIENS_AMOUNT - 1) - correction_r_count) == false && *(*(aliens + 1) + (ALIENS_AMOUNT - 1) - correction_r_count) == false && *(*(aliens + 2) + (ALIENS_AMOUNT - 1) - correction_r_count) == false){
x_correction_r = (correction_r_count + 1) * (alien_w + 1);
correction_r_count += 1;
}
//section 2 - alien movement; just moves aliens left - right
switch(direction){
case RIGHT:
for (int i = 0; i < ALIENS_AMOUNT; i++)
*(aliens_x + i) += alien_v; //move aliens
if (*aliens_x >= (LCDWIDTH - (ALIENS_AMOUNT * (alien_w + 1) - 1)) + x_correction_r) //change aliens direction
direction = LEFT;
break;
case LEFT:
for (int i = 0; i < ALIENS_AMOUNT; i++)
*(aliens_x + i) -= alien_v;
if(*aliens_x < 1 - x_correction_l)
direction = RIGHT;
break;
}
//alien shoot
//if ( *(*(alien_bullets)) >= LCDHEIGHT) *(*(alien_bullets)) = RESET_BULLET
for (int j = 2; j >= 0; j--){
for (int i = 0; i < A_BULLETS_AMOUNT; i++){
col = random(0, ALIENS_AMOUNT);
if ( ( *(*(aliens + j) + col) == true ) && ( *(*(a_bullets + i)) == RESET_BULLET) ) {
*(*(a_bullets + i)) = *(aliens_x + col) + 4;
*(*(a_bullets + i) + 1) = (j + 1) * 8;
}
}
}
//move ship bullets up, and/or delete bullet
for (int i = 0; i < BULLETS_AMOUNT; i++){
if ( *(*(bullets + i)) != RESET_BULLET) //move
*(*(bullets + i) + 1) -= BULLET_V;
if ( *(*(bullets + i) + 1) < -3){ //delete bullet (it's off the screen)
*(*(bullets + i)) = RESET_BULLET;
*(*(bullets + i) + 1) = RESET_BULLET;
}
}
//move alien bullets down, and/or delete bullet
for (int i = 0; i < A_BULLETS_AMOUNT; i++){
if ( *(*(a_bullets + i)) != RESET_BULLET){
*(*(a_bullets + i) + 1) += A_BULLET_V;
}
if ( *(*(a_bullets + i) + 1) > LCDHEIGHT + 3){ //delete bullet (it's off the screen)
*(*(a_bullets + i)) = RESET_BULLET;
*(*(a_bullets + i) + 1) = RESET_BULLET;
}
}
//collision bullet - ship
#if(!TEST)
if (gm < GM_DURATION) gm++;
else{
for (int bx = 0; bx < A_BULLETS_AMOUNT; bx++){
if(gb.collideRectRect( *(*(a_bullets + bx)), *(*(a_bullets + bx) + 1), 2, 3, ship_x, SHIP_y, SHIP_W, SHIP_H)){
ship_x = LCDWIDTH + 3;
lifes -= 1;
gm = 0;
for (int i = 0; i < A_BULLETS_AMOUNT; i++){
*(*(a_bullets + i)) = RESET_BULLET;
*(*(a_bullets + i) + 1) = RESET_BULLET;
}
delay(1000);
ship_x = 38;
}
}
}
#endif
//collision bullet - alien
for (int i = 0; i < ALIENS_ROWS; i++){
for (int j = 0; j < ALIENS_AMOUNT; j++){
for (int b = 0; b < BULLETS_AMOUNT; b++){
if (*(*(aliens + i) + j) == true && gb.collideRectRect( *(aliens_x + j), *(ALIENS_Y + i), alien_w, alien_h, *(*(bullets + b)), *(*(bullets + b) + 1), 2, 3)){
gb.sound.playTick();
*(*(aliens + i ) + j) = false;
aliens_left -= 1;
*(*(bullets + b)) = RESET_BULLET;
*(*(bullets + b) + 1) = RESET_BULLET;
}
}
}
}
//DRAW
gb.display.clear();
//draw ship bullets
(*fun) (BULLETS_AMOUNT, bullets);
//draw aliens bullets
(*fun)(A_BULLETS_AMOUNT, a_bullets);
//draw ship
gb.display.drawBitmap(ship_x, SHIP_y, ship);
//draw aliens
for (int i = 0; i < ALIENS_ROWS; i++){
for (int j = 0; j < ALIENS_AMOUNT; j++){
if( *(*(aliens + i)+j)){
switch (i){
case 0:
gb.display.drawBitmap(*(aliens_x + j), *(ALIENS_Y + i), ALIEN_1);
break;
case 1:
gb.display.drawBitmap(*(aliens_x + j), *(ALIENS_Y + i), ALIEN_2);
break;
case 2:
gb.display.drawBitmap(*(aliens_x + j), *(ALIENS_Y + i), ALIEN_3);
break;
} //switch
} //if
} //for_2
} //for_1 //Draw aliens END
} //while (gb.update()) END
} //void loop() END
// FUNCTIONS
void draw_bullets(const int &BULLETS_AMOUNT, int bullets_table[][2]){
for (int i = 0; i < BULLETS_AMOUNT; i++){
if (*(*(bullets_table + i)) != RESET_BULLET)
gb.display.fillRect( *(*(bullets_table + i)), *(*(bullets_table + i) + 1), 2, 3);
}
}