-
Notifications
You must be signed in to change notification settings - Fork 2
/
starfield.monkey
68 lines (57 loc) · 1.72 KB
/
starfield.monkey
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
Import mojo
Import horizon.application
Class StarField
Field numStars%
Field maxDepth%
Field stars#[]
Field starX#[]
Field starY#[]
Field starSpeed#
Method New()
numStars = 512
maxDepth = 32
starSpeed = 0.19
InitStars()
End
Method New(numStars%, maxDepth%, starSpeed# = 0.19)
Self.numStars = numStars
Self.maxDepth = maxDepth
Self.starSpeed = starSpeed
InitStars()
End
Method InitStars:Void()
stars = stars.Resize(numStars * 3)
starX = starX.Resize(numStars)
starY = starX.Resize(numStars)
For Local i := 0 To stars.Length()-1 Step 3
stars[i] = Rnd(-25, 25)
stars[i+1] = Rnd(-25, 25)
stars[i+2] = Rnd(1, maxDepth)
Next
End
Method Update:Void()
Local ox := Application.GetInstance().width / 2
Local oy := Application.GetInstance().height / 2
For Local i := 0 To stars.Length()-1 Step 3
stars[i+2] = stars[i+2] - starSpeed
If stars[i+2] <= 0
stars[i] = Rnd(-25, 25)
stars[i+1] = Rnd(-25, 25)
stars[i+2] = maxDepth
End
Local k := 128.0 / stars[i+2]
starX[i/3] = stars[i] * k + ox
starY[i/3] = stars[i+1] * k + oy
Next
End
Method Render:Void()
For Local i := 0 To starX.Length()-1
Local size := (1 - Float(stars[i*3+2]) / maxDepth) * 2
Local shade := Clamp((1 - Float(stars[i*3+2]) / maxDepth), 0.0, 1.0)
SetAlpha(shade)
DrawCircle(starX[i] - size/2, starY[i]- size/2, size)
Next
SetAlpha(1)
SetColor(255,255,255)
End
End