dynamic_background.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import random
  2. import pygame
  3. class DynamicBackground():
  4. def __init__(self, size):
  5. self.size = size
  6. self.current = get_valid_color()
  7. self.target = get_valid_color()
  8. self.surface = None
  9. def draw_background(self, surface):
  10. same = True
  11. for x in range(0, 3):
  12. if self.current[x] > self.target[x]:
  13. self.current[x] -= 1
  14. elif self.current[x] < self.target[x]:
  15. self.current[x] += 1
  16. if self.current != self.target:
  17. same = False
  18. #if same:
  19. # self.target = get_valid_color()
  20. surface.fill(self.current)
  21. if self.surface is not None:
  22. surface.blit(self.surface,(0,0))
  23. def set_target_color(self, color, surface):
  24. self.target = [color[0], color[1], color[2]]
  25. #irudi = pygame.Surface(surface.get_size())
  26. #pygame.transform.laplacian(surface, irudi)
  27. irudi = surface
  28. target = pygame.Surface(self.size)
  29. pygame.transform.smoothscale(irudi, self.size, target)
  30. target.set_alpha(80)
  31. self.surface = target
  32. # Returns an array with 3 integers in range of 0-255
  33. # The sum of the three integers will be lower than 255*2
  34. # (510) to avoid very bright colors
  35. # White text should be seen ok with this background color
  36. def get_valid_color():
  37. color = [0, 0, 0]
  38. total = 0
  39. for i in range(0, 3):
  40. color[i] = random.randint(0, 255)
  41. total += color[i]
  42. extra = total - 510
  43. if extra > 0:
  44. i = random.randint(0, 2)
  45. color[i] -= extra
  46. return color