forked from ak-tr/go-rain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (148 loc) · 3.72 KB
/
main.go
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
174
175
176
177
178
179
180
181
182
/*
.::.
.:' .:
,MMM8&&&.:' .:'
MMMMM88&&&& .:'
MMMMM88&&&&&&:'
MMMMM88&&&&&&
.:MMMMM88&&&&&&
.:' MMMMM88&&&&
.:' .:'MMM8&&&'
:' .:'
'::' ak-tr
Rain in your terminal...
that's it.
*/
package main
import (
"math"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
tm "github.com/buger/goterm"
)
const (
HEAVY string = "|"
MEDIUM string = ":"
LIGHT string = "."
)
// Types
type Drop struct {
char string
speed, x, y int
}
func (d *Drop) fall() {
d.y += d.speed
}
type Drops []Drop
func (ds Drops) add(d Drop) Drops {
ds = append(ds, d)
return ds
}
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
time.Sleep(time.Millisecond * 250)
tm.Clear() // Clear screen
tm.MoveCursor(1, 1) // Set cursor pos
tm.Println("\033[?25h\x1b[0;4mhttps://github.com/ak-tr\x1b[0m") // Show cursor
tm.Flush() // Mandatory flush
os.Exit(0) // Exit
}()
// Print escape code to hide cursor
tm.Printf("\033[?25l")
// Set seed for rand
rand.Seed(time.Now().UnixNano())
// Get height and width of terminal
height := tm.Height()
width := tm.Width()
// Create array of drops
var drops Drops
var dropTypes = []string{HEAVY, MEDIUM, LIGHT}
for { // Infinite loop
// Clear screen on each loop
tm.Clear()
// Reset temp value on each loop
tmp := drops[:0]
// Capture resize
if height != tm.Height() || width != tm.Width() {
// Update height and width to new height
height = tm.Height()
width = tm.Width()
// Loop through all drops...
for idx := range drops {
drop := drops[idx]
// ...and ignore if off screen
if drop.x >= width || drop.y >= height {
continue
}
// Add to tmp slice
tmp = append(tmp, Drop{drop.char, drop.speed, drop.x, drop.y})
}
drops = tmp
}
// For each drop
for idx := range drops {
// Get reference to drop and fall
drop := &drops[idx]
drop.fall()
// If y value of drop is more than height, skip loop
if drop.y > height {
continue
}
// Append to tmp array only if previous gaurd clause is false
tmp = append(tmp, Drop{drop.char, drop.speed, drop.x, drop.y})
// Move cursor to location and print character to screen
tm.MoveCursor(drop.x, drop.y)
if drop.char != HEAVY {
tm.Printf("\x1b[90;1m%s", drop.char)
continue
}
tm.Printf("\x1b[37;1m%s", drop.char)
}
drops = tmp
// Generate new drops at the end of each loop
for _, dropType := range dropTypes {
for i := 0; i < getDropsPerLine(width); i++ {
j := generateRandomNumber(0, width)
drops = drops.add(Drop{dropType, getSpeed(dropType), j, 0})
}
}
// Flush required
tm.Flush()
// 30 frames per second...
time.Sleep(time.Second / 30)
}
}
// Get the speed of the drop based on the drop type
// Heavy drops fall by 1 cell per loop
// Light drops fall by 3 cells per loop
func getSpeed(dropType string) int {
switch dropType {
case HEAVY:
return generateRandomNumber(1, 2)
case MEDIUM:
return generateRandomNumber(2, 3)
case LIGHT:
return generateRandomNumber(3, 4)
default:
return 1
}
}
// Get width of screen, divide by 100 and then round up
// If value is more than 1, return random number between 1 and 2
// otherwise return 1
func getDropsPerLine(w int) int {
if int(math.Ceil(float64(w)/100)) > 1 {
return generateRandomNumber(1, 2)
}
return 1
}
// Generate c random numbers of range min to max
func generateRandomNumber(min, max int) int {
return rand.Intn(max-min+1) + min
}