collision.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Two projectiles are sent from random positions and collide with each other
  2. # Template from https://github.com/nickpesce/lit/blob/master/lit/effects/collision.py
  3. import hyperion, time, colorsys, random
  4. # Get parameters
  5. sleepTime = max(0.02, float(hyperion.args.get('speed', 100))/1000.0)
  6. trailLength = max(3, int(hyperion.args.get('trailLength', 5)))
  7. explodeRadius = int(hyperion.args.get('explodeRadius', 8))
  8. # Create additional variables
  9. increment = None
  10. projectiles = []
  11. # Initialize the led data
  12. ledData = bytearray()
  13. for i in range(hyperion.ledCount):
  14. ledData += bytearray((0,0,0))
  15. # Start the write data loop
  16. while not hyperion.abort():
  17. if (len(projectiles) != 2):
  18. projectiles = [ [0, 1, random.uniform(0.0, 1.0)], [hyperion.ledCount-1, -1, random.uniform(0.0, 1.0)] ]
  19. increment = -random.randint(0, hyperion.ledCount-1) if random.choice([True, False]) else random.randint(0, hyperion.ledCount-1)
  20. ledDataBuf = ledData[:]
  21. for i, v in enumerate(projectiles):
  22. projectiles[i][0] = projectiles[i][0]+projectiles[i][1]
  23. for t in range(0, trailLength):
  24. pixel = v[0] - v[1]*t
  25. if pixel + 2 < 0:
  26. pixel += hyperion.ledCount
  27. if pixel + 2 > hyperion.ledCount-1:
  28. pixel -= hyperion.ledCount-1
  29. rgb = colorsys.hsv_to_rgb(v[2], 1, (trailLength - 1.0*t)/trailLength)
  30. ledDataBuf[3*pixel ] = int(255*rgb[0])
  31. ledDataBuf[3*pixel + 1] = int(255*rgb[1])
  32. ledDataBuf[3*pixel + 2] = int(255*rgb[2])
  33. hyperion.setColor(ledDataBuf[-increment:] + ledDataBuf[:-increment])
  34. for i1, p1 in enumerate(projectiles):
  35. for i2, p2 in enumerate(projectiles):
  36. if (p1 is not p2):
  37. prev1 = p1[0] - p1[1]
  38. prev2 = p2[0] - p2[1]
  39. if (prev1 - prev2 < 0) != (p1[0] - p2[0] < 0):
  40. for d in range(0, explodeRadius):
  41. for pixel in range(p1[0] - d, p1[0] + d):
  42. rgb = colorsys.hsv_to_rgb(random.choice([p1[2], p2[2]]), 1, (1.0 * explodeRadius - d) / explodeRadius)
  43. ledDataBuf[3*pixel ] = int(255*rgb[0])
  44. ledDataBuf[3*pixel + 1] = int(255*rgb[1])
  45. ledDataBuf[3*pixel + 2] = int(255*rgb[2])
  46. hyperion.setColor(ledDataBuf[-increment:] + ledDataBuf[:-increment])
  47. time.sleep(sleepTime)
  48. projectiles.remove(p1)
  49. projectiles.remove(p2)
  50. time.sleep(sleepTime)