Esempio n. 1
0
class Button(GeneralObject):
    def __init__(self,
                 x,
                 y,
                 w,
                 h,
                 button_color_dict,
                 text,
                 text_color,
                 on_click=lambda x: None,
                 key=None):
        GeneralObject.__init__(self, x, y, w, h)
        self.state = 'normal'
        self.button_color_dict = button_color_dict
        self.on_click = on_click
        self.text_color = text_color
        self.key = key
        self.text = TextObject(self.centerx, self.centery, lambda: text,
                               self.text_color, c.button_font_name,
                               c.button_font_size, True)

    def draw(self, surface):
        pygame.draw.rect(surface, self.back_color, self.rect)
        self.text.draw(surface)

    def handle_mouse_event(self, type, pos):
        if type == pygame.MOUSEMOTION:
            self.handle_mouse_move(pos)
        elif type == pygame.MOUSEBUTTONDOWN:
            self.handle_mouse_down(pos)
        elif type == pygame.MOUSEBUTTONUP:
            self.handle_mouse_up(pos)

    def handle_mouse_move(self, pos):
        if self.rect.collidepoint(pos):
            if self.state != 'pressed':
                self.state = 'hover'
        else:
            self.state = 'normal'

    def handle_mouse_down(self, pos):
        if self.rect.collidepoint(pos):
            self.state = 'pressed'

    def handle_mouse_up(self, pos):
        if self.state == 'pressed':
            if self.key is not None:
                self.on_click(self, self.key)
            else:
                self.on_click(self)
            self.state = 'hover'

    @property
    def back_color(self):
        return self.button_color_dict[self.state]
Esempio n. 2
0
class Button(GameObject):
    def __init__(self,
                 x,
                 y,
                 w,
                 h,
                 text,
                 on_click=lambda x: None,
                 padding=0,
                 size=cfg.font_size):
        super().__init__(x, y, w, h)
        self.state = 'normal'
        self.on_click = on_click
        self.text = TextObject(x + padding,
                               y + padding,
                               text,
                               color=cfg.button_text_color,
                               font_size=size)
        self.text_easy = text

    @property
    def back_color(self):
        return dict(normal=cfg.button_normal_back_color,
                    hover=cfg.button_hover_back_color,
                    pressed=cfg.button_pressed_back_color)[self.state]

    def draw(self, surface):
        pygame.draw.rect(surface, self.back_color, self.bound)
        pygame.draw.rect(surface, cfg.BLACK, self.bound, 1)
        self.text.draw(surface)

    def handle_mouse_event(self, type, pos):
        if type == pygame.MOUSEMOTION:
            self.handle_mouse_move(pos)
        elif type == pygame.MOUSEBUTTONDOWN:
            self.handle_mouse_down(pos)
        elif type == pygame.MOUSEBUTTONUP:
            self.handle_mouse_up(pos)

    def handle_mouse_move(self, pos):
        if self.bound.collidepoint(pos):
            if self.state != 'pressed':
                self.state = 'hover'
        else:
            self.state = 'normal'

    def handle_mouse_down(self, pos):
        if self.bound.collidepoint(pos):
            self.state = 'pressed'

    def handle_mouse_up(self, pos):
        if self.state == 'pressed':
            self.on_click(self)
            self.state = 'hover'
Esempio n. 3
0
class Button(GameObject):
    def __init__(self, x, y, w, h, text, button_type):
        super().__init__(x, y, w, h)
        self.type = button_type
        self.text = TextObject(x + 1, y + 10, lambda: text,
                               config.BUTTON_TEXT_COLOR, config.BUTTON_FONT,
                               config.BUTTON_SIZE)

    def draw(self, surface):
        draw.rect(surface, config.BUTTON_COLOR, self.bounds)
        self.text.draw(surface)
 def show_message(self,
                  text,
                  color=colors.WHITE,
                  font_name='Arial',
                  font_size=20,
                  centralized=False):
     message = TextObject(c.screen_width // 2, c.screen_height // 2,
                          lambda: text, color, font_name, font_size)
     self.draw()
     message.draw(self.surface, centralized)
     pygame.display.update()
     time.sleep(c.message_duration)
Esempio n. 5
0
 def show_message(self,
                  text,
                  color=cfg.text_color,
                  font_name=cfg.font_name,
                  font_size=cfg.font_size):
     message = TextObject(cfg.screen_width // 2 - 40,
                          (cfg.screen_height // 2) - 40, text, font_size,
                          color, font_name)
     self.draw()
     message.draw(self.surface)
     pygame.display.update()
     time.sleep(3)
Esempio n. 6
0
 def show_message(self,
                  text,
                  color=colors.GRAY24,
                  font_name='Century',
                  font_size=20,
                  centralized=False):
     message = TextObject(c.w_x_size // 2, c.w_y_size // 2, lambda: text,
                          color, font_name, font_size, True)
     self.draw()
     message.draw(self.surface)
     pygame.display.update()
     time.sleep(c.message_duration)
Esempio n. 7
0
 def draw_menu(self):
     w, h = pygame.display.get_surface().get_size()
     exit_button = TextObject(610, 630, "Exit(Esc)", c.font_color,
                              c.font_name, c.font_size)
     help_button = TextObject(380, 630, "Help(H)", c.font_color,
                              c.font_name, c.font_size)
     reset_button = TextObject(510, 710, "New Game(N)", c.font_color,
                               c.font_name, c.font_size)
     exit_button.draw(self.surface)
     help_button.draw(self.surface)
     reset_button.draw(self.surface)
     if self.help_opened:
         self.draw_help()
     if self.game_over:
         self.draw_victory()
Esempio n. 8
0
class Button:
    def __init__(self, x, y, w, h, text, on_click=lambda x: None, padding=0):
        self.bounds = pygame.Rect(x, y, w, h)  # прямоугольный объект
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.padding = padding
        self.state = 'normal'
        self.on_click = on_click
        self.text = TextObject(x + padding, y + padding, lambda: text,
                               c.button_text_color, c.font_name, c.font_size)

    @property
    def back_color(self):
        return dict(normal=c.button_normal_back_color,
                    hover=c.button_hover_back_color,
                    pressed=c.button_pressed_back_color)[self.state]

    def draw(self, surface):
        pygame.draw.rect(surface, self.back_color, self.bounds)
        self.text.draw(surface)

    def update(self):
        pass

    def handle_mouse_event(self, type, pos):
        if type == pygame.MOUSEMOTION:
            self.handle_mouse_move(pos)
        elif type == pygame.MOUSEBUTTONDOWN:
            self.handle_mouse_down(pos)
        elif type == pygame.MOUSEBUTTONUP:
            self.handle_mouse_up(pos)

    def handle_mouse_move(self, pos):
        if self.bounds.collidepoint(pos):
            if self.state != 'pressed':
                self.size_button(self.state)
                self.state = 'hover'
        else:
            self.size_button(self.state)
            self.state = 'normal'

    def handle_mouse_down(self, pos):
        if self.bounds.collidepoint(pos):
            self.size_button(self.state)
            self.state = 'pressed'

    def handle_mouse_up(self, pos):
        if self.state == 'pressed':
            self.size_button(self.state)
            self.on_click(self)
            self.state = 'hover'

    def size_button(self, state):
        if self.state != 'normal':
            self.bounds.height = self.h + c.button_loop_h
            self.bounds.width = self.w + c.button_loop_w
            self.bounds.x = self.x - c.button_loop_w // 2
            self.bounds.y = self.y - c.button_loop_h // 2
            self.text.change_font(
                c.font_size_loop,
                (self.bounds.x + self.padding, self.bounds.y + self.padding))
        else:
            self.bounds.height = self.h
            self.bounds.width = self.w
            self.bounds.x = self.x
            self.bounds.y = self.y
            self.text.change_font(
                c.font_size,
                (self.bounds.x + self.padding, self.bounds.y + self.padding))
Esempio n. 9
0
 def draw_victory(self):
     victory_content = TextObject(500, 30, "YOU WON! CONGRATULATIONS!",
                                  c.font_color, c.font_name, c.font_size)
     victory_content.draw(self.surface)