首页 > 解决方案 > 如何添加片尾画面?

问题描述

我从这里和其他地方尝试了不同的代码,但我似乎无法让我的最终屏幕工作。

我已经尝试过多次转换到结束屏幕,但要么代码完全失败,要么我的精灵出现在结束屏幕的顶部。这就是我现在拥有的,因为它允许我的代码运行。

collide = pygame.sprite.spritecollide(player, enemy_list, False)
    if collide:
        run = False

我希望当敌人精灵接触玩家精灵时游戏结束,但是由于上面的代码使敌人精灵跟随玩家精灵,所以总是出现错误:“float Division 0”。不过,使用错误的代码可能只是我的错。

我不确定其他代码是否会影响最终屏幕代码,但以防万一,这是我的整个代码:

Code is removed for now. Will re-upload in 1 to 2 months

标签: pythonpygame

解决方案


给程序添加一个gameover状态,设置玩家碰撞时的状态。

为游戏结束屏幕创建一个单独的函数。该gameover函数有自己的事件循环:

def gameOverScreen():
    global run, gameover

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

        # do event handling which continues the game
        # [...]
        # if [...]
        #     gameover = False

    # draw the game over screen
    # [...]

    pygame.display.flip()
    clock.tick(100)

gameover根据主循环中的状态调用此函数。
使用continue词干立即继续主循环。

gameover = False
run = True
while run:

    # [...]

    if not gameover and time_difference >= 1500:
        # [...]

    win.fill(white)
    win.blit(background.image, background.rect)

    if not pygame.mixer.music.get_busy():
        pygame.mixer.music.load('bgm.mp3')
        pygame.mixer.music.play()

    if gameover:
        gameOverScreen()
        continue # continue main loop

    for e in enemy_list:
        e.move(player)

    collide = pygame.sprite.spritecollide(player, enemy_list, False)
    if collide:
        gameover = True

    # [...]

推荐阅读