C语言所写的简单的贪吃蛇代码

 
 
  • 才刚开始学习不久,学到链表之后老师布置了这个任务,用了很多不太熟悉的函数,例如WINDOWS.H中的定位光标,暂停、清空面板、改变面板大小等。
  • 之后我会慢慢加上注释。
  • #include<stdio.h>
  • #include<stdlib.h>
  • #include<time.h>
    #include<windows.h>
    #define U 1
    #define D 2
    #define L 3 
    #define R 4 
    void Pos();
    void creatmap();
    void creatsnake();
    int biteself();
    void creatfood();
    void cantcrosswall();
    void snakemove();
    void welcometogame();
    void gamestart();
    void caozuo();
    typedef struct snake
    {
    int x;
    int y;
    struct snake *next;
    }snake;
    int score=0,add=10,maxscore=0;
    int status,sleeptime=200;
    snake *head,*food;
    snake *q;
    int endgamestatus=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);
    }
    void creatmap()
    {
        int i;
        for(i=0;i<58;i+=2)
        {
            Pos(i,0);
            printf("■");
            Pos(i,26);
            printf("■");
        }  
    for(i=1;i<26;i++)
        {
            Pos(0,i);
            printf("■");                        
            Pos(56,i);
            printf("■");        
        }
    }
    void creatsnake()
    {
    snake *tail;
    int i;
    tail=(snake*)malloc(sizeof(snake));
    tail->x=24;
    tail->y=5;
    tail->next=NULL;
    for(i=1;i<=4;i++)
    {
    head=(snake*)malloc(sizeof(snake));
    head->next=tail;
    head->x=24+2*i;
    head->y=5;
    tail=head;
    }
    Pos(tail->x,tail->y);
    printf("⊙");
    tail=tail->next;
    while(tail!=NULL)
    {
    Pos(tail->x,tail->y);
    printf("●");
    tail=tail->next;
    }
    }
    int biteself()
    {
        snake *self;
        self=head->next;
        while(self!=NULL)
        {
            if(self->x==head->x && self->y==head->y)
            {
                return 1;
            }
            self=self->next;
        }
        return 0;
    }
    void creatfood()
    {
    snake *food_1;
    srand((unsigned)time(NULL));
    food_1=(snake*)malloc(sizeof(snake));
    while(food_1->x%2!=0)
    {
    food_1->x=rand()%52+2;
    }
    food_1->y=rand()%24+1;
    q=head;
    while(q->next==NULL)
        {
            if(q->x==food_1->x && q->y==food_1->y)
            {
                free(food_1);
                creatfood();
            }
            q=q->next;
        }
    Pos(food_1->x,food_1->y);
        food=food_1;
        printf("◎");
    }
    void cantcrosswall()
    {  
        if(head->x==0 || head->x==56 ||head->y==0 || head->y==26)
        {
            endgamestatus=1;
        }
    }
    void snakemove()
    {
    snake * nexthead;
        cantcrosswall();
        
        nexthead=(snake*)malloc(sizeof(snake));
        if(status==U)
        {
            nexthead->x=head->x;
            nexthead->y=head->y-1;
            if(nexthead->x==food->x && nexthead->y==food->y)
            {
                nexthead->next=head;
                head=nexthead;
                q=head;
    Pos(q->x,q->y);
    printf("⊙");
    q=q->next;
                while(q!=NULL)
                {
                    Pos(q->x,q->y);
                    printf("●");
                    q=q->next;
                }
                score=score+add;
                creatfood();
            }
            else                                              
            {
                nexthead->next=head;
                head=nexthead;
                q=head;
    Pos(q->x,q->y);
    printf("⊙");
    q=q->next;
                while(q->next->next!=NULL)
                {
                    Pos(q->x,q->y);
                    printf("●");
                    q=q->next;        
                }
                Pos(q->next->x,q->next->y);
                printf("  ");
                free(q->next);
                q->next=NULL;
            }
        }
        if(status==D)
        {
            nexthead->x=head->x;
            nexthead->y=head->y+1;
            if(nexthead->x==food->x && nexthead->y==food->y)  
            {
                nexthead->next=head;
                head=nexthead;
                q=head;
    Pos(q->x,q->y);
    printf("⊙");
    q=q->next;
                while(q!=NULL)
                {
                    Pos(q->x,q->y);
                    printf("●");
                    q=q->next;
                }
                score=score+add;
                creatfood();
            }
            else                               
            {
                nexthead->next=head;
                head=nexthead;
                q=head;
    Pos(q->x,q->y);
    printf("⊙");
    q=q->next;
                while(q->next->next!=NULL)
                {
                    Pos(q->x,q->y);
                    printf("●");
                    q=q->next;        
                }
                Pos(q->next->x,q->next->y);
                printf("  ");
                free(q->next);
                q->next=NULL;
            }
        }
        if(status==L)
        {
            nexthead->x=head->x-2;
            nexthead->y=head->y;
            if(nexthead->x==food->x && nexthead->y==food->y)
            {
                nexthead->next=head;
                head=nexthead;
                q=head;
    Pos(q->x,q->y);
    printf("⊙");
    q=q->next;
                while(q!=NULL)
                {
                    Pos(q->x,q->y);
                    printf("●");
                    q=q->next;
                }
                score=score+add;
                creatfood();
            }
            else                              
            {
                nexthead->next=head;
                head=nexthead;
                q=head;
    Pos(q->x,q->y);
    printf("⊙");
    q=q->next;
                while(q->next->next!=NULL)
                {
                    Pos(q->x,q->y);
                    printf("●");
                    q=q->next;        
                }
                Pos(q->next->x,q->next->y);
                printf("  ");
                free(q->next);
                q->next=NULL;
            }
        }
        if(status==R)
        {
            nexthead->x=head->x+2;
            nexthead->y=head->y;
            if(nexthead->x==food->x && nexthead->y==food->y)
            {
                nexthead->next=head;
                head=nexthead;
                q=head;
    Pos(q->x,q->y);
    printf("⊙");
    q=q->next;
                while(q!=NULL)
                {
                    Pos(q->x,q->y);
                    printf("●");
                    q=q->next;
                }
                score=score+add;
                creatfood();
            }
            else                                    
            {
                nexthead->next=head;
                head=nexthead;
                q=head;
    Pos(q->x,q->y);
    printf("⊙");
    q=q->next;  
                while(q->next->next!=NULL)
                {
                    Pos(q->x,q->y);
                    printf("●");
                    q=q->next;        
                }
                Pos(q->next->x,q->next->y);
                printf("  ");
                free(q->next);
                q->next=NULL;
            }
        }
        if(biteself()==1)      
        {
            endgamestatus=2;
        }
    }
    void pause()
    {
        while(1)
        {
            Sleep(300);
            if(GetAsyncKeyState(VK_SPACE))
            {
                break;
            }
        }
    }
    
    
    void welcometogame()
    {  
    Pos(40,12);
    printf("欢迎来到贪食蛇游戏!");
    Pos(40,25);
    system("pause");
    system("cls");
    }
    
    
    void caozuo()
    {
    Pos(64,16);
        printf("你可以用↑.↓.←.→分别控制蛇的移动.");
    Pos(64,18);
        printf("ESC :退出游戏.space:暂停游戏.");
    Pos(64,20);
    printf("F1:减慢蛇的运动速度 F2:加快它的速度");
    status=R;
    while(1)
        {
            Pos(64,10);
            printf("得分:%d  ",score);
            Pos(64,11);
            printf("每个食物得分:%d分",add);
    Pos(64,12);
            printf("最高分:%d分",maxscore);
            if(GetAsyncKeyState(VK_UP) && status!=D)
            {
                status=U;
            }
            else if(GetAsyncKeyState(VK_DOWN) && status!=U)
            {
                status=D;
            }
            else if(GetAsyncKeyState(VK_LEFT)&& status!=R)
            {
                status=L;
            }
            else if(GetAsyncKeyState(VK_RIGHT)&& status!=L)
            {
                status=R;
            }
            else if(GetAsyncKeyState(VK_SPACE))
            {
                pause();
            }
            else if(GetAsyncKeyState(VK_ESCAPE))
            {
                endgamestatus=3;
                break;
            }
    else if(GetAsyncKeyState(VK_F1))
            {
                if(sleeptime>=50)
                {
                    sleeptime=sleeptime-30;
                    add=add+2;
                    if(sleeptime==320)
                    {
                        add=2;
                    }
                }
            }
            else if(GetAsyncKeyState(VK_F2))
            {
                if(sleeptime<350)
                {
                    sleeptime=sleeptime+30;
                    add=add-2;
                    if(sleeptime==350)
                    {
                        add=1; 
                    }
                }
            }
    Sleep(sleeptime);
            snakemove();
    if(endgamestatus!=0)
    {
    system("cls");
    Pos(24,12);
    if(endgamestatus==1)
    {
    printf("对不起,您撞到墙了,游戏结束.");
    }
    else if(endgamestatus==2)
    {
    printf("对不起,您咬到自己了,游戏结束.");
    }
    else if(endgamestatus==3)
    {
    printf("您结束了游戏。");
    }
    Pos(24,13);
    if(maxscore<score)
    {
    printf("恭喜你打破了记录并成为了最高分!");
                      maxscore= score;
    }
    Pos(24,14);
    printf("您的得分是%d\n",score);
    endgamestatus=0;
    system("pause");
    break;
    }
    }
    }
    void gamestart()
    {
        system("mode con cols=100 lines=30");
        welcometogame();
        creatmap();
        creatsnake();
        creatfood();
    }
    int main()
    {
    while(1)
    {
    gamestart();
    caozuo();
    }
    return 0;
    }


  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
很抱歉,作为AI语言模型,我无法提供您所需的代码。不过,以下是一个简单的贪吃蛇游戏的思路和实现方法,希望对您有所帮助。 思路: 1. 初始化:生成蛇的初始位置和食物的位置; 2. 游戏循环:每次循环,先检测用户输入是否改变了蛇的移动方向,然后移动蛇的头部和身体,并检测是否吃到了食物,如果吃到了,则生成新的食物,并在蛇的尾部添加一个新的身体;如果撞到了墙或者自己的身体,则游戏结束; 3. 实现方式:可以使用二维数组来表示游戏区域,用数字表示蛇的身体、食物和空白位置,然后在循环中根据数组来渲染游戏界面。 实现: 以下代码是一个简单的贪吃蛇游戏的实现,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> // 游戏区域的大小 #define ROW 20 #define COL 20 // 蛇的最大长度 #define MAX_LEN ROW * COL // 游戏区域和蛇的表示 int arena[ROW][COL]; int snake[MAX_LEN][2]; int head, tail; int len; int dx, dy; // 食物的位置 int food_x, food_y; // 初始化游戏 void init() { // 初始化游戏区域 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { arena[i][j] = 0; } } // 初始化蛇 len = 3; head = 0; tail = 0; dx = 0; dy = 1; snake[0][0] = ROW / 2; snake[0][1] = COL / 2; arena[snake[0][0]][snake[0][1]] = 1; for (int i = 1; i < len; i++) { snake[i][0] = snake[i - 1][0] + dx; snake[i][1] = snake[i - 1][1] + dy; arena[snake[i][0]][snake[i][1]] = 1; } // 初始化食物 srand(time(NULL)); food_x = rand() % ROW; food_y = rand() % COL; while (arena[food_x][food_y] != 0) { food_x = rand() % ROW; food_y = rand() % COL; } arena[food_x][food_y] = 2; } // 显示游戏界面 void render() { system("cls"); printf("Welcome to Snake Game!\n"); for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (arena[i][j] == 0) { printf(" "); } else if (arena[i][j] == 1) { printf("O "); } else if (arena[i][j] == 2) { printf("* "); } } printf("\n"); } } // 检测用户输入 void check_input() { if (_kbhit()) { char ch = _getch(); if (ch == 'w') { dx = -1; dy = 0; } else if (ch == 's') { dx = 1; dy = 0; } else if (ch == 'a') { dx = 0; dy = -1; } else if (ch == 'd') { dx = 0; dy = 1; } } } // 移动蛇 void move() { // 计算蛇头的新位置 int new_x = snake[head][0] + dx; int new_y = snake[head][1] + dy; // 检测是否吃到食物 if (arena[new_x][new_y] == 2) { len++; snake[tail][0] = new_x; snake[tail][1] = new_y; tail = (tail + 1) % MAX_LEN; food_x = rand() % ROW; food_y = rand() % COL; while (arena[food_x][food_y] != 0) { food_x = rand() % ROW; food_y = rand() % COL; } arena[food_x][food_y] = 2; } else { // 计算蛇尾的新位置 int old_x = snake[tail][0]; int old_y = snake[tail][1]; arena[old_x][old_y] = 0; snake[tail][0] = new_x; snake[tail][1] = new_y; tail = (tail + 1) % MAX_LEN; } // 检测是否撞墙或者撞到自己的身体 if (new_x < 0 || new_x >= ROW || new_y < 0 || new_y >= COL || arena[new_x][new_y] == 1) { printf("Game Over!\n"); exit(0); } // 更新蛇头的位置 snake[head][0] = new_x; snake[head][1] = new_y; arena[new_x][new_y] = 1; } // 主函数 int main() { init(); while (1) { render(); check_input(); move(); Sleep(100); } return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值