-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpipedream.lua
More file actions
173 lines (151 loc) · 3.77 KB
/
pipedream.lua
File metadata and controls
173 lines (151 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
local event = require("event")
local term = require("term")
local gpu = require("component").gpu
local width, height = gpu.getResolution()
height = height * 2
local minLen, maxLen = 100, 200
local pipes = {}
local sides = {
top = 1,
left = 2,
bottom = 3,
right = 4,
"top",
"left",
"bottom",
"right"
}
local char = "▀"
local turn = 5
local oldFg, oldBg = gpu.getForeground(), gpu.getBackground()
gpu.fill(1, 1, width, height / 2, " ")
local spawnInterval = 500
local lifeTime = 600
local clearInterval = 2500
local i = 0
while true do
if i % spawnInterval == 0 then
local x, y
local side = math.random(1, 4) -- choose one of 4 sides
local direction
if side == sides.top then
x, y = math.random(1, width), 1
direction = sides.bottom
elseif side == sides.left then
x, y = 1, math.random(1, height)
direction = sides.right
elseif side == sides.bottom then
x, y = math.random(1, width), height
direction = sides.top
elseif side == sides.right then
x, y = width, math.random(1, height)
direction = sides.left
end
local length = math.random(minLen, maxLen)
local r, g, b = 0, 0, 0
while r < 127 and g < 127 and b < 127 do
r, g, b = math.random(0, 255), math.random(0, 255), math.random(0, 255)
end
local color = r * 0x10000 + g * 0x100 + b
local pipe = {
x = x,
y = y,
length = length,
dir = direction,
color = color,
life = lifeTime
}
table.insert(pipes, pipe)
end
for n = #pipes, 1, -1 do
local pipe = pipes[n]
if pipe.life == 0 then
table.remove(pipes, n)
else
local c, fg, bg = gpu.get(pipe.x, 1 + math.floor((pipe.y - 1) / 2))
if c ~= char then
fg = oldBg
bg = oldBg
end
local part = (pipe.y - 1) % 2
local sBg, sFG
if part == 0 then
sFg = pipe.color
sBg = bg
else
sFg = fg
sBg = pipe.color
end
if gpu.getForeground() ~= sFg then
gpu.setForeground(sFg)
end
if gpu.getBackground() ~= sBg then
gpu.setBackground(sBg)
end
gpu.set(pipe.x, 1 + math.floor((pipe.y - 1) / 2), char)
local dir = pipe.dir
local shouldTurn = math.random(1, 100) <= turn
local remains = {
pipe.y - 2,
pipe.x - 2,
height - pipe.y - 1,
width - pipe.x - 1
}
if remains[dir] == 0 then
shouldTurn = true
end
if shouldTurn then
local options = {dir - 1, dir + 1}
if options[1] == 0 then
options[1] = 4
end
if options[1] == 5 then
options[1] = 1
end
if options[2] == 0 then
options[2] = 4
end
if options[2] == 5 then
options[2] = 1
end
if remains[options[2]] < 1 then
table.remove(options, 2)
end
if remains[options[1]] < 1 then
table.remove(options, 1)
end
dir = options[math.random(1, #options)]
end
pipe.dir = dir
if pipe.dir == sides.top then
pipe.y = pipe.y - 1
elseif pipe.dir == sides.left then
pipe.x = pipe.x - 1
elseif pipe.dir == sides.right then
pipe.x = pipe.x + 1
elseif pipe.dir == sides.bottom then
pipe.y = pipe.y + 1
end
pipe.life = pipe.life - 1
end
end
i = i + 1
if i >= clearInterval then
if event.pull(2, "interrupted") then
break
end
gpu.setBackground(0x000000)
gpu.setForeground(0xffffff)
gpu.fill(1, 1, width, height / 2, " ")
i = 0
pipes = {}
else
if event.pull(.05, "interrupted") then
break
end
end
end
gpu.setForeground(oldFg)
gpu.setBackground(oldBg)
gpu.fill(1, 1, width, height / 2, " ")
term.setCursor(1, 1)