/** * demo program for use linux frame buffer , this program is snake eat food game, it have some feature: * 1. use touchscreen control the snake move director * 2. auto generate food random * 3. Difficult level auto adjust */#include <stdio.h>#include <stdlib.h>#include <string.h>
#include <tslib.h>#define MAX_LENGTH 100#define SNAKE_NODE_W 10#define SNAKE_NODE_H 10typedef struct node_t { int x; int y; int w; int h; int colindx; int bgcolindx;} Node;
static Node snake_body[MAX_LENGTH];static int snake_len;static Node head;static Node food;static volatile int move_dir;static volatile int isPause=1;void update_screen(int flag){ int i; for(i=0;i<snake_len;i++){ fillrect(snake_body[i].x, snake_body[i].y, snake_body[i].x+snake_body[i].w, snake_body[i].y+snake_body[i].h, (flag?snake_body[i].colindx:snake_body[i].bgcolindx)); } fillrect(food.x,food.y,food.x+food.w,food.y+food.h,food.colindx);}
void gen_food(void){ food.x = food.w*(rand()%(31)); food.y = food.h*(rand()%23); food.colindx = rand()%10+3;}int eat_food(void){ if((food.x == head.x) && (food.y == head.y)) return 1; else return 0;}int is_gameover(void){ int i; for(i=0;i<snake_len;i++){ if((head.x == snake_body[i].x) && (head.y == snake_body[i].y)) return 1; } return 0;}void snake_init(void){ int i; move_dir = 0; isPause = 1; for(i=0;i<3;i++){ snake_body[i].w = SNAKE_NODE_W; snake_body[i].h = SNAKE_NODE_H; snake_body[i].x = i*snake_body[i].w; snake_body[i].y = 0; snake_body[i].colindx = 3; snake_body[i].bgcolindx = 0; } snake_len = 3; food.w = SNAKE_NODE_W; food.h = SNAKE_NODE_H; food.bgcolindx = 0; gen_food();}void snake_move(void){ struct ts_sample samp; int i; int x,y; x = head.x = snake_body[snake_len-1].x; y = head.y = snake_body[snake_len-1].y; head.colindx = snake_body[snake_len-1].colindx; head.bgcolindx = snake_body[snake_len-1].bgcolindx; if(read_ts(&samp)) { if(isPause) { isPause = 0; } //printf("%d,%d\n",samp.x,samp.y); if((_sub(samp.x,x) > 50) && (_sub(samp.y,y)<50)) { if(samp.x > x){ printf("BOTTOM\n"); move_dir = 0; } else{ move_dir = 1; printf("UP\n"); } } if((_sub(samp.x,x) < 50) && (_sub(samp.y,y)>50)) { if(samp.y > y){ move_dir = 2; printf("RIGHT\n"); } else{ move_dir = 3; printf("LEFT\n"); } } //x = samp.x; //y = samp.y; } if(isPause) { usleep(10*1000); return; }
switch(move_dir){ case 0: x += 10; if(x>310) x = 0; break; case 1: x -= 10; if(x<0) x = 310; break; case 2: y += 10; if(y>230) y = 0; break; case 3: y -= 10; if(y<0) y=230; break; } head.x = x; head.y = y; if(is_gameover()){ refresh_screen (); snake_init(); } if(eat_food()){ snake_body[snake_len].x = food.x; snake_body[snake_len].y = food.y; snake_body[snake_len].w = food.w; snake_body[snake_len].h = food.h; snake_body[snake_len].colindx = food.colindx; snake_body[snake_len].bgcolindx = food.bgcolindx; snake_len++; printf("eat one, now %d\n",snake_len); gen_food(); } else{ for(i=0;i<snake_len-1;i++){ snake_body[i].x = snake_body[i+1].x; snake_body[i].y = snake_body[i+1].y; //snake_body[i].colinx = snake_body[i+1].colindx; //snake_body[i].bgcolindx = snake_body[i+1].bgcolindx; } snake_body[i].x = head.x; snake_body[i].y = head.y; } }
void snake_loop(void){ snake_init(); refresh_screen (); while (1) { snake_move(); update_screen(1); usleep(500*1000); update_screen(0); }}

#include <tslib.h>#define MAX_LENGTH 100#define SNAKE_NODE_W 10#define SNAKE_NODE_H 10typedef struct node_t { int x; int y; int w; int h; int colindx; int bgcolindx;} Node;
static Node snake_body[MAX_LENGTH];static int snake_len;static Node head;static Node food;static volatile int move_dir;static volatile int isPause=1;void update_screen(int flag){ int i; for(i=0;i<snake_len;i++){ fillrect(snake_body[i].x, snake_body[i].y, snake_body[i].x+snake_body[i].w, snake_body[i].y+snake_body[i].h, (flag?snake_body[i].colindx:snake_body[i].bgcolindx)); } fillrect(food.x,food.y,food.x+food.w,food.y+food.h,food.colindx);}
void gen_food(void){ food.x = food.w*(rand()%(31)); food.y = food.h*(rand()%23); food.colindx = rand()%10+3;}int eat_food(void){ if((food.x == head.x) && (food.y == head.y)) return 1; else return 0;}int is_gameover(void){ int i; for(i=0;i<snake_len;i++){ if((head.x == snake_body[i].x) && (head.y == snake_body[i].y)) return 1; } return 0;}void snake_init(void){ int i; move_dir = 0; isPause = 1; for(i=0;i<3;i++){ snake_body[i].w = SNAKE_NODE_W; snake_body[i].h = SNAKE_NODE_H; snake_body[i].x = i*snake_body[i].w; snake_body[i].y = 0; snake_body[i].colindx = 3; snake_body[i].bgcolindx = 0; } snake_len = 3; food.w = SNAKE_NODE_W; food.h = SNAKE_NODE_H; food.bgcolindx = 0; gen_food();}void snake_move(void){ struct ts_sample samp; int i; int x,y; x = head.x = snake_body[snake_len-1].x; y = head.y = snake_body[snake_len-1].y; head.colindx = snake_body[snake_len-1].colindx; head.bgcolindx = snake_body[snake_len-1].bgcolindx; if(read_ts(&samp)) { if(isPause) { isPause = 0; } //printf("%d,%d\n",samp.x,samp.y); if((_sub(samp.x,x) > 50) && (_sub(samp.y,y)<50)) { if(samp.x > x){ printf("BOTTOM\n"); move_dir = 0; } else{ move_dir = 1; printf("UP\n"); } } if((_sub(samp.x,x) < 50) && (_sub(samp.y,y)>50)) { if(samp.y > y){ move_dir = 2; printf("RIGHT\n"); } else{ move_dir = 3; printf("LEFT\n"); } } //x = samp.x; //y = samp.y; } if(isPause) { usleep(10*1000); return; }
switch(move_dir){ case 0: x += 10; if(x>310) x = 0; break; case 1: x -= 10; if(x<0) x = 310; break; case 2: y += 10; if(y>230) y = 0; break; case 3: y -= 10; if(y<0) y=230; break; } head.x = x; head.y = y; if(is_gameover()){ refresh_screen (); snake_init(); } if(eat_food()){ snake_body[snake_len].x = food.x; snake_body[snake_len].y = food.y; snake_body[snake_len].w = food.w; snake_body[snake_len].h = food.h; snake_body[snake_len].colindx = food.colindx; snake_body[snake_len].bgcolindx = food.bgcolindx; snake_len++; printf("eat one, now %d\n",snake_len); gen_food(); } else{ for(i=0;i<snake_len-1;i++){ snake_body[i].x = snake_body[i+1].x; snake_body[i].y = snake_body[i+1].y; //snake_body[i].colinx = snake_body[i+1].colindx; //snake_body[i].bgcolindx = snake_body[i+1].bgcolindx; } snake_body[i].x = head.x; snake_body[i].y = head.y; } }
void snake_loop(void){ snake_init(); refresh_screen (); while (1) { snake_move(); update_screen(1); usleep(500*1000); update_screen(0); }}

