推箱子小游戏
用python代码写的一个推箱子小游戏,有喜欢的可以玩一下。
代码如下:
# 用二维数组表示地图
# X 代表箱子,o 代表小人
map = [
“**“,
“ ** “,
“ X** “,
“ o “,
“** *”,
“ ** “,
“ “,
“* **“,
“* “,
“**“
]
# 创建变量step,用于存储已走步数
# step = 0
# 记录小人的初始坐标
global currentPersonRow,currentPersonCol
currentPersonRow = 3
currentPersonCol = 2
# 显示地图的方法
def showMap():
for m in map:
print(m)
# 接收用户输入的方向的函数
def enterDirection():
dir = input(‘请输入小人移动的方向 w:上 s:下 a:左 d:右 \n’)
return dir
# 用于移动小人和箱子
# 这个方法需要改变
def move(dir):
# 创建变量记录小人的下一个坐标
nextPersonRow = 0
nextPersonCol = 0
# 获取下一个位置坐标
# 调用getNextPoisiton()函数计算下一个坐标
global currentPersonRow,currentPersonCol
# print(‘这’)
# print(currentPersonRow,currentPersonCol,nextPersonRow,nextPersonCol)
position = getNextPosition(dir,currentPersonRow,currentPersonCol,nextPersonRow,nextPersonCol)
print(‘第一次:’,position)
# print(position) -> [nextPersonRow,nextPersonCol,currentPersonRow,currentPersonCol]
currentPersonRow = position[2]
currentPersonCol = position[3]
nextPersonRow = position[0]
nextPersonCol = position[1]
# 判断下一个位置是什么 *是墙壁,X是箱子,空格代表通道
if map[nextPersonRow][nextPersonCol] == ‘ ‘:
# 代表下一个位置是通道,可以移动
# map[currentPersonRow][currentPersonCol] = ‘ ‘
# map[nextPersonRow][nextPersonCol] = ‘o’
# 调用改变地图方法
chmap(currentPersonRow,currentPersonCol,map,’ ‘)
chmap(nextPersonRow,nextPersonCol,map,’o’)
# 改变小人当前位置
currentPersonRow = nextPersonRow
currentPersonCol = nextPersonCol
# step+=1 #步数+1
elif map[nextPersonRow][nextPersonCol] == ‘X’:
# 此时下一个位置是箱子,还需要再次判断下一个位置
nextBoxRow = 0
nextBoxCol = 0
# 调用getNextPosition()计算箱子的下一个位置
position = getNextPosition(dir,nextPersonRow,nextPersonCol,nextBoxRow,nextBoxCol)
print(‘第二次:’,position)
nextBoxRow = position[0]
nextBoxCol = position[1]
nextPersonRow = position[2]
nextPersonCol = position[3]
if map[nextBoxRow][nextBoxCol] == ‘ ‘:
# 代表下一个位置是路,可以移动
# map[nextBoxRow][nextBoxCol] = ‘X’
# map[nextPersonRow][nextPersonCol] = ‘o’
# map[currentPersonRow][currentPersonCol] = ‘ ‘
chmap(nextBoxRow,nextBoxCol,map,’X’)
chmap(nextPersonRow,nextPersonCol,map,’o’)
chmap(currentPersonRow,currentPersonCol,map,’ ‘)
# step+=1 #步数+1
# 记录当前小人的位置
currentPersonRow = nextPersonRow
currentPersonCol = nextPersonCol
# 遇到墙壁的情况不做处理
# 获取下一个位置的方法
# 需要五个参数
# 1.移动的方向
# 2.当前行
# 3.当前列
# 4.下一个行位置
# 5.下一个列位置
def getNextPosition(dir,currentPersonRow,currentPersonCol,nextPersonRow,nextPersonCol):
# 创建一个数组,存储行列位置,并返回
position = []
if dir == ‘a’ or dir == ‘A’:
# 向左移一个单位
nextPersonRow = currentPersonRow
nextPersonCol = currentPersonCol - 1
elif dir == ‘d’ or dir == ‘D’:
# 向右移一个单位
nextPersonRow = currentPersonRow
nextPersonCol = currentPersonCol + 1
elif dir == ‘w’ or dir == ‘W’:
# 向上移一个单位
nextPersonRow = currentPersonRow - 1
nextPersonCol = currentPersonCol
elif dir == ‘s’ or dir == ‘S’:
# 向下移一个单位
nextPersonRow = currentPersonRow + 1
nextPersonCol = currentPersonCol
# else:
# print(‘请输入正确的方向’)
position.append(nextPersonRow)
position.append(nextPersonCol)
position.append(currentPersonRow)
position.append(currentPersonCol)
return position
# 改变地图的参数,传入x,y为更改的坐标,map为数组,也就是地图,target是目标图形
def chmap(x,y,map,target):
s = map[x]
l = list(s)
l[y] = target
new_s = ‘’.join(l)
map[x] = new_s
while True:
showMap()
# 每次都判断是否过关
if map[8][9] == ‘X’:
# print(“恭喜您过关了,您一共走了%d步” %step)
print(‘You are win!’)
break
# 调用enterDirection()获取用户输入的方向并复制给dir
dir = enterDirection()
print(“你输入的方向是:%c \n” %dir)
if dir == ‘q’ or dir == ‘Q’:
print(‘游戏退出’)
break
move(dir)