该小游戏运行在ubuntu环境下,编译时使用gcc snake.c -lcurses -lpthread记得必须链接库,记录学习用
#include
#include
#include
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake
{
int hang;
int lie;
struct Snake *next;
};
struct Snake *head = NULL;
struct Snake *tail = NULL;
int key;
int dir;
struct Snake food;
void initFood() //初始化食物
{
int x = rand()%20; //rand()%20 //随机生成食物,用取余保证行列坐标在地图内
int y = rand()%20;
food.hang = x;
food.lie = y;
}
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
int hasSnakeNode(int i, int j)
{
struct Snake *p;
p = head;
while(p != NULL){
if(p->hang == i && p->lie == j){
return 1;
}
p = p->next;
}
return 0;
}
int hasFood(int i, int j)
{
if(food.hang == i && food.lie == j){
return 1;
}
return 0;
}
void gamePic() //地图布局,创建20X20的地图
{
int hang;
int lie;
move(0,0);
for(hang=0;hang<20;hang++){
if(hang == 0){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
if(hang>=0 || hang<= 19)
{
for(lie=0;lie<=20;lie++){
if(lie ==0 || lie==20){
printw("|");
}else if(hasSnakeNode(hang,lie)){
printw("[]");
}else if(hasFood(hang,lie)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(hang == 19){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
printw("By huyu,food.hang=%d,food.lie=%d\n",food.hang,food.lie);
}
}
}
void addNode() //增加节点
{
struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake)); //malloc函数开辟指针的空间
new->next = NULL;
switch(dir){
case UP:
new->hang = tail->hang-1; //向上
new->lie = tail->lie;
break;
case DOWN:
new->hang = tail->hang+1; //向下
new->lie = tail->lie;
break;
case LEFT:
new->hang = tail->hang; //向左
new->lie = tail->lie-1;
break;
case RIGHT:
new->hang = tail->hang; //向右
new->lie = tail->lie+1;
break;
}
tail->next = new;
tail = new;
}
void initSnake() //初始化蛇身
{
struct Snake *p;
dir = RIGHT;
while(head!=NULL){
p = head;
head = head->next;
free(p); //释放空间
}
initFood();
head = (struct Snake *)malloc(sizeof(struct Snake));
head->hang = 1;
head->lie = 1;
head->next = NULL;
tail = head;
addNode();
addNode();
addNode();
addNode();
}
void deleNode()
{
struct Snake *p;
p = head;
head = head->next;
free(p);
}
int ifSnakeDie() //蛇初始化条件,死亡
{
struct Snake *p;
p = head;
if(tail->hang<0 || tail->lie==0 ||tail->hang==20 || tail->lie==20){
return 1;
}
while(p->next!=NULL){
if(p->hang == tail->hang && p->lie == tail->lie){
return 1;
}
p=p->next;
}
return 0;
}
void moveSnake()
{
addNode();
if(hasFood(tail->hang,tail->lie)){
initFood();
}
else{
deleNode();
}
if(ifSnakeDie()){
initSnake();
}
}
void refreshJieMian() //刷新界面
{
while(1){
moveSnake();
gamePic();
refresh(); //刷新函数
usleep(100000); //延时
}
}
void turn(int direction)
{
if(abs(dir) != abs(direction)){ //取绝对值,防止出现反方向走的情况
dir = direction;
}
}
void changeDir()
{
while(1){
key = getch();
switch(key){
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initSnake();
gamePic();
pthread_create(&t1, NULL, refreshJieMian, NULL ); //创建线程
pthread_create(&t2, NULL, changeDir, NULL );
while(1); //不让线程结束
getch();
endwin();
return 0;
}