-
Notifications
You must be signed in to change notification settings - Fork 7
/
invaders.c
498 lines (437 loc) · 12.1 KB
/
invaders.c
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* Space Invaders
Timothy Pettit
This program is modeled after the 80's arcade classic Space Invaders. A "tank" moves across
the bottom of the screen and shoots at "aliens" that move across and down the screen while
dropping bombs. The game is lost if the tank is hit by a bomb or an alien reaches
the bottom of the screen. The game is won by shooting down all of the aliens. This game
also features a menu which allows the user to adjust the overall speed of the game as well
as the relative speeds of the individual elements.
*/
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <time.h>
#include "invaderstructs.h"
#define MAX_BOMBS 1000
void menu(struct options *settings);
void gameover(int win);
/* The main function handles user input, the game visuals, and checks for win/loss conditions */
int main() {
struct player tank;
struct alien aliens[30];
struct shoot shot[3];
struct bomb bomb[MAX_BOMBS];
struct options settings;
unsigned int input, loops=0, i=0, j=0, currentshots=0, currentbombs=0, currentaliens=30;
int random=0, score=0, win=-1;
char tellscore[30];
initscr();
clear();
noecho();
cbreak();
nodelay(stdscr,1);
keypad(stdscr,1);
srand(time(NULL));
/* Set default settings */
settings.overall = 15000;
settings.alien = 12;
settings.shots = 3;
settings.bombs = 10;
settings.bombchance = 5;
/* Set tank settings */
tank.r = LINES - 1;
tank.c = COLS / 2;
tank.ch = '^';
/* Set aliens settings */
for (i=0; i<10; ++i) {
aliens[i].r = 1;
aliens[i].c = i*3;
aliens[i].pr = 0;
aliens[i].pc = 0;
aliens[i].ch = '#';
aliens[i].alive = 1;
aliens[i].direction = 'r';
}
for (i=10; i<20; ++i) {
aliens[i].r = 2;
aliens[i].c = (i-10)*3;
aliens[i].pr = 0;
aliens[i].pc = 0;
aliens[i].ch = '#';
aliens[i].alive = 1;
aliens[i].direction = 'r';
}
for (i=20; i<30; ++i) {
aliens[i].r = 3;
aliens[i].c = (i-20)*3;
aliens[i].pr = 0;
aliens[i].pc = 0;
aliens[i].ch = '#';
aliens[i].alive = 1;
aliens[i].direction = 'r';
}
/* Set shot settings */
for (i=0; i<3; ++i) {
shot[i].active = 0;
shot[i].ch = '*';
}
/* Set bomb settings */
for (i=0; i<MAX_BOMBS; ++i) {
bomb[i].active = 0;
bomb[i].ch = 'o';
bomb[i].loop = 0;
}
/* Display game title,score header,options */
move(0,(COLS/2)-9);
addstr("--SPACE INVADERS--");
move (0,1);
addstr("SCORE: ");
move(0,COLS-19);
addstr("m = menu q = quit");
while(1) {
/* Show score */
sprintf(tellscore, "%d", score);
move(0,8);
addstr(tellscore);
/* Move tank */
move(tank.r,tank.c);
addch(tank.ch);
/* Move bombs */
if (loops % settings.bombs == 0)
for (i=0; i<MAX_BOMBS; ++i) {
if (bomb[i].active == 1) {
if (bomb[i].r < LINES) {
if (bomb[i].loop != 0) {
move(bomb[i].r-1,bomb[i].c);
addch(' ');
}
else
++bomb[i].loop;
move(bomb[i].r,bomb[i].c);
addch(bomb[i].ch);
++bomb[i].r;
}
else {
bomb[i].active = 0;
bomb[i].loop = 0;
--currentbombs;
move(bomb[i].r - 1,bomb[i].c);
addch(' ');
}
}
}
/* Move shots */
if (loops % settings.shots == 0)
for (i=0; i<3; ++i) {
if (shot[i].active == 1) {
if (shot[i].r > 0) {
if (shot[i].r < LINES - 2) {
move(shot[i].r + 1,shot[i].c);
addch(' ');
}
for (j=0; j<30; ++j) {
if (aliens[j].alive == 1 && aliens[j].r == shot[i].r && aliens[j].pc == shot[i].c) {
score += 20;
aliens[j].alive = 0;
shot[i].active = 0;
--currentshots;
--currentaliens;
move(aliens[j].pr,aliens[j].pc);
addch(' ');
break;
}
}
if (shot[i].active == 1) {
move(shot[i].r,shot[i].c);
addch(shot[i].ch);
--shot[i].r;
}
}
else {
shot[i].active = 0;
--currentshots;
move(shot[i].r + 1,shot[i].c);
addch(' ');
}
}
}
/* Move aliens */
if (loops % settings.alien == 0)
for (i=0; i<30; ++i) {
if (aliens[i].alive == 1) {
move(aliens[i].pr,aliens[i].pc);
addch(' ');
move(aliens[i].r,aliens[i].c);
addch(aliens[i].ch);
aliens[i].pr = aliens[i].r;
aliens[i].pc = aliens[i].c;
/* Check if alien should drop bomb */
random = 1+(rand()%100);
if ((settings.bombchance - random) >= 0 && currentbombs < MAX_BOMBS) {
for (j=0; j<MAX_BOMBS; ++j) {
if (bomb[j].active == 0) {
bomb[j].active = 1;
++currentbombs;
bomb[j].r = aliens[i].r + 1;
bomb[j].c = aliens[i].c;
break;
}
}
}
/* Set alien's next position */
if (aliens[i].direction == 'l')
--aliens[i].c;
else if (aliens[i].direction == 'r')
++aliens[i].c;
/* Check alien's next positions */
if (aliens[i].c == COLS - 2) {
++aliens[i].r;
aliens[i].direction = 'l';
}
else if (aliens[i].c == 0) {
++aliens[i].r;
aliens[i].direction = 'r';
}
}
}
/* See if game has been won or lost*/
if (currentaliens == 0) {
win = 1;
break;
}
for (i=0; i<30; ++i) {
if (aliens[i].r == LINES-1) {
win = 0;
break;
}
}
for (i=0; i<MAX_BOMBS; ++i) {
if (bomb[i].r == tank.r && bomb[i].c == tank.c) {
win = 0;
break;
}
}
move(0,COLS-1);
refresh();
usleep(settings.overall);
++loops;
input = getch();
move(tank.r,tank.c);
addch(' ');
/* Check input */
if (input == 'q')
win = 2;
else if (input == KEY_LEFT)
--tank.c;
else if (input == KEY_RIGHT)
++tank.c;
else if (input == ' ' && currentshots < 3) {
for (i=0; i<3; ++i) {
if (shot[i].active == 0) {
shot[i].active = 1;
++currentshots;
--score;
shot[i].r = LINES - 2;
shot[i].c = tank.c;
break;
}
}
}
else if (input == 'm')
menu(&settings);
if (win != -1)
break;
/* Check for valid tank position */
if (tank.c > COLS-2)
tank.c = COLS - 2;
else if (tank.c < 0)
tank.c = 0;
}
gameover(win);
endwin();
return 0;
}
/* This function handles the menu options available to the user */
void menu(struct options *settings) {
char option, buf[30];
int new;
clear();
echo();
nocbreak();
nodelay(stdscr,0);
move(0,0);
addstr("1. Change overall game speed");
move(1,0);
addstr("2. Change alien motion speed");
move(2,0);
addstr("3. Change tank shot speed");
move(3,0);
addstr("4. Change alien bomb speed");
move(4,0);
addstr("5. Change alien bomb dropping frequency");
move(5,0);
addstr("6. Return to the game");
move(6,0);
addstr("7. Exit the game");
move(8,0);
addstr("Enter your option: ");
refresh();
while(1) {
move(8,19);
option = getch();
move(9,0);
deleteln();
move(10,0);
deleteln();
move(11,0);
deleteln();
if (option == '1') {
sprintf(buf,"Current value: %d\n", settings->overall);
move(9,0);
addstr(buf);
move(10,0);
addstr("Enter new value: ");
move(10,17);
refresh();
getch();
getstr(buf);
new = atoi(buf);
/* Check for valid new value */
if (new < 0) {
move(11,0);
addstr("ERROR: Inalid value");
}
else {
settings->overall = new;
}
}
else if (option == '2') {
sprintf(buf,"Current value: %d\n", settings->alien);
move(9,0);
addstr(buf);
move(10,0);
addstr("Enter new value: ");
move(10,17);
refresh();
getch();
getstr(buf);
new = atoi(buf);
/* Check for valid new value */
if (new <= 0) {
move(11,0);
addstr("ERROR: Inalid value");
}
else {
settings->alien = new;
}
}
else if (option == '3') {
sprintf(buf,"Current value: %d\n", settings->shots);
move(9,0);
addstr(buf);
move(10,0);
addstr("Enter new value: ");
move(10,17);
refresh();
getch();
getstr(buf);
new = atoi(buf);
/* Check for valid new value */
if (new <= 0) {
move(11,0);
addstr("ERROR: Inalid value");
}
else {
settings->shots = new;
}
}
else if (option == '4') {
sprintf(buf,"Current value: %d\n", settings->bombs);
move(9,0);
addstr(buf);
move(10,0);
addstr("Enter new value: ");
move(10,17);
refresh();
getch();
getstr(buf);
new = atoi(buf);
/* Check for valid new value */
if (new <= 0) {
move(11,0);
addstr("ERROR: Inalid value");
}
else {
settings->bombs = new;
}
}
else if (option == '5') {
sprintf(buf,"Current value: %d\n", settings->bombchance);
move(9,0);
addstr(buf);
move(10,0);
addstr("Enter new value: ");
move(10,17);
refresh();
getch();
getstr(buf);
new = atoi(buf);
/* Check for valid new value */
if (new > 100 || new < 0) {
move(11,0);
addstr("ERROR: Inalid value");
}
else {
settings->bombchance = new;
}
}
else if (option == '6') {
break;
}
else if (option == '7') {
endwin();
exit(0);
}
else {
move(9,0);
addstr("ERROR: Invalid selection");
move(8,19);
addstr(" ");
refresh();
}
}
clear();
noecho();
cbreak();
nodelay(stdscr,1);
move(0,(COLS/2)-9);
addstr("--SPACE INVADERS--");
move (0,1);
addstr("SCORE: ");
move(0,COLS-19);
addstr("m = menu q = quit");
}
/* This function handles displaying the win/lose screen */
void gameover(int win) {
nodelay(stdscr, 0);
if (win == 0) {
clear();
move((LINES/2)-1,(COLS/2)-5);
addstr("YOU LOSE!");
move((LINES/2),(COLS/2)-11);
addstr("PRESS ANY KEY TO EXIT");
move(0,COLS-1);
refresh();
getch();
}
else if (win == 1) {
clear();
move((LINES/2)-1,(COLS/2)-5);
addstr("YOU WIN!");
move((LINES/2),(COLS/2)-11);
addstr("PRESS ANY KEY TO EXIT");
move(0,COLS-1);
refresh();
getch();
}
}