用PYTHON实现一个简单的小游戏, 推箱子,要求有五个关卡
时间: 2024-03-22 17:42:45 浏览: 44
好的,我可以为您提供一个简单的推箱子小游戏的代码,并实现五个关卡。请注意,这只是一个示例代码,您可以根据自己的需求进行修改和优化。
```python
# 定义游戏地图
MAPS = [
[
"##########",
"# #",
"# #",
"# #",
"# #",
"# @ #",
"# #",
"# #",
"# #",
"##########"
],
[
"##########",
"# #",
"# #",
"# #",
"# #",
"# @ #",
"# #",
"# #",
"# #",
"##########"
],
[
"##########",
"# #",
"# #",
"# #",
"# #",
"# @ $#",
"# #",
"# #",
"# #",
"##########"
],
[
"##########",
"# #",
"# #",
"# #",
"# #",
"# @ $#",
"# #",
"# #",
"# #",
"##########"
],
[
"##########",
"# #",
"# #",
"# #",
"# #",
"# @ $#",
"# #",
"# #",
"# #",
"##########"
]
]
def play_game(map):
# 查找人和箱子的位置
for i in range(len(map)):
for j in range(len(map[i])):
if map[i][j] == "@":
player_pos = (i, j)
elif map[i][j] == "$":
box_pos = (i, j)
while True:
# 打印地图
for row in map:
print(row)
# 判断是否通关
if map[box_pos[0]][box_pos[1]] == "#":
print("恭喜你,进入下一关!")
return True
# 接收用户输入
direction = input("请输入移动方向(w: 上, s: 下, a: 左, d: 右): ")
# 根据用户输入移动人和箱子
if direction == "w":
new_player_pos = (player_pos[0]-1, player_pos[1])
if map[new_player_pos[0]][new_player_pos[1]] == "#":
continue
elif new_player_pos == box_pos:
new_box_pos = (box_pos[0]-1, box_pos[1])
if map[new_box_pos[0]][new_box_pos[1]] == "#":
continue
else:
map[box_pos[0]][box_pos[1]] = " "
map[new_box_pos[0]][new_box_pos[1]] = "$"
box_pos = new_box_pos
map[player_pos[0]][player_pos[1]] = " "
map[new_player_pos[0]][new_player_pos[1]] = "@"
player_pos = new_player_pos
elif direction == "s":
new_player_pos = (player_pos[0]+1, player_pos[1])
if map[new_player_pos[0]][new_player_pos[1]] == "#":
continue
elif new_player_pos == box_pos:
new_box_pos = (box_pos[0]+1, box_pos[1])
if map[new_box_pos[0]][new_box_pos[1]] == "#":
continue
else:
map[box_pos[0]][box_pos[1]] = " "
map[new_box_pos[0]][new_box_pos[1]] = "$"
box_pos = new_box_pos
map[player_pos[0]][player_pos[1]] = " "
map[new_player_pos[0]][new_player_pos[1]] = "@"
player_pos = new_player_pos
elif direction == "a":
new_player_pos = (player_pos[0], player_pos[1]-1)
if map[new_player_pos[0]][new_player_pos[1]] == "#":
continue
elif new_player_pos == box_pos:
new_box_pos = (box_pos[0], box_pos[1]-1)
if map[new_box_pos[0]][new_box_pos[1]] == "#":
continue
else:
map[box_pos[0]][box_pos[1]] = " "
map[new_box_pos[0]][new_box_pos[1]] = "$"
box_pos = new_box_pos
map[player_pos[0]][player_pos[1]] = " "
map[new_player_pos[0]][new_player_pos[1]] = "@"
player_pos = new_player_pos
elif direction == "d":
new_player_pos = (player_pos[0], player_pos[1]+1)
if map[new_player_pos[0]][new_player_pos[1]] == "#":
continue
elif new_player_pos == box_pos:
new_box_pos = (box_pos[0], box_pos[1]+1)
if map[new_box_pos[0]][new_box_pos[1]] == "#":
continue
else:
map[box_pos[0]][box_pos[1]] = " "
map[new_box_pos[0]][new_box_pos[1]] = "$"
box_pos = new_box_pos
map[player_pos[0]][player_pos[1]] = " "
map[new_player_pos[0]][new_player_pos[1]] = "@"
player_pos = new_player_pos
# 开始游戏
for i in range(len(MAPS)):
print("第{}关开始!".format(i+1))
if play_game(MAPS[i]):
continue
else:
print("游戏结束!")
break
```
这个小游戏实现了五个关卡,每个关卡都有一个固定的地图,并且要求玩家通过移动人和箱子来将箱子推到指定位置,从而通关。玩家可以使用w、s、a、d四个方向键来移动人和箱子,如果箱子被推到了固定位置,则该关卡通关,游戏进入下一关。
阅读全文