为什么 pygame 不显示我的图像或棋盘?

Why won't pygame display my images or chessboard?

我正在为我的 IDE 使用 Pycharm Community 2020。我正在使用 Python 3.7.4。我有 Pygame 1.9.6。 所以我一直在关注一个关于如何编写工作棋盘代码的 YouTube 视频,但它并没有向我展示我的棋盘及其上的棋子或任何东西。

我必须分开 python 个文件来为 Chess engine 做这个,它负责存储所有的有关国际象棋游戏当前状态的信息。还将负责确定当前状态下的有效移动。它还将保留移动日志。另一个是 Chess Main 这是主驱动程序文件。它负责处理用户输入并显示当前游戏状态对象。 国际象棋主要代码:

WIDTH = HEIGHT = 512  # 400 is another option
DIMENSION = 8  # dimensions of a chess board are 8x8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15  # for animations later on
IMAGES = {}

def loadImages():
    pieces = ['wp', 'wR', 'wN', 'wB', 'wK', 'wQ', 'bp', 'bR', 'bN', 'bB', 'bK', 'bQ']
    for piece in pieces:
        IMAGES[piece] = p.transform.scale(p.image.load("images/" + piece + ".png"), (SQ_SIZE, SQ_SIZE))
        
# Note: we can access an image saving "IMAGES"['wp']
def main():
    p.init()
    screen = p.display.set_mode((WIDTH, HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = ChessEngine.GameState()
    loadImages()  # only do this once, before the while loop
    running = True
    sqSelected = ()  # no square is selected, keep track of the last click of the user (tuple: (row, col))
    player_Clicks = []  # keep track of player clicks (two tuples [6,4), (4,4)]
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            elif e.type == p.MOUSEBUTTONDOWN:
                location = p.mouse.get_pos()  # (x, y) location of mouse
                col = location[0]//SQ_SIZE
                row = location[1]//SQ_SIZE
                if sqSelected == (row, col):  # the user clicked the same square twice
                    sqSelected = ()  # deselect
                    player_Clicks = []  # clear player clicks
                else:
                    sqSelected = (row, col)
                    player_Clicks.append(sqSelected)  # append for both 1st and 2nd clicks
                if len(player_Clicks) == 2:  # after 2nd click
                    move = ChessEngine.Move(player_Clicks[0], player_Clicks[1], gs.board)
                    print(move.getChessNotation())
                    gs.makeMove(move)
                    sqSelected = ()  # reset user clicks
                    player_Clicks = []

        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()


def drawGameState(screen, gs):
    drawBoard(screen)  # draw squares on the board
    # add in piece highlighting or move suggestions (later)
    drawPieces(screen, gs.board)  # draw pieces on top of those squares

def drawBoard(screen):
    colors = [p.Color("white"), p.Color("gray")]
    for r in range(DIMENSION):
        for c in range(DIMENSION):
            color = colors[((r+c) % 2)]
            p.draw.rect(screen, color, p.Rect(c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE))

    def drawPieces(screen, board):
        for r in range(DIMENSION):
            for c in range(DIMENSION):
                piece = board[r][c]
                if piece != "--":  # not empty square
                    screen.blit(IMAGES)[piece], p.Rect(c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE)

if __name__ == " __main__":
    main()

国际象棋引擎代码:

class GameState:
    def __init__(self):
        # board is an 8x8 2d list, each element of lest has 2 character.
        # The first character represents the color of the piece, "b" or "w"
        # The second character represents the type of the piece, "K", "Q", "R", "B", "N", or "p"
        # "--" - represents an empty space with no piece.
        self.board = [
            ["bR", "bN", "bB", "bQ", "bK", "b8", "bN", "bR"],
            ["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
            ["--", "--", "--", "--", "--", "--", "--", "--"],
            ["--", "--", "--", "--", "--", "--", "--", "--"],
            ["--", "--", "--", "--", "--", "--", "--", "--"],
            ["--", "--", "--", "--", "--", "--", "--", "--"],
            ["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
            ["wR", "wN", "wB", "wQ", "wK", "w8", "wN", "wR"]]
        self.whiteToMove = True
        self.moveLog = []

    def makeMove(self, move):
        self.board[move.startRow][move.startCol] = "--"
        self.board[move.endRow][move.endCol] = move.pieceMoved
        self.moveLog.append(move)  # log the move so we can undo it later
        self.whiteToMove = not self.whiteToMove  # swap players


class Move():
    # maps key to values
    # key : value
    ranksToRows = {"1": 7, "2": 6, "3": 5, "4": 4,
                "5": 3, "6": 2, "7": 1, "8": 0}
    rowsToRanks = {v: k for k, v in ranksToRows.items()}
    filesToCols = {"a": 0, "b": 1, "c": 2, "d": 3,
                "e": 4, "f": 5, "g": 6, "h": 7}
    colsToFiles = {v: k for k, v in filesToCols.items()}

    def __init__(self, startSq, endSq, board):
        self.startRow = startSq[0]
        self.startCol = startSq[1]
        self.endRow = endSq[0]
        self.endCol = endSq[1]
        self.pieceMoved = board[self.startRow][self.startCol]
        self.pieceCaptured = board[self.endRow][self.endCol]

    def getChessNotation(self):
        # you can add to make this like real chess notation
        return self.getRankFile(self.startRow, self.startCol) * self.getRankFile(self.endRow, self.endCol)

    def getRankFile(self, r, c):
        return self.colsToFiles[c] * self.rowsToRanks[r]

所以我不知道为什么 Pycharm 没有显示板,如果你想要我看到的前 2 个视频的 YouTube 链接:
第 1 部分:https://www.youtube.com/watch?v=EnYui0e73Rs

第 2 部分:https://www.youtube.com/watch?v=o24J3WcBGLg

编辑:我修复了第一个答案中的问题并使其正常工作。另外必须修复 if name == "main": (main)。只是(主要)。现在它不想正确移动棋子或注册鼠标点击。我现在要抛出错误:

Traceback (most recent call last):
  line 105, in <module>
   main()
  line 58, in main
    print(move.getChessNotation())
   line 52, in getChessNotation
    return self.getRankFile(self.startRow, self.startCol) * 
 self.getRankFile(self.endRow, self.endCol)
  line 55, in getRankFile
    return self.colsToFiles[c] * self.rowsToRanks[r]
TypeError: can't multiply sequence by non-int of type 'str'
                             

首先,blits 图像的语句在语义上是错误的:

creen.blit(IMAGES)[piece], p.Rect(c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE)

screen.blit(IMAGES[piece], p.Rect(c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE))

此外,数字“b8”和“w8”不存在。您可能指的是“bB”和“wB”。修正数字名称是 self.board 在 class GameState.