上节讲到限制蛇身回头,本节要实现吃食物功能
实现思路
在存储上食物方面可以复用蛇的结构体。初始化食物的时候,我们设置食物的坐标,每次调用这个函数的时候,坐标发生一些规律的变化。另外我们需要扫描食物的函数,这个扫描应该也是放在地图函数中的,最后我们需要检测是否吃到了食物,也就是蛇头(链表的尾指针)是否和食物的坐标重合。
- 定义食物结构体
- 初始化食物
- 扫描食物
- 检测是否吃到了食物
#include"curses.h"
#include "stdlib.h"
#include "pthread.h"
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake
{
int row;
int col;
struct Snake* next;
};
struct Snake food;
int key; // user input
int dir;
struct Snake* head = NULL;
struct Snake* tail = NULL;
void addNode();
void mapinit();
void cursesinit()
{
initscr();
keypad(stdscr,1);
// noecho();
}
void snakeinit()
{
// init food
foodinit();
// dir init
dir = RIGHT;
// free
struct Snake* p;
while(head != NULL)
{
p = head;
head = head->next;
free(p);
}
// create node
head = (struct Snake*)malloc(sizeof(struct Snake));
head->row = 2;
head->col = 2;
head->next = NULL;
tail = head;
addNode();
addNode();
}
void foodinit()
{
static int x = 4;
static int y = 5;
food.row = x;
food.col = y;
x += 2;
y += 2;
}
void addNode()
{
struct Snake* node = (struct Snake*)malloc(sizeof(struct Snake));
node->next = NULL;
switch(dir)
{
case RIGHT:
node->row = tail->row;
node->col = tail->col + 1;
break;
case LEFT:
node->row = tail->row;
node->col = tail->col - 1;
break;
case DOWN:
node->row = tail->row + 1;
node->col = tail->col;
break;
case UP:
node->row = tail->row - 1;
node->col = tail->col;
break;
}
tail->next = node;
tail = node;
}
void deleteNode()
{
struct Snake* p;
p = head;
head = head->next;
free(p);
}
void moveSnake()
{
addNode();
// if there have food
if(hasFood(tail->row,tail->col))
foodinit();
else
deleteNode();
// if snake is over.
if(tail->row == 0 || tail->col == 0 || tail->row >= 20 || tail->col >= 19)
{
snakeinit();
}
}
int hasSnake(int row,int col)
{
struct Snake* p = head;
while(p!=NULL)
{
if(row == p->row && col == p->col)
return 1;
p = p->next;
}
return 0;
}
int hasFood(int row,int col)
{
if(row==food.row && col==food.col)
return 1;
return 0;
}
void mapinit()
{
int row;
int col;
move(0,0);
for(row = 0;row < 20;row++)
{
// one
if(row == 0 || row == 19)
{
for(col = 0;col < 19;col++)
printw("--");
}
// two
else
{
for(col = 0;col < 20;col++)
{
if(col == 0 || col == 19 ) printw("|");
else if(hasSnake(row,col))
{
printw("[]");
}
else if(hasFood(row,col))
{
printw("##");
}
else
{
printw(" ");
}
}
}
printw("\n");
}
printw("By hongzhe\n");
}
void refreshScreen()
{
while(1)
{
moveSnake(); // snake forward
mapinit(); // fresh map
refresh(); // fresh screen
usleep(100000); // sleep(0.5)
}
return;
}
void turnDir(int direction)
{
if(abs(dir) != abs(direction))
dir = direction;
}
void changeDir()
{
while(1)
{
key = getch();
switch(key)
{
case KEY_DOWN:
printw("\rDOWN\t");
turnDir(DOWN);
break;
case KEY_RIGHT:
printw("\rRIGHT\t");
turnDir(RIGHT);
break;
case KEY_LEFT:
printw("\rLEFT\t");
turnDir(LEFT);
break;
case KEY_UP:
printw("\rUP\t");
turnDir(UP);
break;
}
}
return;
}
int main()
{
pthread_t t1;
pthread_t t2;
cursesinit();
snakeinit();
mapinit();
pthread_create(&t1,NULL,refreshScreen,NULL);
pthread_create(&t2,NULL,changeDir,NULL);
while(1); // zu se main thread
getch();
endwin();
return 0;
}