如何在矩形按钮上循环 3 个图像? [英] How to cycle 3 images on a rect button?
问题描述
刚开始学习 Python/Pygame 看视频和阅读学习.我想看一个示例代码,通过鼠标按下在矩形按钮上循环 3 个图像并返回到第一个图像.我真的希望图片是三个选项并返回不同的结果.所以能够循环图像能够在触发执行选择时选择选项和选项.
Just started learning Python/Pygame watching videos and reading to learn . I would like to see a example code to cycle 3 images on a rect button from a mouse press and return to first image. Really I want the pictures to be three options and return different results. So be able to cycle image be able to select option and option when triggered execute choice.
推荐答案
如果我理解正确的话,你需要一个按钮,每次点击它时都会改变外观,并相应地改变其相关功能.
If I understood the question correctly, you need a single button that changes the look every time you click on it, and changes its relative function accordingly.
您应该能够通过创建一个接受两个列表和一个计数器的类来解决您的问题
You should be able to solve your problem by creating a class that takes two list and a counter
1) 图像列表
2) 函数列表
3) 计数器会告诉您选择了哪个图像/功能.
3) the counter tells you which image/function is selected.
函数需要在类中构建,但您可以在类参数中提供您想要的图像(实际上,您可以将它们作为参数传递,但我认为不值得).
The functions needs to be built in the class, but you can provide the image that you want in the class argument (actually, you could pass them as arguments, but I don't think is worth it).
这是代码,我用它们的预期含义注释了一些行(行注释)
Here is the code, I commented some lines with their intended meaning (in line comments)
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()
这篇关于如何在矩形按钮上循环 3 个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!