Python游戏玩家运动

时间:2016-04-09 15:06:37

标签: python

我开始使用Python编程语言制作一个简单的游戏。

我从左下角开始生成一个播放器(P)的网格。

玩家可以输入列和垂直移动但是从左上角开始?我希望玩家的动作从左下角开始。

board = []

pcol = 7
prow = 0

for x in range(8):
    board.append([" * "] * 8)


# player position 
def player():
    board[pcol][prow] = " P "


def printboard(board):
    for row in board:
        row = " ".join(row)
        print (row)

def playermovement():
    global pcol
    global prow


    # clear position
    board[pcol][prow] = " * "

    # new position
    pcol = int(input("Column: "))
    prow = int(input("Vertical: "))
    return board, pcol, prow


player()
printboard(board)

playermovement()


player()
printboard(board)

2 个答案:

答案 0 :(得分:0)

所以你希望它像那样?

board = []

pcol = 7
prow = 0

for x in range(8):
    board.append([" * "] * 8)


# player position 
def player():
    board[pcol][prow] = " P "


def printboard(board):
    for row in board:
        row = " ".join(row)
        print (row)

def playermovement():
    global pcol
    global prow


    # clear position
    board[pcol][prow] = " * "

    # new position
    pcol = int(input("Column: "))
    prow = int(input("Vertical: "))
    pcol=7-pcol
    return board, pcol, prow


player()
printboard(board)

playermovement()


player()
printboard(board)

如果这是你想要实现的目标,那就非常简单......

答案 1 :(得分:0)

您已在此代码中正确理解:

def printboard(board):
    for row in board:

董事会中的每个项目都是一行,而不是一列,并且每一行都包含属于不同列的单元格。

同样你需要解决这个问题:

def player():
    board[pcol][prow] = " P "

首先得到正确的行,然后是列,即:

def player():
    board[prow][pcol] = " P "