dynamic_background.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 (510) to avoid very bright colors
  20. #White text should be seen ok with this background color
  21. def get_valid_color():
  22. color = [0, 0, 0]
  23. total = 0
  24. for i in range(0, 3):
  25. color[i] = random.randint(0, 255)
  26. total += color[i]
  27. extra = total - 510
  28. if extra > 0:
  29. i = random.randint(0, 2)
  30. color[i] -= - extra
  31. return color