单链表实现贪吃蛇

终于把学的单链表塞进贪吃蛇里的.

相比于上一篇的数组,链表的理解程度可能高一些.

上一篇的链接

上代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>

#define U 0
#define D 1
#define L 2
#define R 3

struct node
{
    int x;
    int y;
    struct node *next;
};
typedef struct node NODE;
NODE *head;

int state,speed=200;
int snake_x=6,snake_y=5;
int food_x,food_y;
int wall_x=1,wall_y=1,wall_x1=97,wall_y1=25;


int gotoxy(int x,int y)
{
    COORD coord = {x,y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int add(NODE *head,int x,int y)
{
    NODE *p;
    p=(NODE *)malloc(sizeof(NODE));
    p->x=x;
    p->y=y;
    gotoxy(x,y);
    printf("");
    p->next=head->next;
    head->next=p;
}

int delete(NODE *head)
{
    NODE *p,*q;
    p=(NODE *)malloc(sizeof(NODE));
    for (p=head; p->next!=NULL;p=p->next)
    {
        q=p;
    }
    gotoxy(p->x,p->y);
    printf(" ");
    q->next=NULL;
    free(p);
}


int find(NODE *head,int x,int y)     //FIND:1 NOT FIND :0
{
    NODE *p;
    for (p=head; p!=NULL; p=p->next)
    {
        if (p->x==x && p->y==y)
        {
            return 1;
        }
    }
    return 0;
}

int border(int x,int y)
{
    //查询物体是否撞到蛇身
    if (find(head,x,y)==1)
    {
        return 1;
    }
    //查询物体是否撞到墙
    if (wall_x>=x || wall_x1<=x || wall_y>=y || wall_y1<=y)
    {
        return 1;
    }
    return 0;
}

int food_make()
{
    //初始化随机种子
    srand((unsigned int)time(NULL));
    int x,y;
    x=food_x;
    y=food_y;
    while (1)
    {
        //随机食物坐标
        food_x=rand()%wall_x1+wall_x;
        food_y=rand()%wall_y1+wall_y;

        //查询食物是否生成位置错误
        if (food_x%2 ==1)
        {
            continue;
        }
        
        if (food_x==x && food_y==y)
        {
            continue;
        }
        if (border(food_x,food_y)==1)
        {
            continue;
        }

        //打印食物
        gotoxy(food_x,food_y);
        printf("");
        gotoxy(0,29);

        return 1;
    }
    return 0;
}

int eat()
{
    if (snake_x==food_x && snake_y==food_y)
    {
        food_make();
        return 1;
    }
    return 0;
    
}
int control()
{
    state = D;
    gotoxy(snake_x,snake_y);
    printf(" ");
    while(1)
    {
        if(GetAsyncKeyState(VK_UP) && state!=D)
        {
            state=U;
        }
        else if(GetAsyncKeyState(VK_DOWN) && state!=U)
        {
            state=D;
        }
        else if(GetAsyncKeyState(VK_LEFT)&& state!=R)
        {
            state=L;
        }
        else if(GetAsyncKeyState(VK_RIGHT)&& state!=L)
        {
            state=R;
        }
        else if(GetAsyncKeyState(VK_F1))
        {
            if (speed<=20)
            {
                speed=10;
            }
            else
            {
                speed=speed-10;
            }
            
            
        }
        else if(GetAsyncKeyState(VK_F2))
        {
            speed=speed+10;
        }
        snake_move();
        if (eat()==0)
        {
            delete(head);
        }

    }
}
int snake_move()
{
    if (state == U)
    {
        --snake_y;
        add(head,snake_x,snake_y);
        gotoxy(10,29);
        printf("Speed:%4d",speed);
    }
    if (state == D)
    {
        ++snake_y;
        add(head,snake_x,snake_y);
        gotoxy(10,29);
        printf("Speed:%4d",speed);
    }
    if (state == L)
    {
        --snake_x;
        --snake_x;
        add(head,snake_x,snake_y);
        gotoxy(10,29);
        printf("Speed:%4d",speed);
    }
    if (state == R)
    {
        ++snake_x;
        ++snake_x;
        add(head,snake_x,snake_y);
        gotoxy(10,29);
        printf("Speed:%4d",speed);
    }
    
    Sleep(speed);
}
int main()
{
    head=(NODE *)malloc(sizeof(NODE));
    head->x=NULL;
    head->y=NULL;
    head->next=NULL;
    food_make();
    control();
    system("PAUSE");
    return 0;
}

在这里因为大部分的代码是和上一篇同样的道理,所以有些无关紧要的就没写了

                                                              (第1次更改)

posted @ 2020-05-04 14:27  Your_path  阅读(536)  评论(0编辑  收藏  举报