剛開始學習的Python/pygame的觀看視頻和閱讀中學習。我希望看到一個示例代碼,用鼠標按鈕在rect按鈕上循環顯示3張圖像並返回到第一張圖像。真的,我希望圖片是三個選項,並返回不同的結果。因此,當觸發執行選擇時,能夠循環圖像能夠選擇選項和選項。如何在rect按鈕上循環3張圖像?
0
A
回答
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()
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()
相關問題
- 1. 如何循環按鈕?
- 2. 如何循環按鈕
- 3. 如何在點擊3張圖片後顯示提交按鈕?
- 4. 如何在Spritekit/Swift 3中的圖像上創建按鈕?
- 5. 如何通過單個按鈕循環瀏覽圖像?
- 6. 如何通過單擊按鈕啓動此圖像循環?
- 7. 如何使用Previous/Next按鈕循環顯示圖像?
- 8. 如何在圖像上放置按鈕
- 9. 如何在圖像上放置按鈕
- 10. 如何在濃縮咖啡測試中循環3個按鈕
- 11. WordPress的 - 循環與張貼的圖像
- 12. MVC 3圖像按鈕
- 13. 循環按鈕
- 14. C#如何在按鈕陣列按鈕上添加圖像
- 15. JOptionPane圖像在按鈕上?
- 16. svg rect圖像不顯示在Firefox上
- 17. 在for循環中按住按鈕3秒
- 18. 如何在圖像按鈕上使用動態圖像URl
- 19. 如何設置按鈕上的圖像
- 20. 添加圖像在單張RECT多邊形
- 21. 循環在張量
- 22. 如何運行循環(例如for循環)按鈕單擊android
- 23. 如何把一張桌子和3個按鈕在一個JFrame
- 24. 按鈕按下循環
- 25. 如何用for循環制作按鈕
- 26. 如何用按鈕停止循環?
- 27. 如何通過Vaadin按鈕循環?
- 28. 如何在圖像刷卡視圖上循環?
- 29. 如何使用CSS彈出按鈕圖像上的按鈕
- 30. 如何覆蓋按鈕對象上的按鈕圖像?
與圖像創建列表,然後使用'指數=(索引+ 1)MOD 3'循環索引。 – furas