删除 Pygame 中的渲染文本

Remove Rendered Text in Pygame

我有一些文本已在 window 上呈现。当我使用 screen.fill() 函数时,文本仍然保留在屏幕前面,因此背景发生变化,但文本仍然未被覆盖。

#Importing Stuff
import pygame
import sys
import time
import random
from pygame.locals import*
pygame.init()

#Naming Variables
menu = 0
color = (65,105,225)
tcolor = (255,255,255)
pcolor = (255,255,255)
hcolor = (255,255,255)
width, height = 1920, 1080
screen = pygame.display.set_mode((width, height))
hecolor = (255,255,255)
sys_font = pygame.font.SysFont \
           ("None", 60)

#Initializing Screen
pygame.display.set_caption("TSA Trend Game")
screen.fill(((color)))
pygame.display.update()

#Making Menu
while 1 == 1 and menu == 0:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        #More Variables
        rendered = sys_font.render \
            ("Welcome to Trends of 2016!", True, ((tcolor)))
        play = sys_font.render \
            ("Play Game", True, ((pcolor)))
        help = sys_font.render \
            ("Help", True, ((hcolor)))
        play_r = play.get_rect()
        play_r.x, play_r.y = 710, 500
        help_r = help.get_rect()
        help_r.x, help_r.y = 1170, 500
        render_r = play.get_rect()
        render_r.x, render_r.y = 710, 500
        #Display Text
        screen.blit(rendered, (710, 440))
        screen.blit(help, (1170, 500))
        screen.blit(play, (710, 500))
        pygame.display.update()
    if render_r.collidepoint(pygame.mouse.get_pos()):
        pcolor = (255,255,0)
    else:
        pcolor = (255,255,255)
    if help_r.collidepoint(pygame.mouse.get_pos()):
        hcolor = (255,255,0)
    else:
        hcolor = (255,255,255)
    if event.type == pygame.MOUSEBUTTONDOWN and help_r.collidepoint(pygame.mouse.get_pos()):
        screen.fill(pygame.Color("black"))
pygame.display.update()

在你的 while 循环中,你调用了 fill 函数,然后你循环返回并打印你的文本。

您应该认为所有元素都在图层上。当您从顶部开始循环时,函数的顺序定义了哪个元素位于其他元素之前。

通常从绘制背景开始,然后打印各个元素(考虑图层)。

如果您想单击一个按钮并隐藏菜单(我猜您正在尝试这样做),您需要在变量中保存状态记忆。例如有一个 inMenu,当 True 时显示文本,否则只显示背景。否则当你返回循环时,所有内容都会被再次打印。