简体   繁体   English

在5x5板上进行游戏的python播放器运动

[英]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. 这是我正在创建的游戏的示例代码,在创建玩家时,我已经将所有已编码的内容随机放置在5x5板上。 i need an option for a user to move the player NESW, but i cannot figure it out. 我需要用户移动播放器NESW的选项,但我无法弄清楚。 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: 您应该更新代码以传递元组或传递row和col值,如以下代码片段所示:

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: 1)首先,在生成代码的董事会中输入:

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 board因为它不在函数中。 Return statements are mostly used in functions: Return语句主要用于函数中:

def square(x):
    return x*x

2) Your board will be organized with a list in a list like the following: 2)您的董事会将按照以下列表进行组织:

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. [row]指向板列表中的列表,而[col]指向该列表中的项目。 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 '@'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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