-
Notifications
You must be signed in to change notification settings - Fork 8
/
Beginners - Class Array.monkey2
64 lines (58 loc) · 1.38 KB
/
Beginners - Class Array.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
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
' This the enemy class
Class enemy
' position x and y
Field px:Int,py:Int
' This is called when the class is created
Method New(x:Int,y:Int)
' Set the position
Self.px = x
Self.py = y
End Method
' Method to modify the px and py
Method move(x:Int,y:Int)
px += x
py += y
End Method
End Class
Class MyWindow Extends Window
' How many enemies are there
Field numenemies:Int=10
' The array class
Field myenemy:enemy[]
'
Method New()
' Create our class array
myenemy = New enemy[numenemies]
' Create our enemies inside the class array
For Local i:Int=0 Until numenemies
myenemy[i] = New enemy(Rnd(Width),Rnd(Height))
Next
End method
Method OnRender( canvas:Canvas ) Override
App.RequestRender() ' Activate this method
'
' We can read/modify/call anything inside the class array
myenemy[0].move(5,0)
If myenemy[0].px > Width Then myenemy[0].px = -32
'
' Draw the enemies to the screen
canvas.Clear(Color.Black)
canvas.Color = Color.White
' Loop through everything inside the array
For Local i:=Eachin myenemy
' i is used to run/read/modify the class array
canvas.DrawRect(i.px,i.py,32,32)
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