新闻动态 你的位置:九游人工客服中心电话号码 > 新闻动态 > c语言贪吃蛇游戏代码QZ

c语言贪吃蛇游戏代码QZ

发布日期:2025-02-04 06:50    点击次数:96

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <windows.h>

// 游戏区域的大小

#define WIDTH 60

#define HEIGHT 20

// 蛇的移动方向

typedef enum {

    UP,

    DOWN,

    LEFT,

    RIGHT

} Direction;

// 食物的结构体

typedef struct {

    int x;

    int y;

} Food;

// 蛇的结构体

typedef struct {

    int x[WIDTH * HEIGHT];

    int y[WIDTH * HEIGHT];

    int length;

    Direction dir;

} Snake;

// 初始化蛇

void initSnake(Snake *snake) {

    snake->x[0] = WIDTH / 2;

    snake->y[0] = HEIGHT / 2;

    snake->length = 1;

    snake->dir = RIGHT;

}

// 初始化食物

void initFood(Food *food) {

    food->x = rand() % WIDTH;

    food->y = rand() % HEIGHT;

}

// 判断蛇是否吃到食物

int isEatFood(Snake snake, Food food) {

    if (snake.x[0] == food.x && snake.y[0] == food.y) {

        return 1;

    }

    return 0;

}

// 判断蛇是否撞到自己或边界

int isGameOver(Snake snake) {

    // 撞到边界

    if (snake.x[0] < 0

Powered by 九游人工客服中心电话号码 @2013-2022 RSS地图 HTML地图

Copyright Powered by365站群 © 2013-2024