Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Another question from dumb developer :)

I have class for window:

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:

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:

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

and when I'm run my application I have 3 buttons with same image:

enter image description here

When I' clicking any button - application returns correct ID of clicked button. What is wrong with buttons sprites?

share|improve this question
 
Where do you set self.__src_path in your Button class? –  Mokosha May 19 '13 at 6:36
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.