Created
September 15, 2011 12:38
-
-
Save laserbat/1219133 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 <curses.h> | |
#define H 22 | |
#define W 79 | |
#define OFF 1 | |
#define C_YOU '@' | |
#define C_WALL '#' | |
#define C_FLOOR '.' | |
#define WALL 0 | |
#define FLOOR 1 | |
void start_curses(); | |
void stop_curses(); | |
void init_level(); | |
void main_loop(); | |
int level[W][H]; | |
void start_curses(){ | |
initscr(); | |
noecho(); | |
cbreak(); | |
} | |
void stop_curses(){ | |
endwin(); | |
} | |
void init_level(){ | |
int x = 0; | |
int y = 0; | |
for(x = 0;x <= W;++x){ | |
for(y = 0;y <= H;++y){ | |
level[x][y] = WALL; | |
} | |
} | |
for(x = 1;x < W;++x){ | |
for(y = 1;y < H;++y){ | |
level[x][y] = FLOOR; | |
} | |
} | |
} | |
void main_loop(){ | |
int k; | |
int x, y; | |
int px, py; | |
int px2, py2; | |
char pos[10]; | |
k = x = y = 0; | |
px = py = 5; | |
px2 = py2 = 5; | |
do { | |
if(k == 0){ | |
++k; | |
}else{ | |
k = getch(); | |
} | |
if(k == 'h' || k == '4') | |
--px2; | |
else if(k == 'l' || k == '6') | |
++px2; | |
else if(k == 'j' || k == '2') | |
++py2; | |
else if(k == 'k' || k == '8') | |
--py2; | |
else if(k == 'y' || k == '7'){ | |
--px2;--py2; | |
} | |
else if(k == 'u' || k == '9'){ | |
++px2;--py2; | |
} | |
else if(k == 'b' || k == '1'){ | |
--px2;++py2; | |
} | |
else if(k == 'n' || k == '3'){ | |
++px2;++py2; | |
} | |
if(level[px2][py2] == FLOOR){ | |
px = px2; | |
py = py2; | |
}else{ | |
px2 = px; | |
py2 = py; | |
} | |
for(x = 0;x <= W;++x){ | |
for(y = 0;y <= H;++y){ | |
mvaddch(OFF + y,x,level[x][y]?C_FLOOR:C_WALL); | |
} | |
} | |
mvaddstr(OFF + 10,10,"Hello, world of C roguelikes!"); | |
sprintf(pos, "%d : %d",px,py); | |
mvaddstr(0,0,pos); | |
mvaddch(OFF + py,px,C_YOU); | |
mvaddch(OFF + py,px + 1,'\b'); | |
} while(k != 'q'); | |
} | |
int main(void){ | |
start_curses(); | |
init_level(); | |
main_loop(); | |
stop_curses(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment