Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- # Rough idea.
- class ButtonSytle:
- def __init__(self, font, font_color, button_color, shadow_color):
- self.font = font
- self.font_color = font_color
- self.button_color = button_color
- self.shadow_color = shadow_color
- def render_text(self, text):
- return self.font.render(text, 1, self.font_color)
- def render_shadow_text(self, text):
- return self.font.render(text, 1, self.shadow_color)
- class ButtonFeature:
- def __init__(self):
- pass
- def draw(self, button):
- pass
- def parent_data(self, button):
- pass
- class Text(ButtonFeature):
- def __init__(self, text, style, anchor='center'):
- super().__init__()
- self.text = text
- self.style = style
- self.anchor = anchor
- self.build()
- def build(self):
- self.image = self.style.render_text(self.text)
- self.rect = self.image.get_rect()
- def draw(self, button):
- button.image.blit(self.image, self.rect)
- def parent_data(self, button):
- position = getattr(button.local_rect, self.anchor)
- setattr(self.rect, self.anchor, position)
- class ShadowText(Text):
- def __init__(self, text, style, angle, distance, anchor='center'):
- super().__init__(text, style, anchor)
- self.angle = angle
- self.distance = distance
- def build_shadow(self):
- self.shadow_image = self.style.render_shadow_text(self.text)
- self.shadow_rect = self.shadow_image.get_rect(center=self.rect.center)
- direction = pygame.Vector2()
- direction.from_polar((self.distance, self.angle))
- self.shadow_rect.center += direction
- def draw(self, button):
- button.image.blit(self.shadow_image, self.shadow_rect)
- super().draw(button)
- def parent_data(self, button):
- super().parent_data(button)
- self.build_shadow()
- class Button:
- def __init__(self, name, style, size, position, features, anchor='topleft'):
- self.name = name
- self.style = style
- self.original_image = pygame.Surface(size, pygame.SRCALPHA)
- self.original_image.fill(style.button_color)
- self.image = self.original_image.copy()
- self.local_rect = self.image.get_rect()
- self.rect = self.image.get_rect(**{anchor: position})
- for feature in features:
- feature.parent_data(self)
- feature.draw(self)
- def action(self):
- print(self.name, 'was pressed')
- def draw(self, surface):
- surface.blit(self.image, self.rect)
- def clicked(self, pos):
- return self.rect.collidepoint(pos)
- def main():
- pygame.display.set_caption("Testing")
- screen = pygame.display.set_mode((800, 600))
- clock = pygame.time.Clock()
- fps = 30
- font = pygame.font.Font(None, 24)
- style = ButtonSytle(font, 'lightblue', 'dodgerblue', 'navy')
- buttons = [
- Button('MyButton', style, (120, 40), (10, 60), [Text('My Button', style)]),
- Button('ShadowButton', style, (160, 40), (10, 10), [ShadowText('Shadow Button', style, 45, 3)])
- ]
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1:
- for button in buttons:
- if button.clicked(event.pos):
- button.action()
- elif event.type == pygame.QUIT:
- running = False
- screen.fill('black')
- for button in buttons:
- button.draw(screen)
- pygame.display.flip()
- clock.tick(fps)
- pygame.init()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement