Skip to content

Instantly share code, notes, and snippets.

@luizperes
Created September 10, 2016 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizperes/7a4b7d8e43a025864bac19f5fa1c9ba1 to your computer and use it in GitHub Desktop.
Save luizperes/7a4b7d8e43a025864bac19f5fa1c9ba1 to your computer and use it in GitHub Desktop.
#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