使用Python语言实现自动随机生成一个迷宫,并能用键盘控制小球完成走迷宫游戏
时间: 2024-03-06 09:48:40 浏览: 34
好的,下面是实现自动随机生成一个迷宫,并能用键盘控制小球完成走迷宫游戏的Python代码。这里我们同样使用pygame和随机数模块random。
```python
import pygame
import random
# 定义迷宫的大小和单元格的大小
MAZE_WIDTH = 20
MAZE_HEIGHT = 15
CELL_SIZE = 30
# 定义迷宫的二维数组
maze = [[0 for y in range(MAZE_HEIGHT)] for x in range(MAZE_WIDTH)]
# 定义迷宫的起点和终点
start_pos = (0, 0)
end_pos = (MAZE_WIDTH - 1, MAZE_HEIGHT - 1)
# 随机生成迷宫
def generate_maze():
# 初始化迷宫的基本结构
for x in range(MAZE_WIDTH):
for y in range(MAZE_HEIGHT):
if x == 0 or x == MAZE_WIDTH - 1 or y == 0 or y == MAZE_HEIGHT - 1:
maze[x][y] = 0
else:
maze[x][y] = 1
# 随机打通迷宫的墙壁
for x in range(1, MAZE_WIDTH - 1):
for y in range(1, MAZE_HEIGHT - 1):
if random.randint(0, 2) == 0:
maze[x][y] = 0
# 设置起点和终点
maze[start_pos[0]][start_pos[1]] = 1
maze[end_pos[0]][end_pos[1]] = 1
# 绘制迷宫
def draw_maze(screen):
for x in range(MAZE_WIDTH):
for y in range(MAZE_HEIGHT):
color = (255, 255, 255)
if maze[x][y] == 0:
color = (0, 0, 0)
pygame.draw.rect(screen, color, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
# 绘制小球
def draw_ball(screen, pos):
x, y = pos
pygame.draw.circle(screen, (255, 0, 0), (x * CELL_SIZE + CELL_SIZE // 2, y * CELL_SIZE + CELL_SIZE // 2), CELL_SIZE // 2)
# 判断小球是否能够移动
def can_move(pos, direction):
x, y = pos
if direction == pygame.K_UP:
if y > 0 and maze[x][y - 1] == 1:
return True
elif direction == pygame.K_DOWN:
if y < MAZE_HEIGHT - 1 and maze[x][y + 1] == 1:
return True
elif direction == pygame.K_LEFT:
if x > 0 and maze[x - 1][y] == 1:
return True
elif direction == pygame.K_RIGHT:
if x < MAZE_WIDTH - 1 and maze[x + 1][y] == 1:
return True
return False
# 判断小球是否到达终点
def is_end(pos):
return pos == end_pos
# 主函数
def main():
pygame.init()
screen = pygame.display.set_mode((MAZE_WIDTH * CELL_SIZE, MAZE_HEIGHT * CELL_SIZE))
pygame.display.set_caption("Maze Game")
generate_maze()
ball_pos = start_pos
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and can_move(ball_pos, pygame.K_UP):
ball_pos = (ball_pos[0], ball_pos[1] - 1)
elif event.key == pygame.K_DOWN and can_move(ball_pos, pygame.K_DOWN):
ball_pos = (ball_pos[0], ball_pos[1] + 1)
elif event.key == pygame.K_LEFT and can_move(ball_pos, pygame.K_LEFT):
ball_pos = (ball_pos[0] - 1, ball_pos[1])
elif event.key == pygame.K_RIGHT and can_move(ball_pos, pygame.K_RIGHT):
ball_pos = (ball_pos[0] + 1, ball_pos[1])
screen.fill((255, 255, 255))
draw_maze(screen)
draw_ball(screen, ball_pos)
pygame.display.flip()
if is_end(ball_pos):
font = pygame.font.Font(None, 36)
text = font.render("You Win!", True, (0, 255, 0))
text_rect = text.get_rect(centerx=screen.get_width() // 2, centery=screen.get_height() // 2)
screen.blit(text, text_rect)
pygame.display.flip()
pygame.time.delay(2000)
generate_maze()
ball_pos = start_pos
# 启动主函数
if __name__ == "__main__":
main()
```
运行代码后,你可以使用方向键来控制小球移动,完成走迷宫的游戏。希望对你有帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)