2014-10-08 81 views
2

我想要删除由PygButton创建的按钮。我已经创造了它这样:如何删除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) 

然而,一旦按钮被点击,我想清除屏幕上的按钮和显示屏幕上的东西。

我该怎么做?

回答

2

我想到的一般想法是在按下按钮之后创建新的屏幕。

基本上,我有一个bool,我叫buttonhasbeenpressed。在按下按钮之前,我们只是检查event是否是按钮按钮。按下之后,我们将布尔设置为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()