2017-02-04 246 views
1

我有一些文字已经在窗口上呈现。当我使用screen.fill()函数时,文本仍然保留在屏幕前方,所以背景会发生变化,但文本仍然未被覆盖。删除pygame中的渲染文本

#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() 
+0

改为'1 == 1',你可以使用'True' - 'True和菜单== 0:'但是同样给出'而菜单== 0:'。但是你可以使用'menu = True'而不是'menu = 0',然后'while menu:' – furas

+0

你可以在'while'和'while'之前使用两种颜色渲染文本'blit() – furas

回答

2

在你while循环,你叫fill功能,然后循环备份和打印文本。

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

通常你先画背景,然后打印各个元素(用图层思考)。

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