-
Notifications
You must be signed in to change notification settings - Fork 8
/
Monkey - Asteroid field with Class and List code example.monkey
64 lines (57 loc) · 1.48 KB
/
Monkey - Asteroid field with Class and List code example.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
Import mojo
Class asteroid
Field x:Float
Field y:Float
Field incx:Float
Field incy:Float
Method New()
x = Rnd(640)
y = Rnd(480)
incx = Rnd(-1,1)
incy = Rnd(-1,1)
End
End
Global mylist:List<asteroid> = New List<asteroid>
Class MyGame Extends App
Method OnCreate()
SetUpdateRate(60)
For Local i:Int = 0 Until 10
mylist.AddLast(New asteroid)
End
End
Method OnUpdate()
' if outside the window then go back into the window from the other side
For Local i:asteroid = Eachin mylist
i.x+=i.incx
i.y+=i.incy
If i.x<0 Then i.x = 640
If i.x>640 Then i.x = 0
If i.y<0 Then i.y = 480
If i.y>480 Then i.y = 0
End
For Local i:asteroid = Eachin mylist
For Local ii:asteroid = Eachin mylist
If i<>ii
If rectsoverlap(i.x,i.y,10,10,ii.x,ii.y,10,10) = 1
mylist.Remove ii
End
end
End
End
End
Method OnRender()
Cls(0,0,0)
SetColor(255,255,255)
For Local i:asteroid = Eachin mylist
DrawRect i.x,i.y,10,10
End
End
End
Function Main()
New MyGame()
End
Function rectsoverlap:Int(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
If x1 > (x2 + w2) Or (x1 + w1) < x2 Then Return False
If y1 > (y2 + h2) Or (y1 + h1) < y2 Then Return False
Return True
End