Created
December 16, 2012 05:45
-
-
Save rongyi/4303665 to your computer and use it in GitHub Desktop.
a short snake demo for a douban friend
This file contains hidden or 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 <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
static void direction_up(void); | |
static void direction_down(void); | |
static void direction_left(void); | |
static void direction_right(void); | |
static void direction_up(void) | |
{ | |
printf("snake going up\n"); | |
} | |
static void direction_down(void) | |
{ | |
printf("snake going down\n"); | |
} | |
static void direction_left(void) | |
{ | |
printf("snake going left\n"); | |
} | |
static void direction_right(void) | |
{ | |
printf("sname going right\n"); | |
} | |
struct snake { | |
void (*snake_move)(void); | |
}; | |
static struct snake sbsnake; | |
pthread_t control_thread; | |
static void *control_routine(void *attr) | |
{ | |
while (1) { | |
usleep(1000); | |
switch(getchar()) { | |
case 'a': | |
sbsnake.snake_move = direction_left; | |
break; | |
case 'd': | |
sbsnake.snake_move = direction_right; | |
break; | |
case 's': | |
sbsnake.snake_move = direction_down; | |
break; | |
case 'w': | |
sbsnake.snake_move = direction_up; | |
break; | |
} | |
} | |
} | |
int main ( int argc, char *argv[] ) | |
{ | |
pthread_create(&control_thread, NULL, control_routine, NULL); | |
sbsnake.snake_move = direction_right; | |
while (1) { | |
sleep(1); | |
sbsnake.snake_move(); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment