Created
September 10, 2016 02:01
-
-
Save luizperes/7a4b7d8e43a025864bac19f5fa1c9ba1 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 <ncurses.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
typedef char sprite[8]; | |
typedef struct | |
{ | |
sprite *s; | |
int x; | |
int y; | |
} Object; | |
void init(); | |
void run(); | |
void deinit(); | |
int main() | |
{ | |
init(); | |
run(); | |
deinit(); | |
return 0; | |
} | |
void run() | |
{ | |
int max_x, max_y; | |
getmaxyx(stdscr, max_x, max_y); | |
sprite s_spaceship = { | |
0b00000, | |
0b00000, | |
0b10000, | |
0b11000, | |
0b11100, | |
0b11000, | |
0b10000, | |
0b00000 | |
}; | |
Object spaceship; | |
spaceship.x = 3; | |
spaceship.y = 3; | |
spaceship.s = &s_spaceship; | |
int c; | |
while(true) | |
{ | |
usleep(100000); | |
clear(); | |
timeout(1); | |
c = wgetch(stdscr); | |
switch(c) | |
{ | |
case KEY_LEFT: spaceship.x--; break; | |
case KEY_RIGHT: spaceship.x++; break; | |
case KEY_UP: spaceship.y--; break; | |
case KEY_DOWN: spaceship.y++; break; | |
default: break; | |
} | |
for (int row = 0; row < 8; row++) { | |
for(char bit = 0; bit < 5; bit++) { | |
if (((*spaceship.s)[row] >> bit) & 1) { | |
mvprintw(spaceship.y + row, spaceship.x + 5 - bit, "*"); | |
} | |
} | |
} | |
refresh(); | |
} | |
} | |
void init() | |
{ | |
initscr(); /* initialize the curses library */ | |
keypad(stdscr, TRUE); | |
nodelay(stdscr, TRUE); | |
nonl(); | |
cbreak(); | |
noecho(); | |
curs_set(FALSE); | |
} | |
void deinit() | |
{ | |
clrtoeol(); | |
refresh(); | |
endwin(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment