-
Notifications
You must be signed in to change notification settings - Fork 8
/
Example - 2d Fireworks.monkey2
97 lines (89 loc) · 2.05 KB
/
Example - 2d Fireworks.monkey2
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
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
' This is the particle class - Each particle is a part
' of the fireworks. It starts a light and heads in a
' direction and dims out...
Class particle
'fields
Field x:Float,y:Float
Field direction:Double
Field speed:Float
Field col:Color
Field deleteme:Bool
Field time:Int
Method New(x:Int,y:Int,direction:Double)
Self.x = x
Self.y = y
Self.speed = 3
If Rnd(20)<3 Then Self.speed += Rnd(-.5,.5)
Self.direction = direction
Self.col = New Color(1,1,1)
End Method
Method update()
time+=1
' If burn out then flag for delete
If time > 60 Then deleteme = True
' update the position
x+=Cos(direction)*speed
y+=Sin(direction)*speed
' Dim out the color.
col = New Color(col.R-1.0/60,col.G-1.0/60,col.B-1.0/60)
' Slow the speed by a fraction
speed -= speed/60
End Method
Method draw(canvas:Canvas)
canvas.Color = col
canvas.DrawCircle(x,y,2)
End Method
End Class
Global myparticle:List<particle> = New List<particle>
Class MyWindow Extends Window
Method New()
Title="Fireworks..."
'
ClearColor = Color.Black
'
'
End Method
Method update()
' Here we create the fireworks.
' At a random situation
If Rnd(100)<10
' create a point (x,y)
Local x:Int=Rnd(0,Width)
Local y:Int=Rnd(0,Height)
' 20 or so particles in a random direction
For Local i:Int=0 Until 20
myparticle.Add(New particle(x,y,Rnd(TwoPi)))
Next
End If
' Update each particle
For Local i:=Eachin myparticle
i.update()
Next
' Delete flagged out particles
For Local i:=Eachin myparticle
If i.deleteme = True Then myparticle.Remove(i)
Next
End Method
Method OnRender( canvas:Canvas ) Override
App.RequestRender() ' Activate this method
'
' Here we call the update method
update()
'
' Draw the particles
For Local i:=Eachin myparticle
i.draw(canvas)
Next
' if key escape then quit
If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()
End Method
End Class
Function Main()
New AppInstance
New MyWindow
App.Run()
End Function