200行左右
时间: 2024-01-08 07:03:19 浏览: 29
好的,下面是一个比较完整的吃豆人游戏C语言代码,大概有200多行:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#define MAP_WIDTH 24
#define MAP_HEIGHT 24
#define CELL_WIDTH 20
#define CELL_HEIGHT 20
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
enum CELL_TYPE {
CELL_EMPTY,
CELL_DOT,
CELL_PLAYER,
CELL_WALL
};
//地图
int map[MAP_HEIGHT][MAP_WIDTH] = {0};
//吃豆人的位置和方向
int player_x, player_y, player_dir;
//分数
int score = 0;
//豆子数量
int dot_num = 0;
//游戏结束标志
int game_over = 0;
void init_map(); //初始化地图
void draw_map(); //绘制地图
void draw_score(); //绘制分数
void update(); //更新游戏
void move_player(int dir); //移动吃豆人
void check_dot(); //检查是否吃到豆子
void check_game_over(); //检查游戏是否结束
int main() {
srand(time(NULL)); //设置随机数种子
init_map(); //初始化地图
while (!game_over) { //游戏未结束
draw_map(); //绘制地图
draw_score(); //绘制分数
update(); //更新游戏
check_game_over(); //检查游戏是否结束
Sleep(100); //暂停100毫秒
}
printf("\n\nGame Over! Your score is %d.", score); //输出分数
return 0;
}
void init_map() {
//随机生成豆子和墙壁
for (int i = 0; i < MAP_HEIGHT; i++) {
for (int j = 0; j < MAP_WIDTH; j++) {
if (i == 0 || i == MAP_HEIGHT - 1 || j == 0 || j == MAP_WIDTH - 1) {
map[i][j] = CELL_WALL; //边界设置为墙壁
} else {
if (rand() % 10 == 0) {
map[i][j] = CELL_DOT; //设置豆子标记
dot_num++; //豆子数量加1
} else if (rand() % 20 == 0) {
map[i][j] = CELL_WALL; //设置墙壁标记
}
}
}
}
//随机生成吃豆人的位置和方向
do {
player_x = rand() % MAP_HEIGHT;
player_y = rand() % MAP_WIDTH;
} while (map[player_x][player_y] != CELL_EMPTY);
player_dir = rand() % 4 + 1;
map[player_x][player_y] = CELL_PLAYER; //设置吃豆人标记
}
void draw_map() {
system("cls"); //清屏
for (int i = 0; i < MAP_HEIGHT; i++) {
for (int j = 0; j < MAP_WIDTH; j++) {
switch (map[i][j]) {
case CELL_EMPTY:
printf(" ");
break;
case CELL_DOT:
printf("o");
break;
case CELL_PLAYER:
printf("C");
break;
case CELL_WALL:
printf("#");
break;
}
printf(" ");
}
printf("\n");
}
}
void draw_score() {
printf("Score: %d\n", score);
}
void update() {
if (_kbhit()) { //检测是否有键盘输入
int key = _getch(); //获取键盘输入
switch (key) {
case 'w':
move_player(UP); //向上移动
break;
case 's':
move_player(DOWN); //向下移动
break;
case 'a':
move_player(LEFT); //向左移动
break;
case 'd':
move_player(RIGHT); //向右移动
break;
}
} else {
move_player(player_dir); //沿着当前方向移动
}
}
void move_player(int dir) {
map[player_x][player_y] = CELL_EMPTY; //清除原位置的标记
switch (dir) {
case UP:
if (map[player_x - 1][player_y] != CELL_WALL) {
player_x--; //向上移动
}
break;
case DOWN:
if (map[player_x + 1][player_y] != CELL_WALL) {
player_x++; //向下移动
}
break;
case LEFT:
if (map[player_x][player_y - 1] != CELL_WALL) {
player_y--; //向左移动
}
break;
case RIGHT:
if (map[player_x][player_y + 1] != CELL_WALL) {
player_y++; //向右移动
}
break;
}
map[player_x][player_y] = CELL_PLAYER; //设置新位置的标记
player_dir = dir; //更新方向
check_dot(); //检查是否吃到豆子
}
void check_dot() {
if (map[player_x][player_y] == CELL_DOT) { //吃到豆子
map[player_x][player_y] = CELL_EMPTY; //清除豆子标记
score += 10; //分数加10
dot_num--; //豆子数量减1
if (dot_num == 0) { //所有豆子被吃完
game_over = 1; //游戏结束
}
}
}
void check_game_over() {
if (player_x == 0 || player_x == MAP_HEIGHT - 1 || player_y == 0 || player_y == MAP_WIDTH - 1) { //撞墙
game_over = 1; //游戏结束
}
}
```
这个版本的吃豆人游戏相对于之前的版本,增加了更多的功能和细节,例如墙壁的随机生成、游戏结束判断、吃到豆子的判断和处理等。同时,代码也更加模块化,易于阅读和维护。