python推箱子游戏代码
时间: 2023-07-09 07:53:31 浏览: 45
以下是一个简单的推箱子游戏的 Python 代码示例:
```python
# 定义地图
map = [
"########",
"# #",
"# #",
"# #",
"#@ #",
"# #",
"# #",
"########"
]
# 将地图字符串转换为二维列表
map = [[c for c in row] for row in map]
# 定义人物和箱子的坐标
player_pos = (4, 1)
box_pos = (4, 4)
# 游戏循环
while True:
# 打印地图
for row in map:
print("".join(row))
# 判断是否过关
if map[1][1] == "$" and map[1][2] == "$" and map[2][1] == "$" and map[2][2] == "$":
print("You win!")
break
# 获取玩家输入
move = input("Move (w/a/s/d): ")
# 移动人物
if move == "w":
if map[player_pos[0]-1][player_pos[1]] != "#":
if box_pos == (player_pos[0]-1, player_pos[1]):
if map[box_pos[0]-1][box_pos[1]] != "#":
map[box_pos[0]][box_pos[1]] = " "
box_pos = (box_pos[0]-1, box_pos[1])
map[box_pos[0]][box_pos[1]] = "$"
else:
continue
map[player_pos[0]][player_pos[1]] = " "
player_pos = (player_pos[0]-1, player_pos[1])
map[player_pos[0]][player_pos[1]] = "@"
elif move == "a":
if map[player_pos[0]][player_pos[1]-1] != "#":
if box_pos == (player_pos[0], player_pos[1]-1):
if map[box_pos[0]][box_pos[1]-1] != "#":
map[box_pos[0]][box_pos[1]] = " "
box_pos = (box_pos[0], box_pos[1]-1)
map[box_pos[0]][box_pos[1]] = "$"
else:
continue
map[player_pos[0]][player_pos[1]] = " "
player_pos = (player_pos[0], player_pos[1]-1)
map[player_pos[0]][player_pos[1]] = "@"
elif move == "s":
if map[player_pos[0]+1][player_pos[1]] != "#":
if box_pos == (player_pos[0]+1, player_pos[1]):
if map[box_pos[0]+1][box_pos[1]] != "#":
map[box_pos[0]][box_pos[1]] = " "
box_pos = (box_pos[0]+1, box_pos[1])
map[box_pos[0]][box_pos[1]] = "$"
else:
continue
map[player_pos[0]][player_pos[1]] = " "
player_pos = (player_pos[0]+1, player_pos[1])
map[player_pos[0]][player_pos[1]] = "@"
elif move == "d":
if map[player_pos[0]][player_pos[1]+1] != "#":
if box_pos == (player_pos[0], player_pos[1]+1):
if map[box_pos[0]][box_pos[1]+1] != "#":
map[box_pos[0]][box_pos[1]] = " "
box_pos = (box_pos[0], box_pos[1]+1)
map[box_pos[0]][box_pos[1]] = "$"
else:
continue
map[player_pos[0]][player_pos[1]] = " "
player_pos = (player_pos[0], player_pos[1]+1)
map[player_pos[0]][player_pos[1]] = "@"
```
注:代码中的 `@` 表示人物,`$` 表示箱子。
阅读全文