我遇到了这样的问题:当我在我的pygame应用程序中添加3个时,所有按钮的背景都与上次添加的按钮相同。
我有窗口课程:
class MainMenuWindow(Window):
# singeltone:
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(MainMenuWindow, cls).__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self):
self.background = pygame.transform.scale(pygame.image.load(GLOBAL_APPLICATION_ROOT_PATH.__str__() + "/src/img/backgrounds/main_window.jpg"), \
(GLOBAL_SCREEN_WIDTH, GLOBAL_SCREEN_HEIGHT))
self.window = pygame.display.get_surface()
pygame.mouse.set_visible(1)
pygame.key.set_repeat(0)
btn = Button((130,22),(250,200),"start_playing.png","start_playing","")
btn2 = Button((130,22),(250,250),"options.png","options","")
btn3 = Button((130,22),(250,450),"exit.png","exit","")
self.buttons_list.append(btn)
self.buttons_list.append(btn2)
self.buttons_list.append(btn3)
def draw(self):
"""
Render buttons on screen
"""
self.window.blit(self.background, (0, 0))
for btn in self.buttons_list:
btn.render()
班级按钮:
class Button(Element):
"""
Sprites sheet MUST contain 4 representative images in following order: 'normal','hover','active','disabled'
"""
__src_path = os.path.join(GLOBAL_APPLICATION_ROOT_PATH,"src","img","interface")
# width & height of button
button_size = (100,50)
# x, y position coordinates
button_position = (0,0)
# list of button background states @Surfaces
button_background = {'normal': None, 'hover': None, 'active': None, 'disabled': None}
# atlas with button states backgrounds
master_image_atlas = None
# button ID
button_id = ""
# text on button
button_caption = "Button"
# rect object
button_rect = pygame.Rect(button_position, button_size)
button_status = "normal" # can be 'normal', 'hover', 'active', 'disabled'
hover = False
pressed = False
def __init__(self, size=(20, 10), position=(0, 0), image_atlas="", btn_id="btn_1", btn_caption="Button1"):
"""
:param size: (x,y) size of button
:param position: (x,y) position coordinates
:param image_atlas: name of image file with button sprites
:param btn_id: string ID of button
:param btn_caption: Button caption
"""
super().__init__()
self.button_size = size
self.button_position = position
self.master_image_atlas = image_atlas
self.button_id = btn_id
self.button_caption = btn_caption
self.button_rect = pygame.Rect(self.button_position[0], self.button_position[1], self.button_size[0],self.button_size[1])
self.prepare()
def prepare(self):
"""
Create complete Button object
"""
image_atlas = None
if self.master_image_atlas != None:
image_atlas = slice_image(os.path.join(self.__src_path,"buttons",self.master_image_atlas), self.button_size[0],self.button_size[1])
self.button_background['normal'] = image_atlas[0][0]
self.button_background['hover'] = image_atlas[0][1]
self.button_background['active'] = image_atlas[0][2]
self.button_background['disabled'] = image_atlas[0][3]
else:
image_atlas = None
return self
def render(self):
screen = pygame.display.get_surface()
screen.blit(self.button_background[self.button_status],self.button_position)
def update_state(self,event_handler_object):
state = event_handler_object.check_element_state(self)
if state[1] is None:
self.set_visual_status("normal")
self.pressed = False
return
elif state[1]['status'] == "hover":
self.set_visual_status("hover")
self.pressed = False
return
elif state[1]['status'] == "click":
self.set_visual_status("active")
self.pressed = True
return
类元素:
class Element:
"""
Implements basic functionality for all interface elements
"""
__src_path = os.path.join(GLOBAL_APPLICATION_ROOT_PATH,"src","img","interface")
# width & height of button
button_size = (100,50)
# x, y position coordinates
button_position = (0,0)
# list of button background states @Surfaces
button_background = {'normal': None, 'hover': None, 'active': None, 'disabled': None}
# atlas with button states backgrounds
master_image_atlas = None
# button ID
button_id = ""
# text on button
button_caption = "Button"
# rect object
button_rect = pygame.Rect(button_position, button_size)
button_status = "normal" # can be 'normal', 'hover', 'active', 'disabled'
hover = False
pressed = False
def __init__(self,size=(20,10),position=(0,0),image_atlas="",btn_id="btn_1",btn_caption="Button1"):
pass
def prepare(self):
pass
def update_state(self,event_handler_object):
pass
def get_object_id(self):
return self.button_id
def get_object_rectangle(self):
return self.button_rect
def set_visual_status(self,status):
self.button_status = status
def is_pressed(self):
if self.pressed==True:
#print(["pressed",self.button_id])
return self.button_id
def render(self):
pass
当我运行我的应用程序时,我有3个具有相同图像的按钮:
当我点击任何按钮时 - 应用程序返回正确的点击按钮ID。 按钮精灵有什么问题?