dynamic_background.py 1.0 KB

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