crumble.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import random
  2. import time
  3. class Crumbler:
  4. CRUMBLERS = []
  5. MAX = 30
  6. @classmethod
  7. def cleanup (cls):
  8. for c in cls.CRUMBLERS[cls.MAX:]:
  9. cls.CRUMBLERS.remove(c)
  10. del(c)
  11. @classmethod
  12. def init(cls):
  13. cls.CRUMBLERS=[Crumbler(500,350)]
  14. @classmethod
  15. def loop(cls):
  16. while True:
  17. for c in cls.CRUMBLERS:
  18. c.step()
  19. def __init__(self,x,y,r=None,g=None,b=None):
  20. self.x = x
  21. self.y = y
  22. self.a_x = random.randint(-1,1)
  23. self.a_y = random.randint(-1,1)
  24. self.steps = 0
  25. if r is None and g is None and b is None:
  26. self.r = random.randint(0,255)
  27. self.g = random.randint(0,255)
  28. self.b = random.randint(0,255)
  29. else:
  30. self.r = r
  31. self.g = g
  32. self.b = b
  33. if len(Crumbler.CRUMBLERS) >= Crumbler.MAX:
  34. Crumbler.cleanup()
  35. Crumbler.CRUMBLERS.append(self)
  36. def out_of_bounds(self):
  37. return self.x < 0 or self.y < 0 or self.x > 1024 or self.y > 768
  38. def step(self):
  39. self.x += self.a_x
  40. self.y += self.a_y
  41. self.steps += 1
  42. if self.out_of_bounds():
  43. Crumbler.CRUMBLERS.remove(self)
  44. del(self)
  45. return
  46. print "px %d %d %02x%02x%02x"%(self.x,self.y,self.r,self.g,self.b)
  47. if self.steps%7==0 or random.randint(0,5) == 3:
  48. x = random.randint(-20,20)
  49. if x%3 == 0:
  50. self.r += x
  51. self.r = min(self.r, 255)
  52. self.r = max(self.r, 0)
  53. elif x%3 == 1:
  54. self.g += x
  55. self.g = min(self.g, 255)
  56. self.g = max(self.g, 0)
  57. elif x%3 == 2:
  58. self.b += x
  59. self.b = min(self.b, 255)
  60. self.b = max(self.b, 0)
  61. self.a_x = random.randint(-1,1)
  62. self.a_y = random.randint(-1,1)
  63. if random.randint(0,2) == 2:
  64. Crumbler(self.x,self.y, self.r, self.g, self.b)
  65. random.seed(time.time()**20)
  66. Crumbler.init()
  67. Crumbler.loop()