2015-11-22 33 views
0

剛開始學習的Python/pygame的觀看視頻和閱讀中學習。我希望看到一個示例代碼,用鼠標按鈕在rect按鈕上循環顯示3張圖像並返回到第一張圖像。真的,我希望圖片是三個選項,並返回不同的結果。因此,當觸發執行選擇時,能夠循環圖像能夠選擇選項和選項。如何在rect按鈕上循環3張圖像?

+0

與圖像創建列表,然後使用'指數=(索引+ 1)MOD 3'循環索引。 – furas

回答

0

import pygame 

pygame.init() 

screen = pygame.display.set_mode((300,200)) 

# three images 
images = [ 
    pygame.Surface((100,100)),  
    pygame.Surface((100,100)),  
    pygame.Surface((100,100)),  
] 

images[0].fill((255,0,0)) 
images[1].fill((0,255,0)) 
images[2].fill((0,0,255)) 

# image size and position  
images_rect = images[0].get_rect() 

# starting index 
index = 0 

running = True 

while running: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
     elif event.type == pygame.MOUSEBUTTONDOWN: 
      # check mouse position and pressed button 
      if event.button == 1 and images_rect.collidepoint(event.pos): 
       # cycle index 
       index = (index+1) % 3 

    screen.blit(images[index], images_rect) 
    pygame.display.flip() 

pygame.quit() 

示例使用類 - 創建許多按鈕

import pygame 

class Button(object): 

    def __init__(self, position, size): 
     self._images = [ 
      pygame.Surface(size),  
      pygame.Surface(size),  
      pygame.Surface(size),  
     ] 

     self._images[0].fill((255,0,0)) 
     self._images[1].fill((0,255,0)) 
     self._images[2].fill((0,0,255)) 

     self._rect = pygame.Rect(position, size) 

     self._index = 0 

    def draw(self, screen): 
     screen.blit(self._images[self._index], self._rect) 

    def event_handler(self, event): 
     if event.type == pygame.MOUSEBUTTONDOWN: 
      if event.button == 1 and self._rect.collidepoint(event.pos): 
       self._index = (self._index+1) % 3 


pygame.init() 

screen = pygame.display.set_mode((320,110)) 

button1 = Button((5, 5), (100, 100)) 
button2 = Button((110, 5), (100, 100)) 
button3 = Button((215, 5), (100, 100)) 

running = True 

while running: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
     button1.event_handler(event) 
     button2.event_handler(event) 
     button3.event_handler(event) 

    button1.draw(screen) 
    button2.draw(screen) 
    button3.draw(screen) 

    pygame.display.flip() 

pygame.quit() 

http://imgur.com/BWzrvOG

0

如果我理解正確的問題,你需要一個按鈕,每一次改變一下你點擊它,並相應地改變它的相對功能。

您應該能夠通過創建一個類,需要兩個列表和反

1)圖像

2)的函數列表

3)計數器告訴列表來解決你的問題你選擇了哪個圖像/功能。

的功能需要建立在類,但您可以提供您在類參數需要的圖像(實際上,你可以將它們作爲論據,但我不認爲是值得的)。

下面是代碼,我評論一些線與它們的預期含義(在線評論)

import pygame 
import sys 
pygame.init() 

width = 600 
height = 400 

screen = pygame.display.set_mode((width, height)) 
pygame.display.set_caption("Magic Buttons") 
background = pygame.Surface(screen.get_size()) 

clock = pygame.time.Clock() 

class Buttons: 
    def __init__(self, posX, posY, image1, image2, image3): 
     self.image_list = [image1, image2, image3] # static list of images 
     self.function_list = [self.button_function_1,self.button_function_2,self.button_function_3 ] 
     self.rect_position = (posX, posY) # this is a tuple to identify the upper left corner of the rectangle of the image 
     self.button_type = 0 # initial value of the button, both for the function and the image 
     self.image = pygame.image.load(self.image_list[0]) #default image, index number 0 of image_list 
     self.rect = pygame.Rect(posX, posY, self.image.get_width(), self.image.get_height()) # create a rectangle object same size of the images 

    def check(self, pos): 
     if self.rect.collidepoint(pos) ==True: 
      self.change_button() 
     else: 
      pass 

    def change_button(self): 
     self.button_type = (self.button_type +1)%3 
     self.image = pygame.image.load(self.image_list[self.button_type ]) # load the image relative to button_type 
     self.function_list[self.button_type -1]() # execute the function relative to the new value of button_type 
     self.draw_button() 

    def draw_button(self): 
     screen.blit(self.image, self.rect_position) # blit the new button image 

    def button_function_1(self): 
     print ("function 1 in action") 

    def button_function_2(self): 
     print ("function 2 in action") 

    def button_function_3(self): 
     print ("function 3 in action") 


multibutton = Buttons(100,100,"button1.png","button2.png","button3.png") # create an istance of the button in the x=100, y = 100, with the three image button 



while True: 
    background.fill((0,0,0)) 

    clock.tick(30) 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type == pygame.MOUSEBUTTONUP: 
      pos = pygame.mouse.get_pos() # fetch the position of the mouse 
      multibutton.check(pos) # check if the mouse is on the button 

    multibutton.draw_button() 

    pygame.display.flip()