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
44 WINDOW
*wPlayerMissile
;
46 WINDOW
*wAliensMissile
;
54 * initialize player sprites
56 static void playerInit()
58 wPlayer
= newpad(1, PLAYERWIDTH
); // new pad with appropriate size
59 wclear(wPlayer
); // clear pad
60 wattrset(wPlayer
,COLOR_PAIR(YELLOW
)); // set color
61 waddstr(wPlayer
,"/-^-\\"); // set sprite
66 * display player sprite
68 void playerDisplay(int x
, int y
)
70 copywin(wPlayer
,wBattleField
,0,0,y
,x
,y
,x
+PLAYERWIDTH
-1,0);
77 void playerClear(int x
, int y
)
79 copywin(wEmpty
,wBattleField
,0,0,y
,x
,y
,x
+PLAYERWIDTH
-1,0);
84 * initialize missile sprites
86 static void playerMissileInit()
88 wPlayerMissile
= newpad(1, 1); // new pad with appropriate size
89 wclear(wPlayerMissile
); // clear pad
90 wattrset(wPlayerMissile
,COLOR_PAIR(WHITE
)); // set color
91 waddch(wPlayerMissile
,'!'); // set sprite
92 wrefresh(wPlayerMissile
);
97 * display missile sprite
99 void playerMissileDisplay(int x
, int y
)
101 copywin(wPlayerMissile
,wBattleField
,0,0,y
,x
,y
,x
,0);
106 * clear missile sprite
108 void playerMissileClear(int x
, int y
)
110 copywin(wEmpty
,wBattleField
,0,0,y
,x
,y
,x
,0);
115 * some explosion animation
117 void playerExplosionDisplay(int x
, int y
)
119 WINDOW
* wPlayerExplosion
;
120 char playerExplosionChars
[16+1]="@~`.,^#*-_=\\/%{}";
123 wPlayerExplosion
=newpad(1,PLAYERWIDTH
); // new pad
124 wattrset(wPlayerExplosion
,COLOR_PAIR(YELLOW
)); // set color
126 for(t
=0;t
<5;t
++){ // 5 frames
127 wclear(wPlayerExplosion
); // clear pad
128 for(s
=0;s
<PLAYERWIDTH
;s
++){
129 waddch(wPlayerExplosion
,playerExplosionChars
[rand()%16]); // sprite
132 copywin(wPlayerExplosion
,wBattleField
,0,0,y
,x
,y
,x
+PLAYERWIDTH
-1,0); // display explostion
133 wrefresh(wBattleField
); // refresh battelfield to display explosion frames
134 doSleep(100000); // play animation not too fast
138 } // todo: kann man bestimmt noch besser machen.
142 * initialize aliens sprites
144 static void aliensInit()
146 wAliens
= newpad(ALIENS_MAX_NUMBER_Y
*2,ALIENS_MAX_NUMBER_X
*3);
152 * display aliens sprite
154 void aliensDisplay(int x
, int y
, int wid
, int hgt
)
156 copywin(wAliens
,wBattleField
,0,0,y
,x
,y
+hgt
,x
+wid
+2,0);
161 * clear aliens sprite
163 void aliensClear(int x
, int y
, int wid
, int hgt
)
165 copywin(wEmpty
,wBattleField
,0,0,y
,x
,y
+hgt
,x
+wid
+2,0);
170 * initialize missile sprites
172 static void aliensMissileInit()
174 wAliensMissile
= newpad(1, 1); // new pad
175 wclear(wAliensMissile
); // clear pad
176 wattrset(wAliensMissile
, COLOR_PAIR(CYAN
)); // set color
177 waddch(wAliensMissile
, ':'); // set sprite
182 * display missile sprite
184 void aliensMissileDisplay(int x
, int y
)
186 copywin(wAliensMissile
,wBattleField
,0,0,y
,x
,y
,x
,0);
191 * clear missile sprite
193 void aliensMissileClear(int x
, int y
)
195 copywin(wEmpty
,wBattleField
,0,0,y
,x
,y
,x
,0);
200 * refresh aliens sprite
202 void aliensRefresh(int level
, int *pAliens
)
204 static int frame
= 0; // used for animation; mod 2 == 0: frame1, mod2 == 1: frame2
208 char ships
[2][9][3+1] = {
209 {",^,", "_O-", "-o-", "o=o", "<O>", "_x_", "*^*", "\\_/", "o o"},
210 {".-.", "-O_", "/o\\", "o-o", "<o>", "-x-", "o^o", "/~\\", "oo "}
212 int colors
[9] = {RED
, GREEN
, BLUE
, RED
, YELLOW
, WHITE
, WHITE
, YELLOW
, RED
};
214 wclear(wAliens
); // clear pad
215 wattrset(wAliens
,COLOR_PAIR(RED
)); // set color
217 frame
++; // next frame
219 // draw alien if there is one
220 for (row
= 0; row
< ALIENS_MAX_NUMBER_Y
*2; row
++) {
221 for (k
= 0; k
< ALIENS_MAX_NUMBER_X
; k
++) {
222 if ((row
% 2) == 0) { // display aliens every even row
223 alienType
= *(pAliens
+ c
* (ALIENS_MAX_NUMBER_X
) + k
); // get type of alien //alienBlock[c][k]
225 if (alienType
!= 0) { // if there is an alien to display
226 wattrset(wAliens
,COLOR_PAIR(colors
[alienType
-1])); // set color
227 waddch(wAliens
,ships
[frame
%2][alienType
-1+(3*((level
-1)%3))][0]); // set char1
228 waddch(wAliens
,ships
[frame
%2][alienType
-1+(3*((level
-1)%3))][1]); // set char2
229 waddch(wAliens
,ships
[frame
%2][alienType
-1+(3*((level
-1)%3))][2]); // set char3
231 *(pAliens
+ c
* ALIENS_MAX_NUMBER_X
+ k
) = (alienType
+ 1) % 9;
232 } // todo: what's that? If alien_type > 4 then do a modulo operation???
234 waddstr(wAliens
," "); // no alien
238 waddstr(wAliens
," "); // no aliens at odd rows
241 if ((row
% 2) == 1) {c
++;} // goto next row at alienblock
247 * initialize bunkers sprites
249 static void bunkersInit()
251 wBunkers
= newpad(BUNKERHEIGHT
, BUNKERWIDTH
); // new pad data
257 * display bunkers sprite
258 * needs pointer to bunker-array
260 void bunkersDisplay(int *pBunker
)
264 wattrset(wBunkers
,COLOR_PAIR(CYAN
));
265 for (l
=0;l
<BUNKERHEIGHT
;l
++) {
266 for (k
=0;k
<BUNKERWIDTH
;k
++) {
267 if (*(pBunker
+ (l
* (BUNKERWIDTH
+ 1)) + k
) == 1) { //if (pBunker[l][k]==1) {
268 waddch(wBunkers
,'#');
270 waddch(wBunkers
,' ');
275 copywin(wBunkers
, wBattleField
, 0, 0, BUNKERY
, BUNKERX
, BUNKERY
+ BUNKERHEIGHT
- 1, BUNKERX
+ BUNKERWIDTH
- 1, 0);
280 * clear bunkers sprite
284 copywin(wEmpty
, wBattleField
, 0, 0, BUNKERY
, BUNKERX
, BUNKERY
+ BUNKERHEIGHT
- 1, BUNKERX
+ BUNKERWIDTH
- 1, 0);
289 * clear one element of bunkers sprite at position (x, y)
291 void bunkersClearElement(int x
, int y
)
293 copywin(wEmpty
, wBattleField
, 0, 0, y
, x
, y
, x
, 0);
298 * set actual sprite for ufo animation
302 char ufo
[4][6] = {"<o o>", "<oo >", "<o o>", "< oo>"};
303 static int frame
= 0;
306 wattrset(wUfo
, COLOR_PAIR(MAGENTA
));
307 waddstr(wUfo
, ufo
[frame
% 4]);
314 * initialize ufo sprite
316 static void ufoInit()
318 wUfo
= newpad(1, UFOWIDTH
); // new pad with appropriate size
319 wclear(wUfo
); // clear pad
320 wattrset(wUfo
, COLOR_PAIR(MAGENTA
)); // set color
327 void ufoDisplay(int x
, int y
)
329 copywin(wUfo
, wBattleField
, 0, 0, y
, x
, y
, x
+ UFOWIDTH
- 1, 0);
336 void ufoClear(int x
, int y
)
338 copywin(wEmpty
, wBattleField
, 0, 0, y
, x
, y
, x
+ UFOWIDTH
- 1, 0);
343 * initialize gameover graphic
345 static void gameOverInit()
347 // init game-over banner
348 wGameOver
= newpad(13, 31);
350 wattrset(wGameOver
, COLOR_PAIR(WHITE
));
351 waddstr(wGameOver
, " ");
352 waddstr(wGameOver
, " ##### #### ## ## ###### ");
353 waddstr(wGameOver
, " ## ## ## ####### ## ");
354 waddstr(wGameOver
, " ## ### ###### ## # ## ##### ");
355 waddstr(wGameOver
, " ## ## ## ## ## ## ## ");
356 waddstr(wGameOver
, " ##### ## ## ## ## ###### ");
357 waddstr(wGameOver
, " ");
358 waddstr(wGameOver
, " #### ## ## ###### ###### ");
359 waddstr(wGameOver
, " ## ## ## ## ## ## ## ");
360 waddstr(wGameOver
, " ## ## ## ## ##### ###### ");
361 waddstr(wGameOver
, " ## ## ## ## ## ## ## ");
362 waddstr(wGameOver
, " #### ### ###### ## ## ");
363 waddstr(wGameOver
, " ");
367 * display game over graphic
369 void gameOverDisplay()
371 int x
= (SCREENWIDTH
/ 2) - (31 / 2);
372 int y
= (SCREENHEIGHT
/ 2) - (13 / 2);
373 copywin(wGameOver
, wBattleField
, 0, 0, y
, x
, y
+ 12, x
+ 30, 0);
374 wrefresh(wBattleField
);
379 * initialize title screen
381 static void titleScreenInit()
383 wTitleScreen
= newpad(SCREENHEIGHT
, SCREENWIDTH
);
384 wclear(wTitleScreen
);
389 * display title screen
391 void titleScreenDisplay()
393 static int frame
= 0;
399 char ufo
[4][6] = {"<o o>", "<oo >", "<o o>", "< oo>"};
400 char aliens
[2][9][3+1] = {
401 {",^,", "_O-", "-o-", "o=o", "<O>", "_x_", "*^*", "\\_/", "o o"},
402 {".-.", "-O_", "/o\\", "o-o", "<o>", "-x-", "o^o", "/~\\", "oo "}
404 int score
[3] = {200, 150, 100};
405 int colors
[9] = {RED
, GREEN
, BLUE
, RED
, GREEN
, BLUE
, RED
, GREEN
, BLUE
};
407 static int alien_type
= 0;
409 wTitleText
= newpad(4, 41);
411 wattrset(wTitleText
, COLOR_PAIR(YELLOW
));
412 waddstr(wTitleText
, " ____ __ ");
413 waddstr(wTitleText
, " ___ / _/__ _ _____ ___/ /__ _______");
414 waddstr(wTitleText
, " / _ \\_/ // _ \\ |/ / _ `/ _ / -_) __(_-<");
415 waddstr(wTitleText
, "/_//_/___/_//_/___/\\_,_/\\_,_/\\__/_/ /___/");
418 wAliens
= newpad(7, 11);
420 snprintf(buffer
, sizeof(buffer
),"%s = 500", ufo
[frame
% 4]);
421 wattrset(wAliens
, COLOR_PAIR(MAGENTA
));
422 waddstr(wAliens
, buffer
);
423 if ((frame
= frame
% 60) == 0) {
425 } else if (frame
== 20) {
427 } else if (frame
== 40) {
430 for (i
= alien_type
; i
< alien_type
+ 3; i
++) {
431 waddstr(wAliens
, " ");
432 snprintf(buffer
, sizeof(buffer
), "%s = %d", aliens
[frame
% 2][i
], score
[i
% 3]);
433 wattrset(wAliens
, COLOR_PAIR(colors
[i
]));
434 waddstr(wAliens
, buffer
);
437 wStartText
= newpad(1, 20);
439 wattrset(wStartText
, COLOR_PAIR(RED
));
440 waddstr(wStartText
, "Press SPACE to start");
442 x
= (SCREENWIDTH
/ 2) - (41 / 2);
444 copywin(wTitleText
, wTitleScreen
, 0, 0, y
, x
, y
+ 3, x
+ 40, 0);
446 x
= (SCREENWIDTH
/ 2) - (11 / 2);
448 copywin(wAliens
, wTitleScreen
, 0, 0, y
, x
, y
+ 6, x
+ 10, 0);
450 x
= (SCREENWIDTH
/ 2) - (20 / 2);
451 y
= SCREENHEIGHT
- 2;
452 copywin(wStartText
, wTitleScreen
, 0, 0, y
, x
, y
, x
+ 19, 0);
454 copywin(wTitleScreen
, wBattleField
, 0, 0, 0, 0, SCREENHEIGHT
-1, SCREENWIDTH
-1, 0);
456 wrefresh(wBattleField
);
463 void titleScreenClear()
474 wStatus
= newpad(1, 55);
482 void statusDisplay(int level
, int score
, int lives
)
486 // "Level: 01 Score: 0001450 Lives: /-\ /-\ /-\ /-\ /-\ "
487 // "1234567890123456789012345678901234567890123456789012"
490 xOffset
= (SCREENWIDTH
/ 2) - 24;
494 sprintf (strStatus
, "Level: %2.2d Score: %2.7d Lives: ", level
, score
);
497 wattrset(wStatus
, COLOR_PAIR(RED
));
498 waddstr(wStatus
, strStatus
);
500 // show maximal five lives
501 for (t
= 1; ((t
<= 5) && (t
< lives
)); t
++){
502 waddstr(wStatus
, "/-\\ ");
505 copywin(wStatus
, wBattleField
, 0, 0, SCREENHEIGHT
-1, xOffset
, SCREENHEIGHT
-1, xOffset
+ 54, 0);
512 * initialize battlefield
514 static void battleFieldInit()
516 wEmpty
= newpad(SCREENHEIGHT
, SCREENWIDTH
);
519 wBattleField
= newwin(SCREENHEIGHT
, SCREENWIDTH
, 0, 0); // new window
520 wclear(wBattleField
); // clear window
521 mvwin(wBattleField
, 0, 0); // move window
528 void battleFieldClear()
530 copywin(wEmpty
,wBattleField
,0,0,0,0,SCREENHEIGHT
-1,SCREENWIDTH
-1,0);
535 * refresh screen so that modified graphic buffers get visible
539 redrawwin(wBattleField
); // needed to display graphics properly at startup on some terminals
540 wrefresh(wBattleField
);
548 static void finish(int sig
)
550 endwin(); // <curses.h> reset terminal into proper non-visual mode
556 * initialize n_courses
558 void graphicEngineInit()
560 (void) signal(SIGINT
, finish
); // <signal.h> on signal "SIGINT" call method "finish"
561 (void) initscr(); // <curses.h> do initialization work
562 keypad(stdscr
, TRUE
); // <curses.h> enable keypad for input
563 (void) nonl(); // <curses.h> disable translation return/ newline for detection of return key
564 (void) cbreak(); // <curses.h> do not buffer typed characters, use at once
565 (void) noecho(); // <curses.h> do not echo typed characters
566 start_color(); // <curses.h> use colors
567 init_pair(RED
, COLOR_RED
, COLOR_BLACK
); // <curses.h> define color-pair
568 init_pair(GREEN
, COLOR_GREEN
, COLOR_BLACK
); // <curses.h> define color-pair
569 init_pair(YELLOW
, COLOR_YELLOW
, COLOR_BLACK
); // <curses.h> define color-pair
570 init_pair(BLUE
, COLOR_BLUE
, COLOR_BLACK
); // <curses.h> define color-pair
571 init_pair(CYAN
, COLOR_CYAN
, COLOR_BLACK
); // <curses.h> define color-pair
572 init_pair(MAGENTA
, COLOR_MAGENTA
, COLOR_BLACK
); // <curses.h> define color-pair
573 init_pair(WHITE
, COLOR_WHITE
, COLOR_BLACK
); // <curses.h> define color-pair
575 //timeout(0); // <curses.h> do not wait for input
577 // initialize sprites
This page took 1.44144 seconds and 4 git commands to generate.