Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.sprite import Group, Sprite
- class SimpleButton(Sprite):
- def __init__(self, font, text, rect, callback, foreground, background, midground):
- Sprite.__init__(self)
- self.font = font
- self.rect = rect
- self.hover = False
- self.callback = callback
- self.midground = midground
- self.foreground = foreground
- self.background = background
- self.image = pygame.Surface(self.rect.size)
- self.text(text)
- def text(self, text):
- self.text_image = self.font.render(text, 1, self.foreground)
- self.update_image()
- def on_mousemotion(self, event):
- hover = self.rect.collidepoint(event.pos)
- if self.hover != hover:
- self.hover = hover
- self.update_image()
- def on_mousebuttonup(self, event):
- if event.button == 1:
- if self.hover:
- self.callback(self)
- def update_image(self):
- if self.hover:
- self.image.fill(self.midground)
- else:
- self.image.fill(self.background)
- rect = self.image.get_rect()
- text_rect = self.text_image.get_rect()
- text_rect.center = rect.center
- self.image.blit(self.text_image, text_rect)
- class TextBlock(Sprite):
- def __init__(self, font, text, rect):
- Sprite.__init__(self)
- self.font = font
- self.rect = rect
- self.image = pygame.Surface(self.rect.size)
- self.text(text)
- def colors(self, text):
- if text:
- bg = self.background = pygame.Color(text)
- if bg.r < 128 and bg.g < 128:
- self.foreground = pygame.Color('white')
- else:
- self.foreground = pygame.Color('Black')
- else:
- self.background = pygame.Color('Black')
- def text(self, text):
- self.colors(text)
- self.image.fill(self.background)
- if text:
- text_image = self.font.render(text, 1, self.foreground)
- rect = self.image.get_rect()
- text_rect = text_image.get_rect()
- text_rect.center = rect.center
- self.image.blit(text_image, text_rect)
- class ColorState:
- def __init__(self):
- # color keys
- self.keys = pygame.color.THECOLORS.keys()
- for color in ("gray", "grey"):
- self.keys = [k for k in self.keys if color not in k]
- self.keys.sort()
- self.position = 0
- self.sprites = Group()
- self.buttons = Group()
- self.blocks = Group()
- self.font = pygame.font.Font(None, 24)
- for x in range(5):
- for y in range(10):
- pos = x * 10 + y
- rect = pygame.Rect(25 + x * 150, 10 + y * 50, 140, 40)
- block = TextBlock(self.font, self.keys[pos], rect)
- block.add(self.sprites, self.blocks)
- colors = pygame.Color("White"), pygame.Color("Navy"), pygame.Color("Dodgerblue")
- rect = pygame.Rect(405, 550, 100, 40)
- button = SimpleButton(self.font, ">>", rect, self.color_forwards, *colors)
- button.add(self.sprites, self.buttons)
- rect = pygame.Rect(295, 550, 100, 40)
- button = SimpleButton(self.font, "<<", rect, self.color_backwards, *colors)
- button.add(self.sprites, self.buttons)
- def color_forwards(self, button):
- length = len(self.keys)
- if self.position + 50 < length:
- self.position += 50
- for enum, sprite in enumerate(self.blocks):
- pos = self.position + enum
- if pos < length:
- sprite.text(self.keys[pos])
- else:
- sprite.text(None)
- def color_backwards(self, button):
- if self.position >= 50:
- self.position -= 50
- length = len(self.keys)
- for enum, sprite in enumerate(self.blocks):
- pos = self.position + enum
- if pos < length:
- sprite.text(self.keys[pos])
- else:
- sprite.text(None)
- def draw(self, surface):
- self.sprites.draw(surface)
- def event(self, event):
- if event.type == pygame.MOUSEMOTION:
- for sprite in self.buttons:
- sprite.on_mousemotion(event)
- elif event.type == pygame.MOUSEBUTTONUP:
- for sprite in self.buttons:
- sprite.on_mousebuttonup(event)
- class ColorMachine:
- def __init__(self):
- # basic pygame setup
- pygame.display.set_caption("Pygame Builtin Colors")
- self.surface = pygame.display.set_mode((800, 600))
- self.rect = self.surface.get_rect()
- self.clock = pygame.time.Clock()
- # State
- self.state = ColorState()
- def mainloop(self):
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- else:
- self.state.event(event)
- self.state.draw(self.surface)
- pygame.display.update()
- self.clock.tick(30)
- def main():
- pygame.init()
- machine = ColorMachine()
- machine.mainloop()
- pygame.quit()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement