把我丑哭的C语言贪吃蛇程序

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#define LEN 20
#define WID 20

COORD food;//食物全局变量
int x=2,y=1;//起始坐标
int a[WID][LEN];
int score = 1;//体长
typedef struct node{
    struct node *pro;
    int a,b;
    struct node *next;

}NODE;//身体节点
struct node *h;

void rbody()//record body
{
    int i;
    NODE *p;
    NODE *q;
    p=(NODE *)malloc(sizeof(NODE));
    p->pro=NULL;
    p->a=x;
    p->b=y;
    p->next=h;
    h->pro=p;
    h=p;
    if(x!=food.X || y!=food.Y)
    {
        while(p->next!=NULL)
        {
            p=p->next;
        }
        q=p->pro;
        p->pro=NULL;
        Pos(p->a,p->b);
        printf("  ");
        free(p);
        q->next=NULL;
    }


}
int eatbody()//eat body若返回4则游戏结束
{
    NODE *t;
    t=h;
    t=t->next;
    while(t!=NULL)
    {
        if(t->a==x && t->b==y)
        {
            return 4;
        }
        t=t->next;
    }
    return 0;
}
int mkey()
{
    if (GetAsyncKeyState(VK_UP)) return 8;
    if (GetAsyncKeyState(VK_DOWN)) return 2;
    if (GetAsyncKeyState(VK_LEFT)) return 4;
    if (GetAsyncKeyState(VK_RIGHT)) return 6;
    if (GetAsyncKeyState(VK_RETURN)) return 0;
    else return 0;
}
void Pos(int x,int y)
{
    COORD pos;
    HANDLE houtput;
    pos.X=x;
    pos.Y=y;
    houtput=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(houtput,pos);
}
int mmove()//移动函数如果返回1则撞墙
{
    int c;
    int n=6;
reset_food:
    ffood();

    while(1)
    {

        Sleep(500);
        c=mkey();
        switch (c)
        {
        case 8:
            y--;
            Pos(x,y);
            printf("♀");
            n=8;
            rbody();
            break;
        case 2:
            y++;
            Pos(x,y);
            printf("♀");
            n=2;
            rbody();
            break;
        case 4:
            x-=2;//横向设置为两个空格为一个单位
            Pos(x,y);
            printf("♀");
            n=4;
            rbody();
            break;
        case 6:
            x+=2;
            Pos(x,y);
            printf("♀");
            n=6;
            rbody();
            break;
        case 0:
            automove(n);
            rbody();
            break;
        }
        if(yor(x,y)==1)
        {
            return 1;
        }
        if(x==food.X && y==food.Y)
        {
            goto reset_food;
        }
        if(eatbody()==4)
        {
            return 1;
        }

    }
}
void automove(int n)
{
    if(n==8)
    {
        y--;
        Pos(x,y);
        printf("♀");
    }
    else if(n==2)
    {
        y++;
        Pos(x,y);
        printf("♀");
        n=2;
    }
    else if(n==4)
    {
            x-=2;//横向设置为两个空格为一个单位
            Pos(x,y);
            printf("♀");
            n=4;
    }
    else if(n==6)
    {
            x+=2;
            Pos(x,y);
            printf("♀");
            n=6;
    }
}
void mmap()
{
    int i,j;
    for(i=0;i
    {
        a[0][i]=1;
        a[WID-1][i]=1;
    }
    for(i=0;i
    {
        a[i][0]=1;
        a[i][LEN-1]=1;
    }
    for(i=0;i
    {
        for(j=0;j
        {
            if(j==WID-1)
               {
                   printf("█\n");
                   continue;
               }
            if(a[j][i]==1)
            {
                printf("█");
            }
            else if(a[j][i]==0)
            {
                printf("  ");
            }


        }
    }
}
int yor(int x,int y)//yes和or的合称 目的是判断是否继续游戏
{
    if(x==0 || x==2*WID-2 || y==0 || y==LEN-1)
        return 1;
}
void ffood()//产生食物的坐标
{

    srand(time(NULL));
LOOP:
        food.X=2*(rand() );
        food.Y=rand() ;
    if(food.X<2 || food.X>36 || food.Y<1 || food.Y>18)
        goto LOOP;

    Pos(food.X,food.Y);
    printf("⊙");

}

int main()
{
    h=(NODE *)malloc(sizeof(NODE));
    h->pro=NULL;
    h->a=2;
    h->b=1;
    h->next=NULL;
    mmap();
    if(mmove()==1)
        Pos(50,15);
        printf("游戏结束!");
    Sleep(5000);

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值