簡體   English   中英

如何停止 pygame 圖片重疊

[英]How do I stop pygame pictures overlapping

#in the game loop
if event.key == pygame.K_p:
    paused = True
    pause_menu(paused, sound_state)

    if updated_sound_state:
        sound_state = True
        mixer.music.unpause()
    elif not updated_sound_state:
        sound_state = False
        mixer.music.pause()

#the pause menu
def pause_menu(paused, sound_state):
    global updated_sound_state
    updated_sound_state = sound_state

    mixer.music.pause()

    if updated_sound_state:
        screen.blit(sound_on_image, (5, (display_height - sound_image_y_size)))
    if not updated_sound_state:
        screen.blit(sound_off_image, (5, (display_height - sound_image_y_size)))

    while paused:
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_p:
                    paused = False

                if event.key == pygame.K_s:
                    if sound_state:
                        updated_sound_state = False
                        paused = False
                        pause_menu(paused=True, sound_state=updated_sound_state)
                    elif not sound_state:
                        updated_sound_state = True
                        paused = False
                        pause_menu(paused=True, sound_state=updated_sound_state)

    return sound_state

當我打開暫停菜單時,它會顯示正確的圖像,但是當我打開或關閉聲音時,它會顯示兩個圖像。

當我退出暫停並 go 重新暫停時,它確實顯示了正確的圖像。

我該如何解決?

不要遞歸調用pause_menu 您必須在每一幀中清除繪制場景。 獲取 ` 之前的屏幕copy

current_screen = screen.copy()

在暫停循環中對副本進行blit並在其上繪制菜單:

screen.blit(current_screen, (0, 0))     
if updated_sound_state:
    screen.blit(sound_on_image, (5, (display_height - sound_image_y_size)))
if not updated_sound_state:
    screen.blit(sound_off_image, (5, (display_height - sound_image_y_size)))
pygame.display.update()

完成pause_menu function:

def pause_menu(paused, sound_state):
    global updated_sound_state
    updated_sound_state = sound_state

    mixer.music.pause()

    current_screen = screen.copy()

    while paused:
    
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_p:
                    paused = False

                if event.key == pygame.K_s:
                    if sound_state:
                        updated_sound_state = False
                        paused = False
                    elif not sound_state:
                        updated_sound_state = True
                        paused = False
                    
        screen.blit(current_screen, (0, 0))     
        if updated_sound_state:
            screen.blit(sound_on_image, (5, (display_height - sound_image_y_size)))
        if not updated_sound_state:
            screen.blit(sound_off_image, (5, (display_height - sound_image_y_size)))
        pygame.display.update()

    return sound_state

典型的 PyGame 應用循環必須:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM