用C语言编写一个图形版的迷宫游戏
时间: 2023-09-18 10:11:41 浏览: 83
要用C语言编写一个图形版的迷宫游戏,可以使用图形库如SDL或OpenGL。以下是一个简单的示例程序,使用SDL库实现迷宫游戏。
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define BLOCK_SIZE 20
#define MAZE_WIDTH (SCREEN_WIDTH / BLOCK_SIZE)
#define MAZE_HEIGHT (SCREEN_HEIGHT / BLOCK_SIZE)
typedef struct {
int x, y;
} Point;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
bool maze[MAZE_WIDTH][MAZE_HEIGHT];
void init_maze() {
for (int i = 0; i < MAZE_WIDTH; i++) {
for (int j = 0; j < MAZE_HEIGHT; j++) {
if (i == 0 || j == 0 || i == MAZE_WIDTH - 1 || j == MAZE_HEIGHT - 1) {
maze[i][j] = true; // 外围设置为墙
} else {
maze[i][j] = rand() % 2; // 随机生成迷宫
}
}
}
}
void draw_maze() {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
for (int i = 0; i < MAZE_WIDTH; i++) {
for (int j = 0; j < MAZE_HEIGHT; j++) {
if (maze[i][j]) { // 绘制墙
SDL_Rect rect = {i * BLOCK_SIZE, j * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE};
SDL_RenderFillRect(renderer, &rect);
}
}
}
SDL_RenderPresent(renderer);
}
bool is_valid_point(Point p) {
return p.x >= 0 && p.x < MAZE_WIDTH && p.y >= 0 && p.y < MAZE_HEIGHT;
}
bool can_move(Point p, int direction) {
switch (direction) {
case 0: // 上
p.y--;
break;
case 1: // 下
p.y++;
break;
case 2: // 左
p.x--;
break;
case 3: // 右
p.x++;
break;
}
return is_valid_point(p) && !maze[p.x][p.y];
}
void move_player(Point *p, int direction) {
switch (direction) {
case 0: // 上
p->y--;
break;
case 1: // 下
p->y++;
break;
case 2: // 左
p->x--;
break;
case 3: // 右
p->x++;
break;
}
}
void play_game() {
Point player = {1, 1};
while (player.x != MAZE_WIDTH - 2 || player.y != MAZE_HEIGHT - 2) { // 直到到达终点
SDL_Event event;
while (SDL_PollEvent(&event)) { // 处理事件
if (event.type == SDL_QUIT) {
return;
}
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_UP:
if (can_move(player, 0)) {
move_player(&player, 0);
}
break;
case SDLK_DOWN:
if (can_move(player, 1)) {
move_player(&player, 1);
}
break;
case SDLK_LEFT:
if (can_move(player, 2)) {
move_player(&player, 2);
}
break;
case SDLK_RIGHT:
if (can_move(player, 3)) {
move_player(&player, 3);
}
break;
}
}
}
draw_maze(); // 绘制迷宫和角色
SDL_Rect rect = {player.x * BLOCK_SIZE, player.y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE};
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
SDL_Delay(16); // 控制游戏帧数
}
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Congratulations", "You win!", window);
}
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
return 1;
}
window = SDL_CreateWindow("Maze", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "SDL_CreateWindow failed: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
fprintf(stderr, "SDL_CreateRenderer failed: %s\n", SDL_GetError());
return 1;
}
init_maze();
draw_maze();
play_game();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
```
这个程序使用了SDL库来创建窗口、渲染器和处理事件。迷宫由一个二维数组表示,墙为true,空地为false。玩家角色用一个Point结构体保存,每次移动时检查是否可以移动。游戏帧数控制为60帧每秒。在到达终点时,弹出一个简单的消息框。
这个示例程序只是一个简单的迷宫游戏,可以根据需要进行修改和扩展。
阅读全文