c语言第一个小游戏:贪吃蛇小游戏08(贪吃蛇完结)
贪吃蛇撞墙和想不开咬死自己
#include <curses.h>
#include <stdlib.h>
struct snake{
int hang;
int lie;
struct snake *next;
};
struct snake food;
struct snake *head;
struct snake *tail;
int key;
int dir;
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
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 hasSnakeFood(int i,int j)
{
if(food.hang==i && food.lie==j){
return 1;
}
return 0;
}
void gamepic()
{
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(hasSnakeFood(hang,lie)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(hang == 19){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
}
printw("by shijintao\n");
printw("key =%d\n",key);
}
void addNode()
{
struct snake *new;
new =(struct snake *)malloc(sizeof(struct snake));
new->next=NULL;
switch(dir){
case UP:
new->hang=tail->hang-1;
new->lie=tail->lie;
tail->next=new;
tail = new;
break;
case DOWN:
new->hang=tail->hang+1;
new->lie=tail->lie;
tail->next=new;
tail = new;
break;
case LEFT:
new->lie=tail->lie-1;
new->hang=tail->hang;
tail->next=new;
tail = new;
break;
case RIGHT:
new->lie=tail->lie+1;
new->hang=tail->hang;
tail->next=new;
tail = new;
break;
}
}
void initFood()
{
int x=rand()%20;
int y=rand()%20;
food.hang=x;
food.lie=y;
}
void initSnake()
{
struct snake *p;
while(head != NULL){
p=head;
head=head->next;
free(p);
}
initFood();
head = (struct snake *)malloc(sizeof(struct snake));
dir = RIGHT;
head->hang=2;
head->lie=2;
head->next=NULL;
tail = head;
addNode();
addNode();
}
void deleteNode()
{
struct snake *p;
p = head;
head = head->next;
free(p);
}
int ifsnakeDie()
{
struct snake *p; //为了遍历链表,看有没有咬自己
p=head;
if(tail->hang<0||tail->hang==20||tail->lie==20||tail->lie==0){
return 1;
}
while(p->next!=NULL){
//尾节点和其中一个节点 的行列都相同时就是发生自己咬自己死的情况,也就是每次移动一下每个坐标都会进来判断,链表也都会遍历一遍,如果尾节点的坐标与节点坐标相同 ,那么就是死!!
if(p->hang == tail->hang && p->lie == tail->lie){
return 1;
}
p=p->next;
}
}
void moveSnake()
{
addNode();
if(hasSnakeFood(tail->hang,tail->lie)){
initFood();
}else{
deleteNode();
}
if(ifsnakeDie()){
initSnake();
}
}
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_RIGHT:
turn(RIGHT);
break;
case KEY_LEFT:
turn(LEFT);
break;
}
}
}
void * gamerefresh()
{
while(1){
moveSnake();
gamepic();
refresh();
usleep(100000);
}
}
int main()
{
pthread_t th1;
pthread_t th2;
initNcurse();
initSnake();
gamepic();
pthread_create(&th2,NULL,gamerefresh,NULL);
pthread_create(&th1,NULL,changeDir,NULL);
while(1);
getch();
endwin();
return 0;
}
终于完结了噶,贪吃蛇笔记分享就到这里了。