用c语言写出贪吃蛇代码,包括数组,指针,文件,结构体
时间: 2024-03-24 21:37:02 浏览: 114
好的,以下是一个用C语言实现的贪吃蛇游戏的代码,其中包括了数组、指针、文件和结构体。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define MAP_WIDTH 20
#define MAP_HEIGHT 20
#define EMPTY 0
#define SNAKE_HEAD 1
#define SNAKE_BODY 2
#define FOOD 3
typedef struct Snake {
int x, y;
struct Snake *next;
} Snake;
int map[MAP_WIDTH][MAP_HEIGHT];
Snake *createSnake(int x, int y);
void destroySnake(Snake *snake);
void generateFood();
void moveSnake(Snake *snake, int direction);
void updateMap(Snake *snake);
void render();
void gameOver(int score);
void saveScore(int score);
int loadScore();
int main() {
// 初始化游戏地图
for (int i = 0; i < MAP_WIDTH; i++) {
for (int j = 0; j < MAP_HEIGHT; j++) {
map[i][j] = EMPTY;
}
}
// 创建蛇
Snake *snake = createSnake(MAP_WIDTH/2, MAP_HEIGHT/2);
// 生成食物
generateFood();
// 初始化分数
int score = 0;
// 游戏循环
while (true) {
// 获取键盘输入
if (kbhit()) {
int key = getch();
if (key == 'w' || key == 'W' || key == 72) {
moveSnake(snake, 0);
} else if (key == 's' || key == 'S' || key == 80) {
moveSnake(snake, 1);
} else if (key == 'a' || key == 'A' || key == 75) {
moveSnake(snake, 2);
} else if (key == 'd' || key == 'D' || key == 77) {
moveSnake(snake, 3);
}
}
// 更新蛇的位置
Snake *current = snake;
while (current != NULL) {
Snake *next = current->next;
if (next != NULL) {
current->x = next->x;
current->y = next->y;
}
current = next;
}
// 检测碰撞
if (map[snake->x][snake->y] == SNAKE_BODY || map[snake->x][snake->y] == SNAKE_HEAD || snake->x < 0 || snake->x >= MAP_WIDTH || snake->y < 0 || snake->y >= MAP_HEIGHT) {
gameOver(score);
break;
} else if (map[snake->x][snake->y] == FOOD) {
score++;
generateFood();
Snake *newTail = createSnake(snake->x, snake->y);
Snake *current = snake;
while (current != NULL) {
if (current->next == NULL) {
current->next = newTail;
break;
}
current = current->next;
}
}
// 更新地图
updateMap(snake);
// 渲染画面
render();
// 等待一段时间
Sleep(100);
}
// 释放蛇的内存
destroySnake(snake);
return 0;
}
Snake *createSnake(int x, int y) {
Snake *snake = (Snake *)malloc(sizeof(Snake));
snake->x = x;
snake->y = y;
snake->next = NULL;
map[x][y] = SNAKE_HEAD;
return snake;
}
void destroySnake(Snake *snake) {
while (snake != NULL) {
Snake *next = snake->next;
free(snake);
snake = next;
}
}
void generateFood() {
int x, y;
do {
x = rand() % MAP_WIDTH;
y = rand() % MAP_HEIGHT;
} while (map[x][y] != EMPTY);
map[x][y] = FOOD;
}
void moveSnake(Snake *snake, int direction) {
switch (direction) {
case 0:
snake->y--;
break;
case 1:
snake->y++;
break;
case 2:
snake->x--;
break;
case 3:
snake->x++;
break;
}
}
void updateMap(Snake *snake) {
for (int i = 0; i < MAP_WIDTH; i++) {
for (int j = 0; j < MAP_HEIGHT; j++) {
map[i][j] = EMPTY;
}
}
Snake *current = snake;
while (current != NULL) {
map[current->x][current->y] = SNAKE_BODY;
current = current->next;
}
map[snake->x][snake->y] = SNAKE_HEAD;
map[food.x][food.y] = FOOD;
}
void render() {
system("cls");
for (int i = 0; i < MAP_WIDTH; i++) {
for (int j = 0; j < MAP_HEIGHT; j++) {
if (map[i][j] == EMPTY) {
printf(" ");
} else if (map[i][j] == SNAKE_HEAD) {
printf("@");
} else if (map[i][j] == SNAKE_BODY) {
printf("#");
} else if (map[i][j] == FOOD) {
printf("$");
}
}
printf("\n");
}
printf("Score: %d\n", score);
}
void gameOver(int score) {
printf("Game Over!\n");
printf("Score: %d\n", score);
saveScore(score);
exit(0);
}
void saveScore(int score) {
FILE *fp = fopen("score.txt", "w");
if (fp == NULL) {
printf("Failed to save score.\n");
} else {
fprintf(fp, "%d", score);
fclose(fp);
}
}
int loadScore() {
FILE *fp = fopen("score.txt", "r");
if (fp == NULL) {
printf("Failed to load score.\n");
return 0;
} else {
int score;
fscanf(fp, "%d", &score);
fclose(fp);
return score;
}
}
```
以上是一个简单的用C语言实现的贪吃蛇游戏的代码,其中包括了数组、指针、文件和结构体的使用。请注意,此代码可能需要在Windows环境下编译运行。
阅读全文