在 Python 中调试迷宫游戏

Debugging Maze game in Python

我最近一直在学习 python,目前正在开发一款让角色在迷宫中移动的小型终端游戏。我有一个错误,我的角色被卡住并且不允许进入开放空间。我检查了我所有的动作方向都是正确的,但我不确定角色卡住的原因。

应用程序

played = player.Player(3, 2)

while True:
    board.printBoard(played.rowPosition, played.columnPosition)
    selection = input("Make a move: ")

    if selection == "w":
        if board.checkMove(played.rowPosition - 1, played.columnPosition):
            played.moveUp()
    elif selection == "s":
        if board.checkMove(played.rowPosition + 1, played.columnPosition):
            played.moveDown()
    elif selection == "a":
        if board.checkMove(played.rowPosition, played.columnPosition - 1):
            played.moveLeft()
    elif selection == "d":
        if board.checkMove(played.rowPosition, played.columnPosition - 1):
            played.moveRight()
    else:
        print("Invalid key")

    if board.checkWin(played.rowPosition, played.columnPosition):
        print("You Win!")
        break;

玩家:

class Player:
    def __init__(self, intitalRow, initialColumn):
        self.rowPosition = intitalRow
        self.columnPosition = initialColumn

    def moveUp(self):
        self.rowPosition = self.rowPosition - 1

    def moveDown(self):
        self.rowPosition = self.rowPosition + 1

    def moveRight(self):
        self.columnPosition = self.columnPosition + 1

    def moveLeft(self):
        self.columnPosition = self.columnPosition - 1

游戏板

class GameBoard:
    def __init__(self):
        self.winningRow = 0
        self.winningColumn = 2
        self.board = [
            [" * ", " * ", "   ", " * ", " * ", " * "],
            [
                " * ",
                "   ",
                "   ",
                "   ",
                " * ",
                " * ",
            ],
            [
                " * ",
                "   ",
                " * ",
                " * ",
                "   ",
                " * ",
            ],
            [
                " * ",
                "   ",
                "   ",
                "   ",
                "   ",
                " * ",
            ],
            [" * ", " * ", " * ", " * ", " * ", " * "],
        ]

    def printBoard(self, playerRow, playerColumn):
        for i in range(len(self.board)):
            for j in range(len(self.board[i])):
                if i == playerRow and j == playerColumn:
                    print("P", end="")
                else:
                    print(self.board[i][j], end="")
            print("")

    def checkMove(self, testRow, testColumn):
        if self.board[testRow][testColumn].find("*") != -1:
            print("Can't move there!")
            return False
        return True

    def checkWin(self, playerRow, playerColumn):
        if playerRow == 0 and playerColumn == 2:
            return True
        else: 
            return False

When the selection is "d" the same cell is being checked as when the selection is "a".我认为您的意思是其中之一使用 played.columnPosition + 1.