2014-10-08 79 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()