简体   繁体   中英

python player movement for game in 5x5 board

def playerMove (board,player):
userInput = input("Enter a direction NSWE: ").upper()
if userInput == "N":
    if (player == 0):
        board[player] = '.'
        player += 1
        board[player] = '@'
elif userInput == "S":

    if (player(board)-1):
        board[player] = '.'
        player += 1
        board[player] = '@'
elif userInput == "E":

    if (player < len(board)-1):
        board[player] = '.'
        player += 1
        board[player] = '@'
elif userInput == "W":

    if (player['x'] > 0):
        board[player] = '.'
        player -= 1
        board[player] = '@'

this is a sample coding for a game i am creating, i have everything already coded for when a player is created it is randomly placed on a 5x5 board. i need an option for a user to move the player NESW, but i cannot figure it out. this is not all of my coding, just the part that i need help with.

here is the board coding

'def createBoard():
tempList = []
for i in range (5):
    tempList.append(".")

board = []
for i in range (5):
    board.append(["."]*5)

return board


def showBoard(board):    
    print ("---------")
    print ("|".join(board[0]))
    print ("---------")
    print("|".join(board[1]))
    print ("---------")
    print("|".join(board[2]))
    print ("---------")
    print("|".join(board[3]))
    print ("---------")
    print("|".join(board[4]))
    print ("---------")

def placePlayer(board,player):
    len(board[0])
    row = random.randint(0, len(board) -1)
    col = random.randint(0, len(board[0])-1)
    board[row][col] = '@' 
    return board, row, col

Your player doesn't appear to be referenced by a single integer value, but instead a row and a col

You should update your code to either pass a tuple around or pass in the row and col values as in the following snippet:

def playerMove (board,playerRow, playerCol):
    if userInput == "N":
        if (playerRow > 0):
            board[playerRow][playerCol] = '.'
            playerRow -= 1
            board[playerRow][playerCol] = '@'

Note: The above code assumes that your board is split into a multidimsional array. As it is currently, you would need to manipulate the strings in your array as strings do not allow you to directly modify the contents in place.

1) First of all, in your board generating code you put:

board = []
for i in range (5):
    board.append(["."]*5)

return board

In that code, it is not necessary to use return board as it is not in a function. Return statements are mostly used in functions:

def square(x):
    return x*x

2) Your board will be organized with a list in a list like the following:

board = [
    ['.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.']
    ]

To look up a point on the board you would use the following syntax:

board[row][col]

The [row] points to the list in the board list, while the [col] points to an item in that list. So to find a point on the grid you need both the row and the column.

To move the player up one in the board you would use the following:

board[row][col] = '.' #reset last position to '.'
board[row][col-1] = '@' #set the coordinate above to '@'

And to move the player one to the right you would use the following:

board[row][col] = '.' #reset last position '.'
board[row+1][col] = '@' #set the coordinate to the right to '@'

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