2017-02-04 247 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顯示文本,否則只顯示背景。否則,當你回到循環中時,所有內容都會再次打印。