运用C语言实现贪吃蛇小游戏

废话不多说直接上代码↓↓↓

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

// 欢迎界面
void welcome();
// 绘制游戏边界
void border();
// 设置光标位置
void Pos(int x, int y);
// 获取随机生成的食物位置
void getRandomPos(int *x, int *y);
// 初始化蛇的位置
void initSnake();
// 创建食物
void createFood();
// 验证指定位置是否在蛇身上
int inSnake(int x, int y);
// 蛇移动
void snakeMove();
// 获取玩家输入的方向
int getDirection();
// 设置游戏速度
void setGameSpeed(int difficulty);
// 游戏界面
void gameInterface();

// 蛇的每个节点的结构体
typedef struct Node {
    int x;
    int y;
    struct Node* next;
} node;

node* head; // 蛇头节点指针
node food; // 食物节点
int reCreateFood; // 是否需要重新生成食物
int fail; // 游戏是否结束
int score, add = 10; // 分数和每次吃食物增加的分数
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; // 蛇移动的方向
int d = -1; // 玩家输入的方向
int gameSpeed; // 游戏速度

int main() {
    welcome();
    gameInterface();
    return 0;
}

// 欢迎界面
void welcome() {
    system("mode con cols=100 lines=30");
    system("cls");
    
    Pos(38, 6);
    printf("欢迎来到贪吃蛇游戏\n");
    Pos(38, 8);
    printf("↑↓←→控制方向\n");
    Pos(38, 10);
    printf("ESC退出游戏\n");
    Pos(38, 12);
    printf("按Enter键开始游戏\n");
    getchar();
    system("cls");
}

// 绘制游戏边界
void border() {
    for (int i = 0; i < 60; i ++) {
        Pos(i, 0);
        printf("▇");
        Pos(i, 25);
        printf("▇");
    } 
    
    for (int i = 1; i < 25; i ++) {
        Pos(0, i);
        printf("▇");
        Pos(59, i);
        printf("▇");
    }
}

// 设置光标位置
void Pos(int x, int y) {
    COORD pos;
    pos.X = x;
    pos.Y = y;
    
    HANDLE handleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(handleOutput, pos);
}

// 获取随机生成的食物位置
void getRandomPos(int *x, int *y) {
    srand((unsigned int)time(NULL));
    *x = rand() % 58 + 1;
    *y = rand() % 24 + 1;
}

// 初始化蛇的位置
void initSnake() { 
    head = (node*)malloc(sizeof(node));
    head->x = rand() % 58 + 1; 
    head->y = rand() % 24 + 1;
    head->next = NULL;
    
    Pos(head->x, head->y);
    printf("*");
}

// 创建食物
void createFood() {
    int x, y;
    getRandomPos(&x, &y);
    if (inSnake(x, y)) createFood();
    food.x = x;
    food.y = y;
    Pos(x, y);
    printf("★"); // 更改食物外观
}

// 验证指定位置是否在蛇身上
int inSnake(int x, int y) {
    node* p = head->next;
    while (p) {
        if (x == p->x && y == p->y)
            return 1;
        p = p->next;
    }
    return 0;
} 

// 蛇移动
void snakeMove() {
    if (d == -1)
        while (d == -1) d = getDirection();

    node* newHead = (node*)malloc(sizeof(node));
    newHead->x = head->x + dx[d];
    newHead->y = head->y + dy[d];
    newHead->next = head;
    head = newHead;
    
    node* p = head;
    if (inSnake(p->x, p->y) || (p->x == 0 || p->x == 59 || p->y == 0 || p->y == 25))
        fail = 1;
    else if (p->x != food.x || p->y != food.y) {
        Pos(p->x, p->y);
        printf("*");
        
        while (p->next->next) p = p->next;
        Pos(p->next->x, p->next->y);
        printf(" ");
        p->next = NULL;
    } 
    else {
        reCreateFood = 1;
        score += add;
        Pos(p->x, p->y);
        printf("*");
    }
    Pos(61, 25);
    printf("您的得分为:%d", score);
}

// 获取玩家输入的方向
int getDirection() {
    int ch = _getch();
    switch(ch) {
        case 72: // 上
            if (d != 1) return 0;
            break;
        case 80: // 下
            if (d != 0) return 1;
            break;
        case 75: // 左
            if (d != 3) return 2;
            break;
        case 77: // 右
            if (d != 2) return 3;
            break;
        case 27: // ESC
            fail = 1;
            break;
    }
    return d; 
}

// 设置游戏速度
void setGameSpeed(int difficulty) {
    switch (difficulty) {
        case 1:
            gameSpeed = 2;
            break;
        case 2:
            gameSpeed = 5;
            break;
        case 3:
            gameSpeed = 10;
            break;
        default:
            gameSpeed = 5;
            break;
    }
}

// 游戏界面
void gameInterface() {
    Pos(15, 10);
    printf("欢迎来到贪吃蛇游戏");
    Pos(15, 12);
    printf("请选择难度:");
    Pos(18, 14);
    printf("1. 简单");
    Pos(18, 16);
    printf("2. 中等");
    Pos(18, 18);
    printf("3. 困难");
    
    int difficulty;
    while (1) {
        Pos(15, 20);
        printf("请输入对应数字选择难度:");
        scanf("%d", &difficulty);
        if (difficulty >= 1 && difficulty <= 3)
            break;
        else {
            Pos(15, 22);
            printf("输入有误,请重新选择。");
            Sleep(2000);
            system("cls");
            Pos(15, 10);
            printf("欢迎来到贪吃蛇游戏");
            Pos(15, 12);
            printf("请选择难度:");
            Pos(18, 14);
            printf("1. 简单");
            Pos(18, 16);
            printf("2. 中等");
            Pos(18, 18);
            printf("3. 困难");
        }
    }
    
    while (1) {
        system("cls");
        border();
        initSnake();
        createFood();
        setGameSpeed(difficulty);
        fail = 0;
        score = 0;
        reCreateFood = 0;

        while(1) {
            if (fail) break;
            if (reCreateFood) {
                createFood();
                reCreateFood = 0;
            }
            if (_kbhit()) {
                d = getDirection();
            }
            snakeMove();
            Sleep(1000 / gameSpeed);
        }

        Pos(0, 24);
        printf("\n");
        printf("游戏结束!您的得分为:%d\n", score);

        Pos(18, 26);
        printf("继续游戏吗?(Y/N): ");
        char c;
        fflush(stdin);
        scanf("%c", &c);

        if (c != 'Y' && c != 'y')
            break;
    }
}

大家可以在c中直接跑出相应代码,实现效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值