【发布时间】:2021-05-11 10:05:19
【问题描述】:
我在文件 a 中有以下代码:
while(won == False):
game.userY = setGameRow(row, counter)
game.userX = setGameCol(col, counter)
counter+=1
printBoard(bd, game.userX , game.userY)
move(row, col, bd, mines, game.userX, game.userY)
won = retWon()
在文件 b 中:
def move(row, col, bd, mines, userX, userY):
i = inp()
if(i == "w"):
movX = 0
movY = -1
elif(i == "a"):
movX = -1
movY = 0
elif(i == "s"):
movX = 0
movY = 1
elif(i == "d"):
movX = 1
movY = 0
if((userY == 0 and inp == "w") or (userY == col-1 and inp == "s") or (userX == 0 and inp == "a") or (userX == row-1 and inp == "d")):
print("Invalid movement")
move(row, col, bd, mines)
userX += movX
userY += movY
if(getNumbers(userX, userY, bd) == 9):
lost(mines, row, col)
if(userX==0 and userY == 0):
win(mines, row, col)
电路板如下所示:
Where do you want to go? w
+----------+
|··········|
|··········|
|··········|
|··········|
|··········|
|··········|
|··········|
|··········|
|········00|
|········0*|
+----------+
玩家是·
执行printBoard() 时,userX 和 userY 再次更改为默认值。我知道它们确实会发生变化,但它们最终总是会变回默认值,因此当我移动播放器时棋盘不会更新。
【问题讨论】:
-
由于我们看不到您的完整代码,因此很难说您是如何在函数之间传递属性的。我从你的代码 sn-p 的猜测是 userX 和 userY 只在你的 move 函数中本地更新,而不是在你的脚本中全局更新