C语言实现经典贪吃蛇小游戏(控制台、单链表)

个人用原生c语言写的小游戏,才疏学浅,代码有很多不足,仅供给初学者参考。

介绍

  1. 使用VS2010专业版开发。
  2. 整体思路是在大循环内每0.2秒执行一次【蛇移动】、【判断蛇的状态】、【随机生成食物】、【检查键盘输入】几个模块。
  3. 蛇用到单链表结构。
  4. 标题、光标等相关的函数参考了网上大佬们的写法,在此感谢。

不足之处

  1. 实现了蛇撞墙和撞到自己两种死亡判断,但是没有实现游戏胜利。
  2. 蛇移动的时间和食物出现的时间固定,没有实现游戏难度的选择。
  3. 没有实现游戏菜单,包括游戏结束后记录游戏排名,重新开始游戏等。

源码下载

  • 完整源码请戳这里下载!(src文件夹中有一个误上传的LInkList.c文件,请忽略~)

主要模块代码展示

  • Snake.c文件
#include "GameLib.h"
#include "Snake.h"

/*
* 主要模块:检查键盘输入,移动蛇的位置,随机生成豆豆,Sleep
*/

int ret;            // 蛇状态的开关

int main()
{
    int _close;     // 控制程序开关
    int key;        // 键盘输入
    int drct;       // 1-UP 2-RIGHT 3-DOWN 4-LEFT
    int count;      // 生成食物的时间缓冲
    Snake snake;
    COORD pos,pos2,pos3;    // 蛇的初始点
    Food food;

    pos.X = MAP_LEFT +  MAP_WIDTH / 2;
    pos.Y = MAP_TOP + MAP_HEIGHT / 2;
    pos2.X = MAP_LEFT +  MAP_WIDTH / 2 - 1;
    pos2.Y = MAP_TOP + MAP_HEIGHT / 2;
    pos3.X = MAP_LEFT +  MAP_WIDTH / 2 - 2;
    pos3.Y = MAP_TOP + MAP_HEIGHT / 2;


    initSnake(&snake);
    addSnake(&snake, pos);
    addSnake(&snake, pos2);
    addSnake(&snake, pos3);

    _close = 1;
    ret = 0;
    key = 4;
    count = 0;

    SetTitle("Seven's Snake");
    showMap();
    showSnake(&snake);
    showFood(&food,&count);

    while(_close)
    {   
        if(ret == 1)
        {
            printf("撞墙死亡!");
            getch();
        }
        if(ret == 2)
        {
            printf("撞自己死亡!");
            getch();
        }
        else 
        {
            if(count >= 50)
            {
                showFood(&food,&count);
            }
            count++;
            eatFood(&snake, &food, &count);
            moveSnake(&snake,key);
            Sleep(200);
            if(kbhit()) key = keyword();
            deadSnake(&snake);
        }
    }

    termSnake(&snake);
}

void showFood(Food *food, int *count)
{
    int x_max = MAP_WIDTH + MAP_LEFT + 2;
    int x_min = MAP_LEFT + 1;
    int y_max = MAP_HEIGHT + MAP_TOP + 2;
    int y_min = MAP_TOP + 1;

    *count = 0;
    clearFood(food);
    srand(time(0));
    food->pos.X = rand() % (x_max - x_min) + x_min;
    food->pos.Y = rand() % (y_max - y_min) + y_min;
    SetPosition(food->pos.X, food->pos.Y);
    printf("o");

    SetPosition(0,0);

    return;
}

void clearFood(Food *food)
{
    SetPosition(food->pos.X, food->pos.Y);
    printf(" ");
    food->pos.X = 0;
    food->pos.Y = 0;

    SetPosition(0,0);

    return;
}

void eatFood(Snake *snake, Food *food, int *count)
{
    if(snake->head->pos.X == food->pos.X && snake->head->pos.Y == food->pos.Y)
    {
        addSnake(snake, food->pos);
        showFood(food,count);
    }

    return;
}

void showMap()
{
    int i;
    SetPosition(MAP_LEFT, MAP_TOP);
    for(i = 0; i < MAP_WIDTH / 2 + 2; i++)
    {
        printf("* ");
    }
    SetPosition(MAP_LEFT, MAP_TOP + MAP_HEIGHT + 2);
    for(i = 0; i < MAP_WIDTH / 2 + 2; i++)
    {
        printf("* ");
    }
    for(i = 0; i < MAP_HEIGHT + 2; i++)
    {
        SetPosition(MAP_LEFT, MAP_TOP + i);
        printf("*");
        SetPosition(MAP_LEFT + MAP_WIDTH + 2, MAP_TOP + i);
        printf("*");
    }
}


void showSnake(Snake *snake)
{
    SnakeNode *index;
    index = snake->head;

    while(index != NULL)
    {
        SetPosition(index->pos.X,index->pos.Y);
        printf("*");
        index = index->next;
    }

    SetPosition(0,0);

    return;
}

void initSnake(Snake *snake)
{
    snake->count = 0;
    snake->head = NULL;

    return;
}

void addSnake(Snake *snake, COORD pos)
{
    SnakeNode *pnew;

    pnew = (SnakeNode *)malloc(sizeof(SnakeNode));
    if(pnew == NULL)
    {
        printf("申请空间失败!\n");
        return;
    }

    if(snake->count == 0 && snake->head == NULL)
    {
        pnew->next = NULL;
    }
    else
    {
        pnew->next = snake->head;
    }

    pnew->pos = pos;
    snake->head = pnew;
    snake->count++;

    return;

}

COORD deleteSnake(Snake *snake)
{
    COORD pos;
    SnakeNode *index,*pre;
    index = snake->head;
    if(snake->count > 0)
    {
        while(index->next != NULL)
        {
            pre = index;
            index = index->next;
        }
        pre->next = NULL;
        snake->count--;
        pos = index->pos;
        free(index);
    }
    return pos;
}

void moveSnake(Snake *snake, int drct)
{
    COORD pos;
    SnakeNode *index = snake->head;

    if(snake->head != NULL)
    {
        pos = snake->head->pos;
        if(drct == 1)
        {
            pos.Y--;
        }
        else if(drct == 2)
        {
            pos.X++;
        }
        else if(drct == 3)
        {
            pos.Y++;
        }
        else if(drct == 4)
        {
            pos.X--;
        }
        else
        {
            printf("非法输入");
            return;
        }

        // 撞自己
        while(index != NULL)
        {
            if(index->pos.X == pos.X && index->pos.Y == pos.Y)
            {
                ret = 2;
                return;
            }
            index = index->next;
        }


        addSnake(snake, pos);
        SetPosition(pos.X,pos.Y);
        printf("*");
        pos = deleteSnake(snake);
        SetPosition(pos.X,pos.Y);
        printf(" ");
        SetPosition(0,0);
    }
    return;
}

void deadSnake(Snake *snake)
{
    // 撞墙
    if(snake->head->pos.X <= MAP_LEFT || snake->head->pos.X >= MAP_LEFT + MAP_WIDTH + 2
        || snake->head->pos.Y <= MAP_TOP || snake->head->pos.Y >= MAP_TOP + MAP_HEIGHT + 2)
    {
        ret = 1;
        return;
    }

    return;
}

void termSnake(Snake *snake)
{
    SnakeNode *index;

    while(snake->head != NULL)
    {
        index = snake->head->next;
        free(snake->head);
        snake->head = index;
        snake->count--;
    }

    return;
}

int keyword()
{
    int key;        // 键盘输入

    key = getch();
    if(key == 224)
    {
        key = getch();
        if(key == KEY_UP)
        {
            return 1;
        }
        else if(key == KEY_RIGHT)
        {
            return 2;
        }
        else if(key == KEY_DOWN)
        {
            return 3;
        }
        else if(key == KEY_LEFT)
        {
            return 4;
        }
    }

    return 0;
}
  • Snake.h文件
#include 
#include 
#include 

#define KEY_UP 72
#define KEY_RIGHT 77
#define KEY_DOWN 80
#define KEY_LEFT 75

#define MAP_LEFT 3
#define MAP_TOP 1
#define MAP_WIDTH 40
#define MAP_HEIGHT 20

#define MAP_SIZE (40 + 1) * (20 + 1)

#ifndef _SNAKE_H_
#define _SNAKE_H_

typedef struct Snake Snake;
typedef struct SnakeNode SnakeNode;
typedef struct Food Food;

// 链表
struct Snake
{
    SnakeNode *head;    //记录点坐标的链表
    int count;          //链表长度
};

// 结点
struct SnakeNode
{
    COORD pos;
    SnakeNode *next;
};

struct Food
{
    COORD pos;
};

void showFood(Food *food, int *count);
void clearFood(Food *food);
void eatFood(Snake *snake, Food *food, int *count);

void showMap();
void showSnake();

int keyword();

void initSnake(Snake *snake);
void termSnake(Snake *snake);
void addSnake(Snake *snake, COORD pos);
COORD deleteSnake(Snake *snake);
void moveSnake(Snake *snake, int drct);
void deadSnake(Snake *snake);

#endif //_SNAKE_H_

你可能感兴趣的:(c语言)