Advertisement
Windspar

Displaying Pygame Builtin Colors

May 9th, 2019
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. import pygame
  2. from pygame.sprite import Group, Sprite
  3.  
  4. class SimpleButton(Sprite):
  5.     def __init__(self, font, text, rect, callback, foreground, background, midground):
  6.         Sprite.__init__(self)
  7.  
  8.         self.font = font
  9.         self.rect = rect
  10.         self.hover = False
  11.         self.callback = callback
  12.         self.midground = midground
  13.         self.foreground = foreground
  14.         self.background = background
  15.         self.image = pygame.Surface(self.rect.size)
  16.         self.text(text)
  17.  
  18.     def text(self, text):
  19.         self.text_image = self.font.render(text, 1, self.foreground)
  20.         self.update_image()
  21.  
  22.     def on_mousemotion(self, event):
  23.         hover = self.rect.collidepoint(event.pos)
  24.         if self.hover != hover:
  25.             self.hover = hover
  26.             self.update_image()
  27.  
  28.     def on_mousebuttonup(self, event):
  29.         if event.button == 1:
  30.             if self.hover:
  31.                 self.callback(self)
  32.  
  33.     def update_image(self):
  34.         if self.hover:
  35.             self.image.fill(self.midground)
  36.         else:
  37.             self.image.fill(self.background)
  38.  
  39.         rect = self.image.get_rect()
  40.         text_rect = self.text_image.get_rect()
  41.         text_rect.center = rect.center
  42.         self.image.blit(self.text_image, text_rect)
  43.  
  44. class TextBlock(Sprite):
  45.     def __init__(self, font, text, rect):
  46.         Sprite.__init__(self)
  47.  
  48.         self.font = font
  49.         self.rect = rect
  50.         self.image = pygame.Surface(self.rect.size)
  51.         self.text(text)
  52.  
  53.     def colors(self, text):
  54.         if text:
  55.             bg = self.background = pygame.Color(text)
  56.             if bg.r < 128 and bg.g < 128:
  57.                 self.foreground = pygame.Color('white')
  58.             else:
  59.                 self.foreground = pygame.Color('Black')
  60.         else:
  61.             self.background = pygame.Color('Black')
  62.  
  63.     def text(self, text):
  64.         self.colors(text)
  65.         self.image.fill(self.background)
  66.  
  67.         if text:
  68.             text_image = self.font.render(text, 1, self.foreground)
  69.  
  70.             rect = self.image.get_rect()
  71.             text_rect = text_image.get_rect()
  72.             text_rect.center = rect.center
  73.             self.image.blit(text_image, text_rect)
  74.  
  75. class ColorState:
  76.     def __init__(self):
  77.         # color keys
  78.         self.keys = pygame.color.THECOLORS.keys()
  79.         for color in ("gray", "grey"):
  80.             self.keys = [k for k in self.keys if color not in k]
  81.  
  82.         self.keys.sort()
  83.         self.position = 0
  84.         self.sprites = Group()
  85.         self.buttons = Group()
  86.         self.blocks = Group()
  87.         self.font = pygame.font.Font(None, 24)
  88.  
  89.         for x in range(5):
  90.             for y in range(10):
  91.                 pos = x * 10 + y
  92.                 rect = pygame.Rect(25 + x * 150, 10 + y * 50, 140, 40)
  93.                 block = TextBlock(self.font, self.keys[pos], rect)
  94.                 block.add(self.sprites, self.blocks)
  95.  
  96.         colors = pygame.Color("White"), pygame.Color("Navy"), pygame.Color("Dodgerblue")
  97.         rect = pygame.Rect(405, 550, 100, 40)
  98.         button = SimpleButton(self.font, ">>", rect, self.color_forwards, *colors)
  99.         button.add(self.sprites, self.buttons)
  100.  
  101.         rect = pygame.Rect(295, 550, 100, 40)
  102.         button = SimpleButton(self.font, "<<", rect, self.color_backwards, *colors)
  103.         button.add(self.sprites, self.buttons)
  104.  
  105.     def color_forwards(self, button):
  106.         length = len(self.keys)
  107.         if self.position + 50 < length:
  108.             self.position += 50
  109.  
  110.             for enum, sprite in enumerate(self.blocks):
  111.                 pos = self.position + enum
  112.                 if pos < length:
  113.                     sprite.text(self.keys[pos])
  114.                 else:
  115.                     sprite.text(None)
  116.  
  117.     def color_backwards(self, button):
  118.         if self.position >= 50:
  119.             self.position -= 50
  120.             length = len(self.keys)
  121.            
  122.             for enum, sprite in enumerate(self.blocks):
  123.                 pos = self.position + enum
  124.                 if pos < length:
  125.                     sprite.text(self.keys[pos])
  126.                 else:
  127.                     sprite.text(None)
  128.  
  129.     def draw(self, surface):
  130.         self.sprites.draw(surface)
  131.  
  132.     def event(self, event):
  133.         if event.type == pygame.MOUSEMOTION:
  134.             for sprite in self.buttons:
  135.                 sprite.on_mousemotion(event)
  136.  
  137.         elif event.type == pygame.MOUSEBUTTONUP:
  138.             for sprite in self.buttons:
  139.                 sprite.on_mousebuttonup(event)
  140.  
  141. class ColorMachine:
  142.     def __init__(self):
  143.         # basic pygame setup
  144.         pygame.display.set_caption("Pygame Builtin Colors")
  145.         self.surface = pygame.display.set_mode((800, 600))
  146.         self.rect = self.surface.get_rect()
  147.         self.clock = pygame.time.Clock()
  148.  
  149.         # State
  150.         self.state = ColorState()
  151.  
  152.     def mainloop(self):
  153.         running = True
  154.         while running:
  155.             for event in pygame.event.get():
  156.                 if event.type == pygame.QUIT:
  157.                     running = False
  158.                 else:
  159.                     self.state.event(event)
  160.  
  161.             self.state.draw(self.surface)
  162.             pygame.display.update()
  163.             self.clock.tick(30)
  164.  
  165. def main():
  166.     pygame.init()
  167.     machine = ColorMachine()
  168.     machine.mainloop()
  169.     pygame.quit()
  170.  
  171. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement