C语言命令行小游戏之贪吃蛇

  • Post author:
  • Post category:其他


这是一个通过链表实现的命令行小游戏——贪吃蛇。

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

//声明储存蛇坐标的链表
struct Snake{
    int position[2];
    struct Snake * next;
};

//游戏地图
char Game[12][36];
//记录玩家的默认坐标
int PlayerPosition[2]={5,16};
//玩家的上一个坐标
int BeforePosition[2];
//食物坐标
int FoodPosition[2];

//蛇头的头节点
struct Snake *Headf = NULL;
//蛇的长度,默认为1
int SnakeLength=1;
//分数
int Score = 0;
//游戏是否结束的变量
bool IsOver = false;

//随机生成食物
void Food(){
    //随机生成食物的坐标
    srand((unsigned int)time(NULL));
    // % (n - m + 1) + m;//生成m~n的随机数
    FoodPosition[1] = rand()% 34 + 1;
    FoodPosition[0]  = rand()% 10 + 1;
    
    //将食物在地图中生成
    Game[FoodPosition[0]][FoodPosition[1]] = '$';
}

//创建蛇身体,通过链表进行链接每一块身体
struct Snake *CreatBody(struct Snake *Head,int position[2]){
    struct Snake *head;
    struct Snake *p1,*p2;
    
    head = Head;

    p1 = (struct Snake *)malloc(sizeof(struct Snake));
    p1->position[0] = position[0];p1->position[1]=position[1];
    p1->next = NULL;
    
    //判断头部是否创建,创建则生成身体
    if(head==NULL){
        head = p1;
    }else{

        p2 = head;
        while(p2->next!=NULL){
            p2 = p2->next;
        }
        p2->next = p1;
        // printf("Body Created 1st\n");
    }

    return head;
}

//绘出游戏界面
void DrawUI(struct Snake *Head){

    struct Snake *p1=Head;
    printf("Score:%d\n",Score);

    p1 = Head;
    //先确定蛇的长度
    while (p1->next!=NULL)
    { 
        p1 = p1->next;
        SnakeLength+=1;
        // printf("Got SnakeLength!%d\n",SnakeLength);
    }
    p1 = Head;
    //根据蛇的长度,读取链表,将蛇添加在游戏界面里
    for(int i = 0; i < SnakeLength; i++){
        Game[p1->position[0]][p1->position[1]]='+';
        p1 = p1->next;
        // printf("Draw Snake!\n");
    }
    SnakeLength =1 ;

    //画出游戏地图
    for (int i = 0; i < 12; i++)
    {
       for(int j = 0; j < 36; j++){
            printf("%c",Game[i][j]);
       }
       printf("\n");
    }

}


//蛇的移动逻辑
void Move(struct Snake *Head){
    struct Snake *p1,*p2;
    p1 = Head;p2 = Head;
    //方向
    char Direction;
    int pos[2];
    int BeforePos[2];
    BeforePos[0]  = p1->position[0];BeforePos[1]  = p1->position[1];

    Direction = getchar();

    if(BeforePos[0]== 0|| BeforePos[0] ==11 || BeforePos[1]==0|| BeforePos[1]==35){
        IsOver = true;
    }

    if(Direction == 'w'){
            if(Score == 0){
                p1->position[0] -= 1;
                Game[BeforePos[0]][BeforePos[1]]=' ';
            }else{
                p1 = Head;
                p2 = (struct Snake*)malloc(sizeof(struct Snake));
                p2->position[0]=p1->position[0];
                p2->position[1]=p1->position[1];
                struct Snake *p3;
                p3 = p1->next;
                p1->next = p2;
                p2 ->next = p3;
                p1 = Head;
                while (p1->next != NULL)
                {
                    p2 = p1;
                    p1 = p1->next;                    
                }
                Game[p1->position[0]][p1->position[1]]=' ';
                BeforePosition[0]=p1->position[0];
                BeforePosition[1]=p1->position[1];
                p2->next = NULL;
                free(p1);
                p1 = Head;                
                p1->position[0] -= 1;
            }
                    
                    
    }
    if(Direction == 's'){
        if(Score == 0){
                p1 = Head;
                p1->position[0] += 1;
                Game[BeforePos[0]][BeforePos[1]]=' ';
        }else{
            p1 = Head;
                p2 = (struct Snake*)malloc(sizeof(struct Snake));
                p2->position[0]=p1->position[0];
                p2->position[1]=p1->position[1];
                struct Snake *p3;
                p3 = p1->next;
                p1->next = p2;
                p2 ->next = p3;
                p1 = Head;
                while (p1->next != NULL)
                {
                    p2 = p1;
                    p1 = p1->next;
                }
                BeforePosition[0]=p1->position[0];
                BeforePosition[1]=p1->position[1];
                 Game[p1->position[0]][p1->position[1]]=' ';
                p2->next = NULL;
                free(p1);
                p1 = Head;                
            p1->position[0] += 1;
        }
    }
    if(Direction == 'a'){
        if(Score == 0){
            p1 = Head;
            p1->position[1] -= 1;
            Game[BeforePos[0]][BeforePos[1]]=' ';
        }else{
            p1 = Head;
                p2 = (struct Snake*)malloc(sizeof(struct Snake));
                p2->position[0]=p1->position[0];
                p2->position[1]=p1->position[1];
                struct Snake *p3;
                p3 = p1->next;
                p1->next = p2;
                p2 ->next = p3;
                p1 = Head;
                while (p1->next != NULL)
                {
                    p2 = p1;
                    p1 = p1->next;
                    // Game[p1->position[0]][p1->position[1]]=' ';
                    
                }
                BeforePosition[0]=p1->position[0];
                BeforePosition[1]=p1->position[1];
                 Game[p1->position[0]][p1->position[1]]=' ';
                p2->next = NULL;
                free(p1);
                p1 = Head;                
            p1->position[1] -= 1;
        }
    }
    if(Direction == 'd'){
        if(Score == 0){
            p1 = Head;
            p1->position[1] += 1;
            Game[BeforePos[0]][BeforePos[1]]=' ';
        }else{
            p1 = Head;
                p2 = (struct Snake*)malloc(sizeof(struct Snake));
                p2->position[0]=p1->position[0];
                p2->position[1]=p1->position[1];
                struct Snake *p3;
                p3 = p1->next;
                p1->next = p2;
                p2 ->next = p3;
                p1 = Head;
                while (p1->next != NULL)
                {
                    p2 = p1;
                    p1 = p1->next;
                    // Game[p1->position[0]][p1->position[1]]=' ';
                    
                }
                BeforePosition[0]=p1->position[0];
                BeforePosition[1]=p1->position[1];
                 Game[p1->position[0]][p1->position[1]]=' ';
                p2->next = NULL;
                free(p1);
                p1 = Head;               
            p1->position[1] += 1;
        }
    } 
    
}

int main(){
    struct Snake *head;
    head = CreatBody(Headf,PlayerPosition);
    for (int i = 0; i < 12; i++)
    {
       for(int j = 0; j < 36; j++){
            if(i==0 || j==0 || j==35 || i==11)Game[i][j] = 'x';else Game[i][j]=' ';
       }
    }
    Food();
    while (!IsOver)
    {
        struct Snake *p1 = head;
        DrawUI(head);
        Move(head);
        if(p1->position[0] == FoodPosition[0]&& p1->position[1] == FoodPosition[1]){
            //Game[BeforePosition[0]][BeforePosition[1]] = '*';
            Score+=1;
            Food();
            head = CreatBody(head,BeforePosition);
        }
    }
    printf("Game Over!\n");
}



版权声明:本文为qq_35556658原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。