我想删除PygButton创建的按钮。我已经创建了它:
button1 = pygbutton.PygButton((50, 50, 60, 30), '1')
button2 = pygbutton.PygButton((120, 50, 60, 30), '2')
button3 = pygbutton.PygButton((190, 50, 60, 30), '3')
allButtons = (button1,button2,button3)
for b in allButtons:
b.draw(screen)
然而,一旦点击一个按钮,我想清除屏幕上的按钮并在屏幕上显示其他内容。
我该怎么做?
答案 0 :(得分:2)
我想到的一般想法是在按下按钮后制作新屏幕。
基本上,我有一个叫做buttonhasbeenpressed
的布尔。在按下按钮之前,我们只是检查event
是否按下按钮。按下它之后,我们将bool设置为True
,“清除”背景(通过在旧屏幕上创建一个新屏幕),然后继续执行我们想要的任何其他操作。我的示例代码只“删除”按钮,更改背景颜色,并更改窗口上的标题,但您可以使用此提示更改您对游戏后按钮按下状态的任何想法。
以下是您应该可以在您的计算机上运行以进行测试的示例。
import pygame,pygbutton
from pygame.locals import *
pygame.init()
#Create the "Pre Button Press Screen"
width = 1024
height = 768
screen = pygame.display.set_mode([width,height])
pygame.display.set_caption('OLD SCREEN NAME')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
screen.blit(background, [0,0])
pygame.display.flip()
button1 = pygbutton.PygButton((50, 50, 60, 30), '1')
button2 = pygbutton.PygButton((120, 50, 60, 30), '2')
button3 = pygbutton.PygButton((190, 50, 60, 30), '3')
buttonhasbeenpressed = False
def screenPostButtonPress():
width = 1024
height = 768
screen = pygame.display.set_mode([width,height])
pygame.display.set_caption('NEW SCREEN NAME!!!!!!!')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((20, 20, 40))
screen.blit(background, [0,0])
pygame.display.flip()
#buttons not on screen after a button has been pressed
def waitingForButtonClick():
allButtons = [button1,button2,button3]
buttonevent1 = button1.handleEvent(event)
buttonevent2 = button2.handleEvent(event)
buttonevent3 = button3.handleEvent(event)
for b in allButtons:
b.draw(screen)
if 'click' in buttonevent1 or 'click' in buttonevent2 or 'click' in buttonevent3:
return False
return True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#Wait for a button to be pressed, once one has, "clear" the screen by creating a new screen
if buttonhasbeenpressed == False and waitingForButtonClick() == False:
buttonhasbeenpressed = True
screenPostButtonPress()
pygame.display.update()