Created
June 10, 2016 01:08
-
-
Save jkwill87/02209781074873b15ef073367eadccc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <ncurses.h> | |
#include <time.h> | |
#include <unistd.h> | |
/******************************************************************************* | |
* | |
* ncurses-frogger.c: | |
* a quick frogger mockup which demonstrates some of the basic functionality | |
* and features of ncurses. | |
* | |
* Created: Friday, February 11, 2016 | |
* Author: Jessy Williams-Rancourt | |
* Contact: jessy@jessywilliams.com | |
* | |
******************************************************************************/ | |
#define NCARS 20 // number of random cars to generate | |
#define COLS 49 | |
#define ROWS 16 | |
/* Stuctures */ | |
typedef struct car { | |
int x; | |
int y; | |
char c; | |
} car; | |
typedef enum status { | |
IN_PROGRESS = 0, | |
QUIT, | |
WON, | |
LOST | |
} status; | |
int main() { | |
int x_max, x_cur, y_max, y_cur; | |
char c; | |
int level = 1; | |
car cars[NCARS]; | |
status win_condition; | |
srand (time (NULL)); | |
/* Set up ncruses */ | |
initscr(); | |
timeout (50); | |
// nodelay (stdscr, TRUE); | |
noecho(); | |
curs_set (false); | |
getmaxyx (stdscr, y_max, x_max); | |
do { | |
/* Print Border Around Sreen */ | |
border (' ', ' ', '-', '^', ' ', ' ', ' ', ' '); | |
mvprintw (0, x_max / 2 - 7, "CIS2500 FROGGER"); | |
refresh(); | |
/* (Re)set win condition */ | |
win_condition = IN_PROGRESS; | |
/* Create random cars */ | |
for (int i = 0; i < NCARS; i++) { | |
cars[i].x = rand() % (x_max - 2) + 1; | |
cars[i].y = rand() % (y_max - 10) + 1; | |
cars[i].c = 'A' + (rand() % 26); | |
} | |
/* Set starting postion of frog */ | |
y_cur = y_max - 5; | |
x_cur = x_max / 2; | |
move (y_max - 5, x_max / 2); | |
/* Begin game loop */ | |
while (!win_condition) { | |
c = getch(); | |
mvaddch (y_cur, x_cur, ' '); // clear old postition | |
switch (c) { | |
case 'w': | |
move (--y_cur, x_cur); | |
break; | |
case 'a': | |
move (y_cur, --x_cur); | |
break; | |
case 's': | |
move (++y_cur, x_cur); | |
break; | |
case 'd': | |
move (y_cur, ++x_cur); | |
break; | |
case 'q': | |
win_condition = QUIT; | |
} | |
mvaddch (y_cur, x_cur, '@'); | |
if (y_cur == 0) { | |
win_condition = WON; | |
break; | |
} | |
/* Move cars */ | |
for (int i = 0; i < NCARS; i++) { | |
mvaddch (cars[i].y, cars[i].x - 1, ' '); | |
if (cars[i].x >= x_max - 2) cars[i].x = 1; | |
mvaddch (cars[i].y, cars[i].x++, cars[i].c); | |
if (y_cur == cars[i].y && x_cur == cars[i].x) | |
win_condition = LOST; | |
} | |
refresh(); // update screen | |
// /* Base movement off of level */ | |
// nanosleep ((const struct timespec[]) { | |
// {0, 20000000L * (1 - level * 0.05)} | |
// }, NULL); // 25ms | |
} | |
/* Print Win Condition */ | |
if (win_condition == WON) printw ("You won!"); | |
else if (win_condition == LOST) printw ("You lost!"); | |
refresh(); | |
sleep (1); | |
level++; | |
clear(); // reset screen | |
} while (win_condition != QUIT); | |
endwin(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment