-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounce.go
76 lines (59 loc) · 1.11 KB
/
bounce.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
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
const td float64 = 0.05
const ballCount int = 5
const maxH float64 = 50
const g float64 = 9.8
const a float64 = -g * td
type ball struct {
v float64
h float64
c rune
}
var balls = make([]ball, ballCount)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
var maxV float64 = math.Sqrt(maxH * 2.0 * g)
var runes = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
for i := 0; i < ballCount; i++ {
balls[i] = ball{
v: maxV * float64(rand.Intn(51)+50) / 100,
h: 0,
c: runes[rand.Intn(len(runes))],
}
}
for {
for i, ball := range balls {
var v = ball.v
var h = ball.h
h = math.Max(
0,
(h + (v * td) + (0.5 * a * td * td)),
)
if h == 0 {
v = -0.75 * v
}
v += a
if int(v) == 0 && int(h) == 0 {
v = maxV * float64(rand.Intn(11)+90) / 100
}
balls[i].v = v
balls[i].h = h
}
fmt.Print("\033[H\033[2J")
for h := int(maxH); h > 0; h-- {
for _, ball := range balls {
if int(ball.h) == h {
fmt.Print(string(ball.c) + " ")
}
}
fmt.Println()
}
time.Sleep(time.Second / 50)
}
}