dynamic_background.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import random
  2. import pygame
  3. change_speed = 2
  4. class DynamicBackground():
  5. def __init__(self, size):
  6. self.current = get_valid_color()
  7. self.target = get_valid_color()
  8. self.auto_mode = True
  9. self.image_loaded = False
  10. self.size = size
  11. self.surface = pygame.Surface(self.size).convert()
  12. self.target_current_same = False
  13. def draw_background(self):
  14. if self.image_loaded:
  15. return self.surface.copy()
  16. else:
  17. if not self.target_current_same:
  18. for x in range(0, 3):
  19. if abs(self.current[x]-self.target[x]) < change_speed:
  20. self.current[x] = self.target[x]
  21. self.target_current_same = True
  22. else:
  23. self.target_current_same = False
  24. if self.current[x] > self.target[x]:
  25. self.current[x] -= change_speed
  26. elif self.current[x] < self.target[x]:
  27. self.current[x] += change_speed
  28. if self.auto_mode and self.target_current_same:
  29. self.target = get_valid_color()
  30. self.target_current_same = False
  31. self.surface.fill(self.current)
  32. return self.surface.copy()
  33. def set_target_color(self, color, image):
  34. if color is not None:
  35. self.auto_mode = False
  36. self.target_current_same = False
  37. self.target = get_similar_valid_color(color)
  38. else:
  39. self.auto_mode = True
  40. self.target = get_valid_color()
  41. self.target_current_same = False
  42. if image is not None:
  43. image_size = get_aspect_scale_size(image, self.size)
  44. target = pygame.transform.smoothscale(image, image_size)
  45. target.set_alpha(150)
  46. self.image_loaded = True
  47. self.surface.fill((0, 0, 0))
  48. pos = ((self.size[0] - image_size[0])/2,
  49. (self.size[1] - image_size[1])/2)
  50. self.surface.blit(target, pos)
  51. else:
  52. self.image_loaded = False
  53. # It will return the same color if sum is less than 510
  54. # Otherwise a darker color will be returned
  55. # White text should be seen ok with this background color
  56. def get_similar_valid_color(color):
  57. sum = color[0] + color[1] + color[2]
  58. new_color = [0, 0, 0]
  59. if sum > 510:
  60. rest = (sum - 510)/3 + 1
  61. for x in range(0, 3):
  62. new_color[x] = color[x] - rest
  63. return new_color
  64. else:
  65. return color
  66. # Returns an array with 3 integers in range of 0-255
  67. # The sum of the three integers will be lower than 255*2
  68. # (510) to avoid very bright colors
  69. # White text should be seen ok with this background color
  70. def get_valid_color():
  71. color = [0, 0, 0]
  72. total = 0
  73. for i in range(0, 3):
  74. color[i] = random.randint(0, 255)
  75. total += color[i]
  76. extra = total - 510
  77. if extra > 0:
  78. i = random.randint(0, 2)
  79. color[i] -= extra
  80. return color
  81. def get_aspect_scale_size(img, (bx, by)):
  82. size = img.get_size()
  83. aspect_x = bx / float(size[0])
  84. aspect_y = by / float(size[1])
  85. if aspect_x > aspect_y:
  86. aspect = aspect_x
  87. else:
  88. aspect = aspect_y
  89. new_size = (int(aspect*size[0]), int(aspect*size[1]))
  90. return new_size