2 * nInvaders - a space invaders clone for ncurses
3 * Copyright (C) 2002-2003 Dettus
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * homepage: http://ninvaders.sourceforge.net
20 * mailto: ninvaders-devel@lists.sourceforge.net
28 #include "nInvaders.h"
37 int status
; // status handled in timer
40 #define GAME_NEXTLEVEL 2
44 #define GAME_HIGHSCORE 6
50 * initialize level: reset attributes of most units
52 static void initLevel()
64 * evaluate command line parameters
66 static void evaluateCommandLine(int argc
, char **argv
)
69 // -l : set skill level
70 if (argc
== 3 && strcmp(argv
[1], "-l") == 0) {
71 if (argv
[2][0] >= '0' && argv
[2][0] <= '9') {
72 skill_level
= argv
[2][0] - 48;
78 // -gpl : show GNU GPL
79 if (argc
== 2 && strcmp(argv
[1], "-gpl") == 0) {
83 // wrong command line: show usage
84 if (argc
== 2 || (argc
== 3 && strcmp(argv
[1], "-l") != 0)) {
92 static void finish(int sig
)
99 fprintf(stderr
,"=========================================================================\n");
100 fprintf(stderr
,"\n");
102 fprintf(stderr
,"Final score: %7.7ld, Final level: %2.2d\nFinal rating... ",score
,level
);
104 fprintf(stderr
,"Quitter\n\n");
106 fprintf(stderr
,"Alien Fodder\n\n");
108 fprintf(stderr
,"Easy Target\n\n");
110 fprintf(stderr
,"Barely Mediocre\n\n");
112 fprintf(stderr
,"Shows Promise\n\n");
114 fprintf(stderr
,"Alien Blaster\n\n");
116 fprintf(stderr
,"Earth Defender\n\n");
118 fprintf(stderr
,"Supreme Protector\n\n");
127 statusDisplay(level
, score
, lives
);
132 * reads input from keyboard and do action
139 ch
= getch(); // get key pressed
154 level
= 0; // reset level
155 score
= 0; // reset score
156 lives
= 3; // restore lives
157 status
= GAME_NEXTLEVEL
;
158 } else if (ch
== 'q') { // quit game
164 break; // don't do anything
168 if (ch
== 'l' || ch
== KEY_RIGHT
) { // move player right
169 if (lastmove
== 'l') {
170 playerTurboOn(); // enable Turbo
172 playerTurboOff(); // disable Turbo
174 playerMoveRight(); // move player
175 lastmove
= 'l'; // remember last move for turbo mode
176 } else if (ch
== 'h' || ch
== KEY_LEFT
) { // move player left
177 if (lastmove
== 'h') {
178 playerTurboOn(); // enable Turbo
180 playerTurboOff(); // disable Turbo
182 playerMoveLeft(); // move player
183 lastmove
= 'h'; // remember last move for turbo mode
184 } else if (ch
== 'k' || ch
== ' ') { // shoot missile
185 playerLaunchMissile();
186 } else if (ch
== 'p') { // pause game until 'p' pressed again
187 // set status to game paused
188 status
= GAME_PAUSED
;
189 } else if (ch
== 'W') { // cheat: goto next level
190 status
= GAME_NEXTLEVEL
;
191 } else if (ch
== 'L') { // cheat: one more live
194 } else if (ch
== 'q') { // quit game
196 } else { // disable turbo mode if key is not kept pressed
207 * this method is executed every 1 / FPS seconds
211 static int aliens_move_counter
= 0;
212 static int aliens_shot_counter
= 0;
213 static int player_shot_counter
= 0;
214 static int ufo_move_counter
= 0;
215 static int title_animation_counter
= 0;
216 static int game_over_counter
= 0;
220 case GAME_NEXTLEVEL
: // go to next level
222 level
++; // increase level
224 initLevel(); // initialize level
226 aliens_move_counter
= 0;
227 aliens_shot_counter
= 0;
228 player_shot_counter
= 0;
229 ufo_move_counter
= 0;
231 weite
= (shipnum
+(skill_level
*10)-(level
*5)+5)/10;
237 // change status and start game!
240 case GAME_LOOP
: // do game handling
243 if (aliens_move_counter
== 0 && aliensMove() == 1) {
244 // aliens reached player
249 // move player missile
250 if (player_shot_counter
== 0 && playerMoveMissile() == 1) {
252 status
= GAME_NEXTLEVEL
;
255 // move aliens' missiles
256 if (aliens_shot_counter
== 0 && aliensMissileMove() == 1) {
258 lives
--; // player looses one life
259 drawscore(); // draw score
260 playerExplode(); // display some explosion graphics
261 if (lives
== 0) { // if no lives left ...
262 status
= GAME_OVER
; // ... exit game
267 if (ufo_move_counter
== 0 && ufoShowUfo() == 1) {
268 ufoMoveLeft(); // move it one position to the left
272 if (aliens_shot_counter
++ >= 5) {aliens_shot_counter
=0;} // speed of alien shot
273 if (player_shot_counter
++ >= 1) {player_shot_counter
=0;} // speed of player shot
274 if (aliens_move_counter
++ >= weite
) {aliens_move_counter
=0;} // speed of aliend
275 if (ufo_move_counter
++ >= 3) {ufo_move_counter
=0;} // speed of ufo
280 case GAME_PAUSED
: // game is paused
283 case GAME_OVER
: // game over
284 if (game_over_counter
== 100) {
286 status
= GAME_HIGHSCORE
;
287 game_over_counter
= 0;
294 case GAME_EXIT
: // exit game
298 case GAME_HIGHSCORE
: // display highscore
299 if (title_animation_counter
== 0) {
300 titleScreenDisplay();
303 if (title_animation_counter
++ >= 6) {title_animation_counter
= 0;} // speed of animation
315 struct itimerval myTimer
;
316 struct sigaction myAction
;
317 myTimer
.it_value
.tv_sec
= 0;
318 myTimer
.it_value
.tv_usec
= 1000000 / FPS
;
319 myTimer
.it_interval
.tv_sec
= 0;
320 myTimer
.it_interval
.tv_usec
= 1000000 / FPS
;
321 setitimer(ITIMER_REAL
, &myTimer
, NULL
);
323 myAction
.sa_handler
= &handleTimer
;
324 myAction
.sa_flags
= SA_RESTART
;
325 sigaction(SIGALRM
, &myAction
, NULL
);
329 int main(int argc
, char **argv
)
337 evaluateCommandLine(argc
, argv
); // evaluate command line parameters
338 graphicEngineInit(); // initialize graphic engine
340 // set up timer/ game handling
342 status
= GAME_HIGHSCORE
;
344 // read keyboard input
346 // do movements and key-checking
354 void doScoring(int alienType
)
356 int points
[4] = {500, 200, 150, 100}; // 0: ufo, 1:red, 2:green, 3:blue
358 score
+= points
[alienType
]; // every alien type does different scoring points
360 // every 6000 pts player gets a new live
361 if (score
% 6000 == 0){
365 drawscore(); // display score
This page took 2.226413 seconds and 4 git commands to generate.