所以,我想在python中创建一个非常简单的自上而下的游戏。我有一个非常简单的网格。
import sys
ROWS=5
COLUMNS=5
def display(grid):
for r in range (0,ROWS,1):
for c in range (0,COLUMNS,1):
sys.stdout.write(grid[r][c])
print()
def initialize():
grid=[]
for r in range (0,ROWS,1):
grid.append([])
grid[0]=['#','#','O','#','#']
grid[1]=['#','#',' ','#','#']
grid[2]=['#','#',' ','#','#']
grid[3]=['#','#',' ','#','#']
grid[4]=['#','#',' ','#','#']
return(grid)
def main():
grid=initialize()
display(grid)
main()
O是玩家。现在,我想要的是创建一个简单的函数,首先要求用户指示方向(2,表示向下,8表示向上,4表示左,6表示右),然后在网格上移动O. 我怎么能这样做?
答案 0 :(得分:0)
这是让你入门的东西:
# all your top-level setup code here
def initialize():
grid=[]
for r in range (0,ROWS,1):
grid.append([])
grid[0]=['#','#',' ','#','#']
grid[1]=['#','#',' ','#','#']
grid[2]=['#','#',' ','#','#']
grid[3]=['#','#',' ','#','#']
grid[4]=['#','#',' ','#','#']
row, col = 0, 2
grid[row][col] = '0'
return grid, row, col
def move(grid, oldrow, oldcol, newrol, newcol):
grid[oldrow][oldcol] = ' '
grid[newrol][newcol] = 'O'
def main():
grid, row, col = initialize()
while True:
display(grid)
dir = input("Which way? ")
if dir == '2':
# move up, make sure to handle errors
elif dir == '4':
# move left
# ditto for 6 and 8
else:
print("That's not a direction. Try 2, 4, 6, or 8.")
只需填写评论部分即可。
我实施move
的方式,用户将在他身后留下一些空白。如果你想记住他在那里(#
或)之前的原始价值,那么你还需要一个变量。