Advertisement
Windspar

Pygame button. Thinking outside the box.

Mar 5th, 2025
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. import pygame
  2.  
  3. # Rough idea.
  4. class ButtonSytle:
  5.     def __init__(self, font, font_color, button_color, shadow_color):
  6.         self.font = font
  7.         self.font_color = font_color
  8.         self.button_color = button_color
  9.         self.shadow_color = shadow_color
  10.  
  11.     def render_text(self, text):
  12.         return self.font.render(text, 1, self.font_color)
  13.  
  14.     def render_shadow_text(self, text):
  15.         return self.font.render(text, 1, self.shadow_color)
  16.  
  17. class ButtonFeature:
  18.     def __init__(self):
  19.         pass
  20.  
  21.     def draw(self, button):
  22.         pass
  23.  
  24.     def parent_data(self, button):
  25.         pass
  26.  
  27. class Text(ButtonFeature):
  28.     def __init__(self, text, style, anchor='center'):
  29.         super().__init__()
  30.         self.text = text
  31.         self.style = style
  32.         self.anchor = anchor
  33.         self.build()
  34.  
  35.     def build(self):
  36.         self.image = self.style.render_text(self.text)
  37.         self.rect = self.image.get_rect()
  38.  
  39.     def draw(self, button):
  40.         button.image.blit(self.image, self.rect)
  41.  
  42.     def parent_data(self, button):
  43.         position = getattr(button.local_rect, self.anchor)
  44.         setattr(self.rect, self.anchor, position)
  45.  
  46. class ShadowText(Text):
  47.     def __init__(self, text, style, angle, distance, anchor='center'):
  48.         super().__init__(text, style, anchor)
  49.         self.angle = angle
  50.         self.distance = distance
  51.  
  52.     def build_shadow(self):
  53.         self.shadow_image = self.style.render_shadow_text(self.text)
  54.         self.shadow_rect = self.shadow_image.get_rect(center=self.rect.center)
  55.         direction = pygame.Vector2()
  56.         direction.from_polar((self.distance, self.angle))
  57.         self.shadow_rect.center += direction
  58.  
  59.     def draw(self, button):
  60.         button.image.blit(self.shadow_image, self.shadow_rect)
  61.         super().draw(button)
  62.  
  63.     def parent_data(self, button):
  64.         super().parent_data(button)
  65.         self.build_shadow()
  66.  
  67. class Button:
  68.     def __init__(self, name, style, size, position, features, anchor='topleft'):
  69.         self.name = name
  70.         self.style = style
  71.         self.original_image = pygame.Surface(size, pygame.SRCALPHA)
  72.         self.original_image.fill(style.button_color)
  73.         self.image = self.original_image.copy()
  74.         self.local_rect = self.image.get_rect()
  75.         self.rect = self.image.get_rect(**{anchor: position})
  76.  
  77.         for feature in features:
  78.             feature.parent_data(self)
  79.             feature.draw(self)
  80.  
  81.     def action(self):
  82.         print(self.name, 'was pressed')
  83.  
  84.     def draw(self, surface):
  85.         surface.blit(self.image, self.rect)
  86.  
  87.     def clicked(self, pos):
  88.         return self.rect.collidepoint(pos)
  89.  
  90. def main():
  91.     pygame.display.set_caption("Testing")
  92.     screen = pygame.display.set_mode((800, 600))
  93.     clock = pygame.time.Clock()
  94.     fps = 30
  95.  
  96.     font = pygame.font.Font(None, 24)
  97.     style = ButtonSytle(font, 'lightblue', 'dodgerblue', 'navy')
  98.     buttons = [
  99.         Button('MyButton', style, (120, 40), (10, 60), [Text('My Button', style)]),
  100.         Button('ShadowButton', style, (160, 40), (10, 10), [ShadowText('Shadow Button', style, 45, 3)])
  101.     ]
  102.  
  103.     running = True
  104.     while running:
  105.         for event in pygame.event.get():
  106.             if event.type == pygame.MOUSEBUTTONDOWN:
  107.                 if event.button == 1:
  108.                     for button in buttons:
  109.                         if button.clicked(event.pos):
  110.                             button.action()
  111.             elif event.type == pygame.QUIT:
  112.                 running = False
  113.  
  114.         screen.fill('black')
  115.         for button in buttons:
  116.             button.draw(screen)
  117.  
  118.         pygame.display.flip()
  119.         clock.tick(fps)
  120.  
  121. pygame.init()
  122. main()
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement