哪位大神能帮我写一个python的code啊 求求啦写一个小游戏。 每一个循环,画一个简单的8x8 ASCII 游戏格子。 格子需要有边界,空间,玩家P的位置和magic key “K”的位置。然后得到使用者输入的他们想要移动的方向(“北North”,南South,东East,“西West”)。更新玩家的横坐标“X”,纵坐标“Y” 并且开始画下一个循环的格子。不要让玩家在边界上移动。 直到玩家到达magic key K的时候,print出玩家赢了 并且退出。把magic key K的位置设成(3,6),把玩家的初始位置设成(2,4)记住,横纵坐标我们把(0,0)放在左上角。例子:Wele to the Loops HW. Get to the key!!! -------------------| . . . . . . . . || . . . . . . . . || . . . . . . . . || . . . . . . . . || . . P . . . . . || . . . . . . . . | | . . . K . . . . || . . . . . . . . | -------------------North, South, East or West? south-------------------| . . . . . . . . || . . . . . . . . || . . . . . . . . || . . . . . . . . || . . . . . . . . || . . P . . . . . || . . . K . . . . || . . . . . . . . |-------------------North, South, East or West? east-------------------| . . . . . . . . || . . . . . . . . || . . . . . . . . || . . . . . . . . || . . . . . . . . || . . . P . . . . || . . . K . . . . || . . . . . . . . |-------------------North, South, East or West? south You found the magic key!(你找到了magic keyK!)【Write the function printBoard(playerX, playerY, keyX, keyY) that takes the XY coordinate positions of both theplayer and the key, and prints out the entire board. Then, in main() get the direction from theuser. Validate the input. Write the function updatePosition(direction, playerX, playerY) that takes the direction the user entered, theoriginal player's X and Y, and returns the updated player's X and Y.Write the function offboard(pX, pY) that takes the player's new XY position and return True if they are now on the border and False if they are not. Add a call to offboard() in updatePosition(). Write the function reachedKey(playerX, playerY, keyX, keyY) which returns True if the player reached the key and False if not. Finally, edit main() so that the program keepsgoing until the player has reached the key.】实在不知道怎么翻译了拜托了 大神 尽快啊 马上要due了
网友回答
【答案】 #楼上竟然比我还快
def printBoard(playerX, playerY, keyX, keyY):
board = [ ['.']* 8 for _ in range(8) ]
board[playerY][playerX] = 'P'
board[keyY][keyX] = 'K'
print('-'*10)
for i in range(8):
print('|' + ''.join(board[i]) + '|')
print('-'*10)
def updatePosition(direction, playerX, playerY):
d = {'north': (-1, 0),
'south': (1, 0),
'west': (0, -1),
'east': (0, 1) }
direction = direction.lower()
playerX += d[direction][1]
playerY += d[direction][0]
return playerX, playerY
def offboard(pX, pY):
return pX 7 or pY > 7
def reachedKey(playerX, playerY, keyX, keyY):
return playerX == keyX and playerY == keyY
if __name__ == '__main__':
x, y = 2,4
keyX , keyY = 3, 6
print('Wele to the Loops HW. Get to the key!!!')
printBoard(x, y, keyX, keyY)
while True:
print('North, South, East or West?')
dire = raw_input()
if dire not in ['north', 'south', 'west', 'east']:
continue
x, y = updatePosition(dire, x, y)
if offboard(x, y):
print('offboard')
continue
if reachedKey(x, y, keyX, keyY):
print('You found the magic key!')
break
printBoard(x, y, keyX, keyY)