dynamic_background.py 1.3 KB

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