yeeeeeeeeeeeee

uhhh fish

Nov 5th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. -- Dynamic Game of Life with fade-in/fade-out
  2. math.randomseed(os.time())
  3.  
  4. local width, height = 20, 10
  5. local fadeSpeed = 0.2 -- how fast cells fade
  6. local symbols = {"o", ".", "0", "O"} -- from dead to alive
  7. local maxRandomInfluence = 0.3
  8.  
  9. -- Initialize grid with intensity 0..1
  10. local grid = {}
  11. for y = 1, height do
  12. grid[y] = {}
  13. for x = 1, width do
  14. grid[y][x] = math.random()
  15. end
  16. end
  17.  
  18. -- Count alive neighbors (intensity > 0.5 counts as alive)
  19. local function countNeighbors(x, y)
  20. local count = 0
  21. for dy = -1,1 do
  22. for dx = -1,1 do
  23. if not (dx == 0 and dy == 0) then
  24. local nx, ny = x + dx, y + dy
  25. if nx >= 1 and nx <= width and ny >= 1 and ny <= height then
  26. if grid[ny][nx] > 0.5 then
  27. count = count + 1
  28. end
  29. end
  30. end
  31. end
  32. end
  33. return count
  34. end
  35.  
  36. -- Update grid with neighbor influence and random flips
  37. local function updateGrid()
  38. local newGrid = {}
  39. for y = 1, height do
  40. newGrid[y] = {}
  41. for x = 1, width do
  42. local neighbors = countNeighbors(x, y)
  43. local current = grid[y][x]
  44. local target
  45.  
  46. -- Neighbor-influenced target intensity
  47. local neighborInfluence = neighbors / 8
  48. local randomChance = math.random()
  49.  
  50. if randomChance < neighborInfluence then
  51. target = 1 -- move toward alive
  52. elseif randomChance < neighborInfluence + maxRandomInfluence then
  53. target = 1 - current -- small random flip
  54. else
  55. target = 0 -- move toward dead
  56. end
  57.  
  58. -- Fade intensity toward target
  59. if current < target then
  60. current = math.min(current + fadeSpeed, target)
  61. elseif current > target then
  62. current = math.max(current - fadeSpeed, target)
  63. end
  64.  
  65. newGrid[y][x] = current
  66. end
  67. end
  68. grid = newGrid
  69. end
  70.  
  71. -- Map intensity to symbol
  72. local function intensityToSymbol(intensity)
  73. local index = math.floor(intensity * #symbols) + 1
  74. if index > #symbols then index = #symbols end
  75. return symbols[index]
  76. end
  77.  
  78. -- Draw grid
  79. local function drawGrid()
  80. term.clear()
  81. term.setCursorPos(1,1)
  82. for y = 1, height do
  83. local line = ""
  84. for x = 1, width do
  85. line = line .. intensityToSymbol(grid[y][x])
  86. end
  87. print(line)
  88. end
  89. end
  90.  
  91. -- Main loop
  92. while true do
  93. drawGrid()
  94. updateGrid()
  95. sleep(0.2)
  96. end
  97.  
Advertisement
Add Comment
Please, Sign In to add comment