-
Notifications
You must be signed in to change notification settings - Fork 0
/
stars
37 lines (31 loc) · 744 Bytes
/
stars
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
#!/usr/bin/env ruby
COLS = `tput cols`.to_i
ROWS = `tput lines`.to_i
DEPTH = [COLS, ROWS].max
MAX = (COLS * ROWS * 0.05).round
SIZES = %w(. * 💩 ).freeze
def main
stars = Array.new(MAX) { |_i| Star.new }
loop do
print "\e[s\e[2J#{stars.map(&:animate).join}\e[u"
sleep 0.1
end
end
class Star
def reset
@x = rand(COLS * 2) - COLS / 2
@y = rand(ROWS * 2) - ROWS / 2
@z = 1
@zs = rand(10) + 3
end
alias initialize reset
def animate
@z += @zs
d = (@z == DEPTH ? 1 : DEPTH.to_f / (DEPTH - @z))
x2 = (@x * d + COLS / 2).round
y2 = (@y * d + ROWS / 2).round
ch = SIZES[@z.to_f / DEPTH * SIZES.size]
(x2 < COLS && y2 < ROWS) && "\e[#{y2};#{x2}H#{ch}" || (reset && '')
end
end
main