-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstar.go
70 lines (65 loc) · 1.35 KB
/
star.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
package insta
import (
"image/color"
"math"
"math/rand"
"time"
)
type star struct {
x float64
y float64
dx float64
dy float64
alive bool
hue int
}
func Spaceflight(c Client, duration time.Duration) {
stars := make([]star, 400)
till := time.Now().Add(duration)
for {
scr := NewScreen()
added := 0
alive := 0
for i, s := range stars {
if added < 2 && !s.alive && time.Now().Before(till) {
s = star{
x: ScreenWidth / 2,
y: ScreenHeight / 2,
dx: (rand.Float64()*2 + 0.2) - 1.1,
dy: (rand.Float64()*2 + 0.2) - 1.1,
alive: true,
hue: rand.Intn(360),
}
added += 1
}
if !s.alive {
continue
}
alive += 1
s.x += s.dx
s.y += s.dy
s.dx *= 1.05
s.dy *= 1.05
if s.x < 0 || s.y < 0 || s.x >= ScreenWidth || s.y >= ScreenHeight {
s.alive = false
} else {
dist := math.Hypot((s.x-ScreenWidth/2)/ScreenWidth/2, (s.y-ScreenHeight/2)/ScreenHeight/2)
saturation := dist * 3
if saturation > 1.0 {
saturation = 1.0
}
brightness := 0.1 + dist*3
if brightness > 1.0 {
brightness = 1.0
}
r, g, b := HsvToRgb(float64(s.hue), saturation, brightness)
scr.Set(int(s.x), int(s.y), color.RGBA{uint8(r * 255), uint8(g * 255), uint8(b * 255), 128})
}
stars[i] = s
}
c.SetScreen(scr)
if alive == 0 {
break
}
}
}