Die Programm-Vorlage für die Entwicklung des Konsolen-Spiels Snake.
/* Snake, Console Game TEMPLATE! Linux-System required! @author: www.c-howto.de */ #include<stdio.h> #include<termio.h> #define BLANK 0 #define HEAD 1 #define TAIL 2 #define FOOD 9 #define KEY_MOVE_UP 119 #define KEY_MOVE_LEFT 97 #define KEY_MOVE_DOWN 115 #define KEY_MOVE_RIGHT 100 #define WIDTH 60 #define HEIGHT 20 #define DEBUG 0 struct position { int x; int y; } pos; int field[WIDTH][HEIGHT]; static struct termio savemodes; static int havemodes = 0; int tty_break(); int tty_getchar(); int tty_fix(); void resetField(); void printField(); void setItem(int, int, int); void move(int); int main () { int key; resetField(); // Startposition pos.x=WIDTH/2; pos.y=HEIGHT/2; setItem(pos.x, pos.y, HEAD); tty_break(); while(key!='x') { printField(); key=tty_getchar(); move(key); if(DEBUG) { printf(" %d %c |", key); } } printf("\n\n"); return 0; } void move(int key) { // zu implementieren, to implement } void setItem(int x, int y, int item) { // zu implementieren, to implement } void resetField() { // zu implementieren, to implement } void printField() { // zu implementieren, to implement } int tty_break() { struct termio modmodes; if(ioctl(fileno(stdin), TCGETA, &savemodes) < 0) return -1; havemodes = 1; modmodes = savemodes; modmodes.c_lflag &= ~ICANON; modmodes.c_cc[VMIN] = 1; modmodes.c_cc[VTIME] = 0; return ioctl(fileno(stdin), TCSETAW, &modmodes); } int tty_getchar() { return getchar(); } int tty_fix() { if(!havemodes) return 0; return ioctl(fileno(stdin), TCSETAW, &savemodes); }