步骤:地图构建->初始化蛇身->让蛇朝一个方向动起来->让蛇配合键盘改变方向移动(需要用到多线程,还要解决蛇撞墙)->随机产生食
#include <curses.h>
#include <stdlib.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct SnakeBody
{
int hang;
int lie;
struct SnakeBody *next;
};
struct SnakeBody *head = NULL;
struct SnakeBody *tail = NULL;
struct SnakeBody food;
int key;
int dir;
void initfood()
{
int x = rand()%20;
int y = rand()%20;
food.hang = x;
food.lie = y;
}
void initScr()
{
initscr();
keypad(stdscr,1);
noecho();
}
int hasfood(int i,int j)
{
if(food.hang == i && food.lie == j)
{
return 1;
}
return 0;
}
int hasSnakeNode(int i,int j)
{
struct SnakeBody *p;
p = head;
while(p != NULL)
{
if(p->hang==i && p->lie==j)
{
return 1;
}
p = p->next;
}
return 0;
}
void gamepic()
{
int hang,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) == 1)
{
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 WangYangLong,key=%d\n,food.hang=%d,food.lie=%d",key,food.hang,food.lie);
printw("\n");
}
}
}
void addBody()
{
struct SnakeBody *new = (struct SnakeBody *)malloc(sizeof(struct SnakeBody));
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;
}
new->next = NULL;
tail->next = new;
tail = new;
}
void initBody()
{
struct SnakeBody *p;
dir = RIGHT;
while(head!=NULL)
{
p = head;
head = head->next;
free(p);
}
initfood();
head = (struct SnakeBody *)malloc(sizeof(struct SnakeBody));
head->hang = 1;
head->lie = 1;
head->next =NULL;
tail = head;
addBody();
addBody();
addBody();
}
void deleteBody()
{
struct SnakeBody *p;
p = head;
head = head->next;
free(p);
}
int SnakeDie()
{
struct SnakeBody *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()
{
addBody();
if(hasfood(tail->hang,tail->lie))
{
initfood();
}
else
{
deleteBody();
}
if(SnakeDie())
{
initBody();
}
}
void refreshJieMian()
{
while(1)
{
moveSnake();
gamepic();
refresh();
usleep(200000);
}
}
void turn(int direction)
{
if(abs(dir) != abs(direction))
{
dir = direction;
}
}
void getDir()
{
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;
initScr();
initBody();
gamepic();
pthread_create(&t1,NULL,refreshJieMian,NULL);
pthread_create(&t2,NULL,getDir,NULL);
while(1);
getch();
endwin();
return 0;
}
物让蛇去吃(注意蛇不能咬到自己)
文章评论