功能异常简单的贪吃蛇(队列实现)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define u 0
#define r 1
#define l 2
#define d 3
#define fail 0
#define success 1
#define maxsize 100
int Size=0;
int difficulty=0;
int dx[4]={-1,0,0,1};
int dy[4]={0,1,-1,0};
int map[maxsize][maxsize];
typedef struct body{
    int x;
    int y;
}body;
typedef struct snake{
    int size,n,head,tail,dir;
    body que[maxsize*maxsize+1];
}snake;
typedef struct food{
    int x;
    int y;
}food;
food apple;
snake s;
void GoToXY(int x, int y)
{
       COORD pos = {x,y};                 
       HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);    
       SetConsoleCursorPosition(hOut, pos);      
}
int printxy(int x,int y,char c)
{
    GoToXY(y,x);
    printf("%c",c);
    return 0;
}
int initmap()
{
    int i,j;
    for (i=1;i<Size-1;i++)
        for (j=1;j<Size-1;j++)
            map[i][j]=0;
    for (j=0;j<Size;j++)
    {
        map[0][j]=1;
        printxy(0,j,'#');
        map[Size-1][j]=1;
        printxy(Size-1,j,'#');
    }
    for (i=0;i<Size;i++)
    {
        map[i][0]=1;
        printxy(i,0,'#');
        map[i][Size-1]=1;
        printxy(i,Size-1,'#');
    }
    return 0;
}
int dropapple()
{
    int x=apple.x;
    int y=apple.y;
    int nx=rand()%(Size-2)+1;
    int ny=rand()%(Size-2)+1;
    while (map[nx][ny])
    {
        nx=rand()%(Size-2)+1;
        ny=rand()%(Size-2)+1;
    }
    apple.x=nx;
    apple.y=ny;
    printxy(apple.x,apple.y,'@');
    return 0;
}
int initgame()
{
    s.size=Size*Size+1;
    s.n=1;
    s.head=0;
    s.tail=0;
    s.que[0].x=Size/2;
    s.que[0].y=Size/2;
    s.dir=u;
    map[Size/2][Size/2]=1;
    time_t t;
    srand((unsigned)time(&t));
    int i,j;
    return 0;
}
int enque(int dir)
{
    int x,y;
    x=s.que[s.tail].x+dx[dir];
    y=s.que[s.tail].y+dy[dir];
    if (map[x][y])
    { 
        printxy(x,y,'!'); 
        return 1;
    } 
    s.tail=s.tail==s.size-1?0:s.tail+1;
    s.que[s.tail].x=x;
    s.que[s.tail].y=y;
    map[x][y]=1;
    printxy(x,y,'*');
    return 0;
}
int deque()
{
    int x,y;
    x=s.que[s.head].x;
    y=s.que[s.head].y;
    s.head=s.head==s.size-1?0:s.head+1;
    map[x][y]=0;
    printxy(x,y,' ');
    return 0;
}
int moves()
{
    int crash;
    crash=enque(s.dir);
    if (s.que[s.head].x==apple.x && s.que[s.head].y==apple.y)
    {
        if (crash)
            return fail;
        else
            return success;
    }
    else
    {
        deque();
        if (crash)
            return fail;
        else
            return -1; 
    }
}
int playgame()
{
    dropapple();
    while (1)
    {
        if (kbhit())
        {
            char c=getch();
            int dir;
            switch(c){
            case 'w':dir=u;break;
            case 'a':dir=l;break;
            case 's':dir=d;break;
            case 'd':dir=r;break;
            case 'q':return 0;
            default :dir=-1;
            }
            if (dir>=0 && dir!=3-s.dir)
                s.dir=dir;
        }
        switch (moves(s)){
        case fail:
            GoToXY(0,Size);
            return 0;
        case success:
            dropapple();
            break;
        default :break;
        }
        sleep(1/(double)difficulty);
    }
}
int question()
{
    while (Size<20 || Size>40)
    {
        printf("please input map size(20~40):");
        scanf("%d",&Size);
    }
    while (difficulty<10 || difficulty>1000)
    {
        printf("\nplease input diffculty(10~1000,100 is recommended):");
        scanf("%d",&difficulty);
    }
    system("cls");
}
int main(void)
{
    question();
    initmap();
    initgame();
    playgame();
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值