早教吧 育儿知识 作业答案 考试题库 百科 知识分享

哪位大神能帮我写一个python的code啊求求啦写一个小游戏。每一个循环,画一个简单的8x8ASCII游戏格子。格子需要有边界,空间,玩家"P"的位置和magickey“K”的位置。然后得到使用者输入

题目详情
哪位大神能帮我写一个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)放在左上角。
例子:
Welcome 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 key"K"!)
【Write the function printBoard(playerX, playerY, keyX, keyY) that takes the XY coordinate positions of both the
player and the key, and prints out the entire board.

Then, in main() get the direction from the
user. Validate the input.

Write the function updatePosition(direction, playerX, playerY) that takes the direction the user entered, the
original 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 keeps
going 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 < 0 or pY < 0 or 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('Welcome 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)