You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Pygame中screen.fill无法清除Blit绘制的内容?

问题:Pygame调用screen.fill无法完全清除屏幕,文本残留

我用Pygame开发Python程序时,发现调用screen.fill没法完全清屏,还会有文本残留。

我的代码

主循环代码

while True:
    scene0()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            screen.fill("White")
            pygame.display.flip()

scene0()函数代码

def scene0():
        into_font = pygame.font.Font(None, 200)
        intro_surf = into_font.render('Game Name', False, 'Black').convert()
        intro_rect = intro_surf.get_rect(center=(600, 300))
        txt_font = pygame.font.Font(None, 90)
        text_surf = txt_font.render('Press Space to Start', False, "Black").convert()
        text_rect = text_surf.get_rect(center=(600,420))

        screen.blit(intro_surf, intro_rect)
        screen.blit(text_surf, text_rect)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

            if exit_button.draw(screen):
                run = exit()

问题原因

  1. 主循环逻辑错误:每次循环都会先执行scene0()把文本画到屏幕上,之后才处理空格键事件调用screen.fill清屏。但主循环的下一次迭代又会立刻调用scene0(),把文本重新画回去——你看到的不是残留,是新绘制的内容。
  2. 事件处理重复:scene0()里也调用了pygame.event.get(),会把事件队列里的事件提前取走,导致主循环可能收不到空格键或退出事件。
  3. 字体渲染问题:用convert()渲染文本时没处理透明背景,可能导致文本区域出现异常色块(虽然不是当前清屏问题的直接原因,但需要修复)。

修复方案

重构主循环,用状态变量控制当前显示的场景,统一在主循环处理事件,并且每次绘制前先清屏:

import pygame
pygame.init()
screen = pygame.display.set_mode((1200, 600))
# 此处假设你已正确初始化exit_button
exit_button = ... 

# 用状态变量标记当前显示的场景
current_scene = "scene0"

def scene0():
    into_font = pygame.font.Font(None, 200)
    # 用convert_alpha()保证文本透明背景正常
    intro_surf = into_font.render('Game Name', False, 'Black').convert_alpha()
    intro_rect = intro_surf.get_rect(center=(600, 300))
    txt_font = pygame.font.Font(None, 90)
    text_surf = txt_font.render('Press Space to Start', False, "Black").convert_alpha()
    text_rect = text_surf.get_rect(center=(600,420))

    screen.blit(intro_surf, intro_rect)
    screen.blit(text_surf, text_rect)
    
    # 只绘制按钮,事件统一交给主循环处理
    if exit_button.draw(screen):
        pygame.quit()
        exit()

while True:
    # 统一处理所有事件,避免事件丢失
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            # 按下空格切换到空场景
            current_scene = "empty"

    # 每次绘制前先清屏,保证画布干净
    screen.fill("White")
    
    # 根据当前状态绘制对应内容
    if current_scene == "scene0":
        scene0()
    # 空场景不需要绘制任何内容,只保留清屏后的白色背景

    # 统一更新显示
    pygame.display.flip()

核心修复点

  • 用状态变量控制场景切换,按下空格后不再执行scene0(),自然不会重复绘制文本。
  • 所有事件都在主循环处理,避免scene0()提前消耗事件。
  • 每次循环先清屏再绘制内容,从根本上避免残留问题。
  • 字体渲染改用convert_alpha(),修复透明背景异常。

内容的提问来源于stack exchange,提问作者fleya wo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 01:54:56