简体   繁体   中英

Python game player movement

I am starting to make a simple game using the Python programming language.

I have generated a grid with the player (P) starting in the bottom left corner.

The player can enter column and vertical movement but starts from the top left corner? I want the player movement to start from the bottom left corner.

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)

So you want it to be like that?

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)

If that is what you wanted to achieve it was very simple...

You've correctly understood in this code:

def printboard(board):
    for row in board:

that each item in the board is a row, not a column, and that each row contains cells which belong to a different column.

Similarly you need to fix this:

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

to get the correct row first, and then the column, ie:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM