dynamic_background.py 1.9 KB

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