用Python编一个有趣的迷宫游戏。
以下是用Python语言实现的简单化的迷宫游戏。这个游戏允许玩家通过控制方向键移动角色在迷宫中来回移动。最终目标是找到出口。
# 迷宫布局,0表示通道,1表示墙
maze = [
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 0, 1, 0, 1],
[1, 0, 1, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1]
]
# 玩家的起始位置
player_pos = [1, 0]
# 迷宫的出口位置
exit_pos = [4, 4]
def print_maze():
for i, row in enumerate(maze):
for j, cell in enumerate(row):
if [i, j] == player_pos:
print('P', end=' ') # P 表示玩家
elif [i, j] == exit_pos:
print('E', end=' ') # E 表示出口
elif cell == 1:
print('#', end=' ') # # 表示墙
else:
print(' ', end=' ')
print()
def is_exit_reached():
return player_pos == exit_pos
def move_player(direction):
x, y = player_pos
if direction == 'w' and maze[x - 1][y] != 1:
player_pos[0] -= 1
elif direction == 's' and maze[x + 1][y] != 1:
player_pos[0] += 1
elif direction == 'a' and maze[x][y - 1] != 1:
player_pos[1] -= 1
elif direction == 'd' and maze[x][y + 1] != 1:
player_pos[1] += 1
def main():
print("欢迎来到迷宫游戏!")
print("使用 W A S D 来控制上下左右移动。")
print("找到出口 E 并逃离迷宫!")
while not is_exit_reached():
print_maze()
move = input("你的移动(W/A/S/D):").lower()
move_player(move)
if is_exit_reached():
print("恭喜!你找到了出口并成功逃离了迷宫!")
if __name__ == "__main__":
main()
在这个游戏中由0和1组成,其中一代表0代表可通行的通道。玩家使用wasd键来控制上下左右移动如果玩家移动到了出口的位置游戏结束并显示恭喜信息。
请注意本游戏是非常基础的版本。所以有些功能没有完善。如有需要请自行完善。