Okay, a very simple curses program here, the idea is loosely based on a faint memory I have of a type-in in basic that I typed in and played some years back.
Synopsis- your thing ('O') runs around the outside edge. When you press space it shoots one of it's bullets into the internal space. The object is to hit the 15 Xs on the inside. Eliminate the targets and get 15 more targets, 15 more bullets, and you go a little faster. If you can hit 2 with one bullet you've just saved yourself a bullet. If you beat level 10 you win the game.
So what's would you give for setting, story, and name of this game?
Code:#include <curses.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
int dirs[6] = {0, 1, 0, -1, 0, 1};
void init () {
srand (time(NULL));
initscr ();
raw (); nodelay(stdscr,1); noecho(); curs_set(0); nonl(); keypad(stdscr,1);
}
void play () {
int x, y, bx, by, c, d, in, bul, lvl, buf[LINES][COLS];
y = x = d = lvl = 0; bul = 10;
do {
bx = x; by = y;
clear ();
lvl++; bul += 15;
mvprintw (LINES - 1, 37, "Round %d", lvl);
for (y = 0; y < LINES; y++) for (x = 0; x < COLS; x++) buf[y][x] = 0;
for (c = 0; c < 15; c++) { /* Set up enemies. */
do {
y = 1 + rand () % (LINES - 3); x = 1 + rand () % (COLS - 2);
} while (buf[y][x]);
buf[y][x] = 1;
mvaddch (y, x, 'X');
}
x = bx; y = by;
mvaddch (y, x, 'O');
mvprintw (LINES - 1, 0, "Bullets : %d", bul);
mvprintw (LINES - 1, 60, "Press SPACE to fire.");
refresh ();
while ((bul > 0) && (c > 0)) {
in = getch ();
if (tolower(in) == 'q') return;
if (in == ' ') { /* shoot a bullet */
bul--;
mvprintw (LINES - 1, 0, "Bullets : %d ", bul);
bx = x; by = y;
do {
x += dirs[d + 1]; y += dirs[d + 2];
if (buf[y][x]) {
beep ();
buf[y][x] = 0;
c--;
}
else mvaddch (y, x, '.');
refresh ();
napms (15);
mvaddch (y, x, ' ');
} while (!((x == 0) + (y == 0) + (x == COLS - 1) + (y == LINES - 2)));
x = bx; y = by;
}
mvaddch (y, x, ' ');
x += dirs[d]; y += dirs[d + 1];
if (((x == 0) + (y == 0) + (x == COLS - 1) + (y == LINES - 2)) == 2) {
d ++; d %= 4;
}
mvaddch (y, x, 'O');
refresh ();
napms (275 - 25 * lvl);
} /* end of level loop. Either out of bullets or out of enemies. */
} while ((c == 0) && (lvl < 10));
if (c) mvprintw (10, 27, "Out of bullets. Game over.");
else mvprintw (10, 15, "Congratulations! You have defeated every enemy!");
refresh ();
}
int main () {
init ();
play ();
nodelay(stdscr,0); getch ();
endwin ();
return 0;
}