【问题标题】:Remove Rendered Text in Pygame在 Pygame 中删除渲染文本
【发布时间】:2017-06-21 11:19:08
【问题描述】:

我有一些已在窗口上呈现的文本。当我使用 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()

【问题讨论】:

  • 而不是1 == 1,您可以使用True - while True and menu == 0: ,但同样给出while menu == 0:。但是你可以使用menu = True 而不是menu = 0 然后while menu:
  • 您可以在while 之前和while 中渲染两种颜色的文本,仅替换blit() 中的图像

标签: python pygame render


【解决方案1】:

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

您应该将所有元素视为分层。当你从顶部开始循环时,你的函数的顺序是定义什么元素在什么其他元素的前面。

通常您首先绘制背景,然后打印各个元素(考虑图层)。

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

【讨论】:

    猜你喜欢
    • 2014-07-13
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    相关资源
    最近更新 更多