Skip to content

Commit 9240544

Browse files
authored
Create Beginners - Class Array.monkey2
1 parent 73050b9 commit 9240544

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Beginners - Class Array.monkey2

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#Import "<std>"
2+
#Import "<mojo>"
3+
4+
Using std..
5+
Using mojo..
6+
7+
' This the enemy class
8+
Class enemy
9+
' position x and y
10+
Field px:Int,py:Int
11+
' This is called when the class is created
12+
Method New(x:Int,y:Int)
13+
' Set the position
14+
Self.px = x
15+
Self.py = y
16+
End Method
17+
' Method to modify the px and py
18+
Method move(x:Int,y:Int)
19+
px += x
20+
py += y
21+
End Method
22+
End Class
23+
24+
Class MyWindow Extends Window
25+
' How many enemies are there
26+
Field numenemies:Int=10
27+
' The array class
28+
Field myenemy:enemy[]
29+
'
30+
Method New()
31+
' Create our class array
32+
myenemy = New enemy[numenemies]
33+
' Create our enemies inside the class array
34+
For Local i:Int=0 Until numenemies
35+
myenemy[i] = New enemy(Rnd(Width),Rnd(Height))
36+
Next
37+
End method
38+
39+
Method OnRender( canvas:Canvas ) Override
40+
App.RequestRender() ' Activate this method
41+
'
42+
' We can read/modify/call anything inside the class array
43+
myenemy[0].move(5,0)
44+
If myenemy[0].px > Width Then myenemy[0].px = -32
45+
'
46+
' Draw the enemies to the screen
47+
canvas.Clear(Color.Black)
48+
canvas.Color = Color.White
49+
' Loop through everything inside the array
50+
For Local i:=Eachin myenemy
51+
' i is used to run/read/modify the class array
52+
canvas.DrawRect(i.px,i.py,32,32)
53+
Next
54+
' if key escape then quit
55+
If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()
56+
End Method
57+
58+
End Class
59+
60+
Function Main()
61+
New AppInstance
62+
New MyWindow
63+
App.Run()
64+
End Function

0 commit comments

Comments
 (0)