欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

单链表实现贪吃蛇

程序员文章站 2022-03-26 20:06:10
终于把学的单链表塞进贪吃蛇里的. 相比于上一篇的数组,链表的理解程度可能高一些. 上一篇的链接 上代码: #include #include #include #include #include ......

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

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

上一篇的

上代码:

#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次更改)