python - 我的 Sprite 在 Pygame 中无法正确显示

标签 python pygame sprite display pygame-surface

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

显示 Sprite 代码:

# 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 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()

map 显示代码:

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 必须返回 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()

关于python - 我的 Sprite 在 Pygame 中无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60134387/

相关文章:

java - 控制和创建多个 Sprite 数组 Java Libgdx

ios - Monotouch中最有效的sprite(frame)动画方式

python - 为什么Python2.x中新型类的 "object"父类不写成 "Object"?

python - 将额外的 void* 与 C 回调一起传递是标准做法吗

python - 如何将圆圈移动到预定的 x,y 坐标?

python - 轨迹球转动不正确

python - 如何在 pygame 中用我旋转和移动的汽车旋转我的碰撞箱?

python - pyGTK 自动调整笔记本选项卡大小

java - 如何将 "compact"Python 移植到 "compact"Java?

swift - 调整 Sprite 的亮度