-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
211 lines (178 loc) · 4.08 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"fmt"
"math"
"math/rand"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"golang.org/x/image/colornames"
"golang.org/x/image/font/basicfont"
)
var maxX float64
var maxY float64
var accel = 1.03
func wait(in chan *imdraw.IMDraw, out chan *imdraw.IMDraw, jitter int) {
startTime := time.Now()
initialPause := time.Duration(rand.Intn(jitter)) * time.Millisecond
for time.Since(startTime) < initialPause {
imd := <-in
out <- imd
}
}
func randColor(alpha float64) pixel.RGBA {
return pixel.RGB(rand.Float64(), rand.Float64(), rand.Float64()).Mul(pixel.Alpha(alpha))
}
func roll(min, max int) float64 {
return float64(rand.Intn(max-min) + min)
}
// Drop represents a single drop on screen
type Drop struct {
height float64
width float64
x float64
y float64
top pixel.RGBA
bot pixel.RGBA
velocity float64
}
// Fall moves a Drop downards and adjusts velocity
func (drop *Drop) Fall(accel float64) {
drop.y -= drop.velocity
drop.velocity *= accel
}
// Draw pushes pixels to the IMDraw object
func (drop *Drop) Draw(imd *imdraw.IMDraw) *imdraw.IMDraw {
imd.Color = drop.top
imd.Push(pixel.V(drop.x, drop.y))
imd.Color = drop.bot
imd.Push(pixel.V(drop.x, drop.y+drop.height))
imd.Line(drop.width)
return imd
}
// NewDrop constructs a new Drop
func NewDrop(maxX, maxY float64) (drop *Drop) {
drop = &Drop{
height: roll(10, 70),
width: roll(1, 4),
x: float64(rand.Intn(int(maxX))),
top: randColor(0.9),
bot: randColor(0.1),
velocity: roll(2, 5),
}
drop.y = maxY - drop.height
return
}
func rundrop(in, out chan *imdraw.IMDraw) {
drop := NewDrop(maxX, maxY)
wait(in, out, 500)
for imd := range in {
out <- drop.Draw(imd)
if drop.y <= 0 {
drop = NewDrop(maxX, maxY)
wait(in, out, 500)
} else {
drop.Fall(accel)
}
}
}
func addDrop(chans map[chan *imdraw.IMDraw]chan *imdraw.IMDraw) int {
in := make(chan *imdraw.IMDraw)
out := make(chan *imdraw.IMDraw)
chans[in] = out
go rundrop(in, out)
return 1
}
func getTimes(win *pixelgl.Window) int {
switch {
case win.Pressed(pixelgl.KeyLeftControl), win.Pressed(pixelgl.KeyRightControl):
return 100
case win.Pressed(pixelgl.KeyLeftShift), win.Pressed(pixelgl.KeyRightShift):
return 10
default:
return 1
}
}
func run() {
monX, monY := pixelgl.PrimaryMonitor().Size()
maxX = monX
maxY = monY
paused := false
chans := make(map[chan *imdraw.IMDraw]chan *imdraw.IMDraw)
atlas := text.NewAtlas(basicfont.Face7x13, text.ASCII)
basicText := text.New(pixel.V(0, 0), atlas)
numDrops := 0
alpha := 1.0
cfg := pixelgl.WindowConfig{
Title: "Rain-bow",
Bounds: pixel.R(0, 0, maxX, maxY),
VSync: true,
Monitor: pixelgl.PrimaryMonitor(),
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
numDrops += addDrop(chans)
for !win.Closed() {
if !paused {
imd := imdraw.New(nil)
for in, out := range chans {
in <- imd
imd = <-out
}
win.Clear(colornames.Black)
basicText.Clear()
alpha = math.Max(0.0, alpha-0.004)
basicText.Color = pixel.Alpha(alpha)
fmt.Fprintf(basicText, "%d %.2f", numDrops, accel)
basicText.Draw(win, pixel.IM.Scaled(basicText.Orig, 3))
imd.Draw(win)
}
if win.JustPressed(pixelgl.KeyQ) {
return
}
if win.JustPressed(pixelgl.KeySpace) {
paused = !paused
}
if win.JustPressed(pixelgl.KeyUp) {
for i := 0; i < getTimes(win); i++ {
numDrops += addDrop(chans)
}
alpha = 1.0
}
if win.JustPressed(pixelgl.KeyDown) {
for i := 0; i < getTimes(win); i++ {
for k := range chans {
close(k)
delete(chans, k)
break
}
numDrops = int(math.Max(0, float64(numDrops-1)))
}
alpha = 1.0
}
if win.JustPressed(pixelgl.KeyRight) {
accel += 0.01
alpha = 1.0
}
if win.JustPressed(pixelgl.KeyLeft) {
accel -= 0.01
alpha = 1.0
}
if win.JustPressed(pixelgl.KeyR) {
for k := range chans {
close(k)
delete(chans, k)
}
numDrops = 0
alpha = 1.0
}
win.Update()
}
}
func main() {
pixelgl.Run(run)
}