早教吧作业答案频道 -->其他-->
哪位大神能帮我写一个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了
写一个小游戏。 每一个循环,画一个简单的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)
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)
看了 哪位大神能帮我写一个pyth...的网友还看了以下:
斩秦英的本戏是什么斩秦英是什么戏的一折 2020-04-06 …
电脑操作系统Windows下有一个有趣的游戏“扫雷”,下图是扫雷游戏的一部分:说明:雷区方格中数字 2020-06-17 …
下列有关文学常识及课文内容的表述,有错误的一项是(3分)()A.小说《社戏》按照盼看社戏——去看社 2020-06-23 …
关于社戏的一道语文题目课文题目为《社戏》,由此可见小说的中心情节是“看社戏”.思考围绕这一中心事件 2020-06-23 …
赌乃游戏的一种,小赌怡人,大赌伤财,这句话蕴含着怎样的哲学思想?唔该识既帮帮忙 2020-06-26 …
要社戏的一段还有这段的赏析(一定要有那一段原文) 2020-07-10 …
Windows2000下有一个有趣的游戏“扫雷”,下图是扫雷游戏的一部分:(说明:图中数字2表示在 2020-07-18 …
赞美藏戏的一句话 2020-07-25 …
A,B,C三个方格中有地雷的概率分别是多大?“扫雷"是一个有趣的游戏,图7是此游戏的一部分.数字”2 2020-11-21 …
电脑上有一个有趣的“扫雷”游戏,图是扫雷游戏的一部分,说明:图中数字2表示在以该数字为中心的周边8个 2020-11-21 …