This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
/
surprise.go
198 lines (174 loc) · 4.24 KB
/
surprise.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
package main
import (
"math/rand"
"time"
sprite "github.com/pdevine/go-asciisprite"
tm "github.com/pdevine/go-asciisprite/termbox"
)
var allSprites sprite.SpriteGroup
var Width int
var Height int
var random *rand.Rand
const whale_c0 = `xxxxxxxxxxxxxxx##xxxxxxxx.xxxxx
xxxxxxxxx##x##x##xxxxxxx==xxxxx
xxxxxx##x##x##x##xxxxxx===xxxxx
xx/""""""""""""""""\___/x===xxx
x{ /xx===x
xx\______ o __/xxxxxxx
xxxx\ \ __/xxxxxxxxxx
xxxxx\____\______/xxxxxxxxxxxxx`
const birthday_c0 = `
@@@@@@@@@@@(_)@@@@@@@(_)
@@@@@@@@@@@@# @(_)@@@#@@@(_)@@@@@(_)
@@@@@@@@_-.-#=='-#-===#====#=(_)==.#=.==__
@@@@@@.' # # # # # # `+"`"+`.
@@@@@: # # # # :
@@@@@:. # .:
@@@@@| `+"`"+`-.__ __.-' |
@@@@@| `+"`````"+`""============""''''' |
@@@@@| . . .-. .-. .-. . . |
@@@@@| |-| |-| |-' |-' | |
@@@@@| ' ' ' ' ' ' ' |
@@@@@| .-. .-. .-. .-. . . .-. .-. . . . |
@@_.-| |( | |( | |-| | ) |-| | | |-._
.' '. '_' '-' ' ' ' . . '-' ' ' ' . .' `+"`"+`.
: `+"`"+`-.__ __.-' :
~. `+"`````"+`""""""""""""""""''''' .'
@@@`+"`"+`-.._ _..-'
@@@@@@@@`+"`````"+`""""-----_____------""""'''''`
type Cake struct {
sprite.BaseSprite
Timer int
TimeOut int
}
type Confetti struct {
sprite.BaseSprite
Timer int
TimeOut int
VY int
VYTimer int
VYTimeOut int
}
type Flicker struct {
sprite.BaseSprite
Timer int
}
func NewCake() *Cake {
s := &Cake{BaseSprite: sprite.BaseSprite{
Visible: true,
Y: Height/2},
}
s.AddCostume(sprite.NewCostume(birthday_c0, '@'))
s.X = Width/2 - s.Width/2
points := []sprite.Point{
sprite.Point{12, 0},
sprite.Point{22, 0},
sprite.Point{17, 1},
sprite.Point{27, 1},
sprite.Point{35, 1},
sprite.Point{30, 2},
}
for cnt, p := range points {
f := &Flicker{BaseSprite: sprite.BaseSprite{
Visible: true,
X: s.X+p.X,
Y: s.Y+p.Y,
},
}
f.AddCostume(sprite.NewCostume(")", '@'))
f.AddCostume(sprite.NewCostume("(", '@'))
f.CurrentCostume = cnt%2
allSprites.Sprites = append(allSprites.Sprites, f)
}
return s
}
func (f *Flicker) Update() {
f.Timer = f.Timer + 1
if f.Timer > 4 {
if f.CurrentCostume == 0 {
f.CurrentCostume = 1
} else {
f.CurrentCostume = 0
}
f.Timer = 0
}
}
func NewConfetti() *Confetti {
s := &Confetti{BaseSprite: sprite.BaseSprite{
Visible: true},
Timer: 0,
TimeOut: 3}
s.AddCostume(sprite.NewCostume(whale_c0, 'x'))
s.AddCostume(sprite.NewCostume(whale_c0, 'x'))
s.X = random.Intn(Width)
s.Y = -random.Intn(Height)
s.VY = random.Intn(2) + 1
s.VYTimer = 0
s.VYTimeOut = 2
return s
}
func (s *Confetti) Update() {
s.Timer = s.Timer + 1
s.VYTimer = s.VYTimer + 1
if s.Timer > 2 {
if s.CurrentCostume == 0 {
s.CurrentCostume = 1
} else {
s.CurrentCostume = 0
}
s.Timer = 0
}
if s.VYTimer > s.VYTimeOut {
s.Y = s.Y + s.VY
if s.Y > Height {
s.Y = 0 - s.Height
}
s.VYTimer = 0
}
}
func main() {
// XXX - Wait a bit until the terminal is properly initialized
time.Sleep(1000 * time.Millisecond)
random = rand.New(rand.NewSource(time.Now().UnixNano()))
err := tm.Init()
if err != nil {
panic(err)
}
defer tm.Close()
Width, Height = tm.Size()
event_queue := make(chan tm.Event)
go func() {
for {
event_queue <- tm.PollEvent()
}
}()
for n := 0; n < 40; n++ {
c := NewConfetti()
allSprites.Sprites = append(allSprites.Sprites, c)
}
cake := NewCake()
allSprites.Sprites = append(allSprites.Sprites, cake)
txt := "Press 'ESC' to quit."
c := sprite.NewCostume(txt, '~')
text := sprite.NewBaseSprite(Width/2-len(txt)/2, Height-2, c)
allSprites.Sprites = append(allSprites.Sprites, text)
mainloop:
for {
tm.Clear(tm.ColorDefault, tm.ColorDefault)
select {
case ev := <-event_queue:
if ev.Type == tm.EventKey {
if ev.Key == tm.KeyCtrlC || ev.Key == tm.KeyEsc || ev.Ch == 'q' {
break mainloop
}
} else if ev.Type == tm.EventResize {
Width = ev.Width
Height = ev.Height
}
default:
allSprites.Update()
allSprites.Render()
time.Sleep(30 * time.Millisecond)
}
}
}