推箱子python代码
时间: 2023-10-12 09:16:42 浏览: 160
推箱子代码
以下是一个简单的推箱子游戏的 Python 代码:
```python
# 定义地图
map = [
"#####",
"# #",
"# #",
"#O #",
"#####"
]
# 玩家初始位置
player_x = 1
player_y = 1
# 箱子初始位置
box_x = 3
box_y = 3
# 游戏循环
while True:
# 打印地图
for row in map:
print(row)
# 判断是否胜利
if map[3][1] == "@" and map[3][2] == "@" and map[3][3] == "#":
print("You win!")
break
# 接收玩家输入
move = input("Move: ")
# 移动玩家
if move == "w":
if map[player_y - 1][player_x] != "#":
player_y -= 1
elif move == "s":
if map[player_y + 1][player_x] != "#":
player_y += 1
elif move == "a":
if map[player_y][player_x - 1] != "#":
player_x -= 1
elif move == "d":
if map[player_y][player_x + 1] != "#":
player_x += 1
# 移动箱子
if player_x == box_x and player_y == box_y:
if move == "w":
if map[box_y - 1][box_x] != "#":
box_y -= 1
elif move == "s":
if map[box_y + 1][box_x] != "#":
box_y += 1
elif move == "a":
if map[box_y][box_x - 1] != "#":
box_x -= 1
elif move == "d":
if map[box_y][box_x + 1] != "#":
box_x += 1
# 更新地图
map[player_y] = map[player_y][:player_x] + "@" + map[player_y][player_x+1:]
map[box_y] = map[box_y][:box_x] + "O" + map[box_y][box_x+1:]
```
这段代码实现了一个简单的推箱子游戏,通过用户输入来控制玩家移动和箱子移动,最终目标是将箱子推到指定位置。
阅读全文