用python语言制作推箱子小游戏
时间: 2023-10-09 15:11:14 浏览: 101
好的,以下是一个基于 Python 语言的推箱子小游戏的示例代码,希望能帮到您。
```python
# -*- coding: utf-8 -*-
# 定义地图
map = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 2, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 2, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
# 定义人的初始位置
man_x = 3
man_y = 3
# 定义箱子的数量和初始位置
box_num = 2
box_pos = [[3, 4], [4, 4]]
# 定义目标点的数量和位置
goal_num = 2
goal_pos = [[7, 3], [7, 4]]
# 游戏主循环
while True:
# 打印地图
for i in range(len(map)):
for j in range(len(map[i])):
# 打印人
if i == man_x and j == man_y:
print('M', end=' ')
# 打印箱子
elif [i, j] in box_pos:
print('B', end=' ')
# 打印目标点
elif [i, j] in goal_pos:
print('G', end=' ')
# 打印墙壁
elif map[i][j] == 1:
print('#', end=' ')
# 打印空地
else:
print(' ', end=' ')
print()
# 判断游戏是否结束
if len(set(box_pos) & set(goal_pos)) == goal_num:
print('恭喜您过关了!')
break
# 获取玩家输入
direction = input('请输入移动方向(w上s下a左d右):')
# 移动人
if direction == 'w':
if map[man_x - 1][man_y] != 1 and [man_x - 1, man_y] not in box_pos:
man_x -= 1
elif [man_x - 1, man_y] in box_pos and map[man_x - 2][man_y] != 1 and [man_x - 2, man_y] not in box_pos:
man_x -= 1
box_pos[box_pos.index([man_x - 1, man_y])] = [man_x - 2, man_y]
elif direction == 's':
if map[man_x + 1][man_y] != 1 and [man_x + 1, man_y] not in box_pos:
man_x += 1
elif [man_x + 1, man_y] in box_pos and map[man_x + 2][man_y] != 1 and [man_x + 2, man_y] not in box_pos:
man_x += 1
box_pos[box_pos.index([man_x + 1, man_y])] = [man_x + 2, man_y]
elif direction == 'a':
if map[man_x][man_y - 1] != 1 and [man_x, man_y - 1] not in box_pos:
man_y -= 1
elif [man_x, man_y - 1] in box_pos and map[man_x][man_y - 2] != 1 and [man_x, man_y - 2] not in box_pos:
man_y -= 1
box_pos[box_pos.index([man_x, man_y - 1])] = [man_x, man_y - 2]
elif direction == 'd':
if map[man_x][man_y + 1] != 1 and [man_x, man_y + 1] not in box_pos:
man_y += 1
elif [man_x, man_y + 1] in box_pos and map[man_x][man_y + 2] != 1 and [man_x, man_y + 2] not in box_pos:
man_y += 1
box_pos[box_pos.index([man_x, man_y + 1])] = [man_x, man_y + 2]
```
这个示例代码实现了一个简单的推箱子小游戏,玩家可以通过移动人的方式来移动箱子,将箱子推到目标点上即可过关。该示例代码仅供参考,您可以根据自己的需求进行修改和优化。
阅读全文
相关推荐


















