Animationen für die Ceiling Pixel Lamp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.7 KiB

  1. #!/usr/bin/env python3
  2. import sys
  3. import os
  4. import numpy as np
  5. import pygame
  6. Nx = int(sys.argv[1])
  7. Ny = int(sys.argv[2])
  8. try:
  9. frequency = float(sys.argv[3])
  10. except (IndexError, ValueError):
  11. frequency = 5
  12. fps = 10
  13. nstates = 5
  14. colors = np.linspace(0, 255, nstates, dtype=np.uint8)
  15. red = np.random.choice(colors, size=(Ny,Nx))
  16. green = np.zeros((Ny, Nx))
  17. blue = np.zeros((Ny, Nx))
  18. grid = np.dstack((red, green, blue)).astype(np.uint8)
  19. clock = pygame.time.Clock()
  20. def clamp(value, min, max):
  21. if value > max:
  22. return max
  23. elif value < min:
  24. return min
  25. return value
  26. while True:
  27. for px in range(Nx):
  28. for py in range(Ny):
  29. statecount = np.zeros(nstates, dtype=np.uint8)
  30. for ix in [-1, 0, 1]:
  31. for iy in [-1, 0, 1]:
  32. if (px+ix) >= Nx or (px+ix) <= 0 or (py+iy) >= Ny or (py+iy) <= 0:
  33. i = 0
  34. else:
  35. i = int(grid[py + iy, px + ix, 0] / colors[1])
  36. #i = int(grid[(px + ix) % Nx, (py + iy) % Ny, 0] / colors[1])
  37. #i = int(grid[max(min(px + ix, Nx-1), 0), max(min(py + iy, Ny-1), 0), 0] / colors[1])
  38. #print(px, py, max(min(px + ix, Nx-1), 0), max(min(py + iy, Ny-1), 0), file=sys.stderr)
  39. statecount[i] = statecount[i] + 1
  40. i = int(grid[py, px, 0] / colors[1])
  41. i_successor = (i+1) % colors.size
  42. if statecount[i_successor] >= 2: #(2-np.random.randint(0, 2)):
  43. grid[py, px, 0] = colors[i_successor]
  44. # for i in range(Nx*Ny):
  45. # c1 = clamp(i % Nx - 1, 0, Nx - 1)
  46. # c2 = clamp(int(i / Nx) - 1, 0, Ny - 1)
  47. # neighbourhood = grid[c1:(c1+3), c2:(c2+3), 0]
  48. # #print(neighbourhood.size, file=sys.stderr)
  49. out = grid.tobytes()
  50. os.write(1, out)
  51. clock.tick_busy_loop(fps)