我的 Sprites 在 Pygame 中显示不正确

My Sprites are not showing properly in Pygame

我的精灵在我的程序中没有正确显示。地图显示正常,但当我按右上角的大 X 退出程序时,精灵只显示一瞬间(但当我按 ESC 退出时不显示)

显示精灵代码:

# Protagonist Sprites
protagR = [pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png")]
protagL = [pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png")]
protagU = [pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png")]
protagD = [pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png")]
# Bullet Sprites
bulletsP = [pygame.image.load("ASG2_Graphics/Bullet_1.png"), pygame.image.load("ASG2_Graphics/Bullet_2.png"), pygame.image.load("ASG2_Graphics/Bullet_3.png"), pygame.image.load("ASG2_Graphics/Bullet_4.png")]
bulletsE = [pygame.image.load("ASG2_Graphics/E_Bullet_1.png"), pygame.image.load("ASG2_Graphics/E_Bullet_2.png"), pygame.image.load("ASG2_Graphics/E_Bullet_3.png")]
# Enemy Sprites
enemyR = [pygame.image.load("ASG2_Graphics/Enemy_R1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_5.png")]
enemyL = [pygame.image.load("ASG2_Graphics/Enemy_L1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_5.png")]
enemyU = [pygame.image.load("ASG2_Graphics/Enemy_U1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_5.png")]
enemyD = [pygame.image.load("ASG2_Graphics/Enemy_D1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_5.png")]

Class代码:

class player():
        def __init__(self, x, y, width, height, walkCount):
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.vel = 5
            self.up = False
            self.down = False
            self.left = False
            self.right = False
            self.up = False
            self.down = False
            self.walkCount = walkCount

        def redrawGameWindow(self, window):
            dest = (self.x, self.y)
            if self.walkCount + 1 >= 30:
                self.walkCount = 0
            if self.left:
                window.blit(protagL[self.walkCount//3], dest)
                self.walkCount += 1
            elif self.right:
                window.blit(protagR[self.walkCount//3], dest)
                self.walkCount += 1
            elif self.up:
                window.blit(protagU[self.walkCount//3], dest)
                self.walkCount += 1
            elif self.down:
                window.blit(protagD[self.walkCount//3], dest)
                self.walkCount += 1
            else:
                window.blit(protagL[self.walkCount//3], dest)
            pygame.display.update()

地图显示代码:

def multilineRender(screen, text, x, y, the_font, colour=(0, 0, 0), justification="left"):
    justification = justification[0].upper()
    # text = text.strip().replace('\r','').split('\n')
    max_width = 0
    text_bitmaps = []
    # Convert all the text into bitmaps, calculate the justification width
    for char in text:
        text_bitmap = the_font.render(char, True, colour)
        text_width = text_bitmap.get_width()
        text_bitmaps.append((text_width, text_bitmap))
        if (max_width < text_width):
            max_width = text_width
    # Paint all the text bitmaps to the screen with justification
    for (width, bitmap) in text_bitmaps:
        xPos = x
        width_diff = max_width - width
        if justification == 'R':  # right-justify
            xPos = x + width_diff
        elif justification == 'C':  # centre-justify
            xPos = x + (width_diff // 2)

        screen.blit(bitmap, (xPos, y))
        y += bitmap.get_height()
    pygame.display.update()

字符显示代码:

def movePlayer(board):
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(15)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        arrowKeys = pygame.key.get_pressed()
        if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
            board.x -= board.vel
            board.left = True
            board.right = False
            board.up = False
            board.down = False
        elif arrowKeys[pygame.K_RIGHT] and board.x < 510 - board.width - board.vel:
            board.x += board.vel
            board.right = True
            board.left = False
            board.up = False
            board.down = False
        elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
            board.y -= board.vel
            board.right = False
            board.left = False
            board.up = True
            board.down = False
        elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
            board.y += board.vel
            board.right = False
            board.left = False
            board.up = False
            board.down = True
        elif arrowKeys[pygame.K_ESCAPE]:  # ESC key
            stopGame()
        else:
            board.right = False
            board.left = False
            board.up = False
            board.down = False
            board.walkCount = 0

    board.redrawGameWindow(window)
    pygame.display.quit() 

主要代码:

def main():
    inFile = open("map(TEST).txt", "r")
    text = inFile.read().splitlines()  # splitlines is to exclude the '\n'
    inFile.close()
    window.fill((0, 0, 0))

    myfont = pygame.font.SysFont("Calibri", 35)

    window.fill((0,0,0))
    board = player(30, 45, 64, 64, 0)
    multilineRender(window, text, 20, 20, myfont, (255, 255, 255))

    while True:
        # movePlayer(board)
        multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
        movePlayer(board)

我还应该提一下,当我退出程序时,出现了这个错误:

Traceback (most recent call last):
  File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 156, in <module>
    main()
  File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 153, in main
    multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
  File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 83, in multilineRender
    screen.blit(bitmap, (xPos, y))
pygame.error: display Surface quit

...但我怀疑这是否相关。

movePlayer 中删除 pygame.display.quit() 并将 movePlayer 中的 while 循环变为选择 if。请注意,您在 main 中有一个循环,因此请使用它。
此外,我建议在 main.

中执行 pygame.display.update()

主应用程序循环必须:

  • 处​​理事件并根据输入事件更新对象。
  • 清除显示
  • 绘制场景
  • 更新显示
def main():
    inFile = open("map(TEST).txt", "r")
    text = inFile.read().splitlines()  # splitlines is to exclude the '\n'
    inFile.close()
    myfont = pygame.font.SysFont("Calibri", 35)

    board = player(30, 45, 64, 64, 0)

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(15)

        # clear the display
        window.fill((0, 0, 0))

        # draw the scene
        board.redrawGameWindow(window)
        multilineRender(window, text, 20, 20, myfont, (255, 255, 255))

        # update the display 
        pygame.display.update()

        # handle the events and update the objects
        run = movePlayer(board)

movePlayer 不得不 return true 游戏还在 运行 否则 false:

def movePlayer(board):

    run = True
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    arrowKeys = pygame.key.get_pressed()
    if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
        board.x -= board.vel
        board.left, board.right, board.up, board.down = True, False, False, False
    elif arrowKeys[pygame.K_RIGHT] and board.x < 510 - board.width - board.vel:
        board.x += board.vel
        board.left, board.right, board.up, board.down = False, True, False, False
    elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
        board.y -= board.vel
        board.left, board.right, board.up, board.down = False, False, True, False
    elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
        board.y += board.vel
        board.left, board.right, board.up, board.down = False, False, False, True
    elif arrowKeys[pygame.K_ESCAPE]:  # ESC key
        stopGame()
    else:
        board.left, board.right, board.up, board.down = False, False, False, False
        board.walkCount = 0

    return run

multilineRender 没有 pygame.display.update():

def multilineRender(screen, text, x, y, the_font, colour=(0, 0, 0), justification="left"):
    justification = justification[0].upper()
    # text = text.strip().replace('\r','').split('\n')
    max_width = 0
    text_bitmaps = []
    # Convert all the text into bitmaps, calculate the justification width
    for char in text:
        text_bitmap = the_font.render(char, True, colour)
        text_width = text_bitmap.get_width()
        text_bitmaps.append((text_width, text_bitmap))
        if (max_width < text_width):
            max_width = text_width
    # Paint all the text bitmaps to the screen with justification
    for (width, bitmap) in text_bitmaps:
        xPos = x
        width_diff = max_width - width
        if justification == 'R':  # right-justify
            xPos = x + width_diff
        elif justification == 'C':  # centre-justify
            xPos = x + (width_diff // 2)

        screen.blit(bitmap, (xPos, y))
        y += bitmap.get_height()