-
Notifications
You must be signed in to change notification settings - Fork 8
/
Monkey-X - 2d Block Pushing - code example.monkey
94 lines (85 loc) · 2.32 KB
/
Monkey-X - 2d Block Pushing - 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
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
Import mojo
Global tilewidth = 32
Global tileheight = 32
Class box
Field x:Float,y:Float
Method New(x:Float,y:Float)
Self.x = x
Self.y = y
End Method
Method draw()
SetColor 255,0,0
DrawRect x,y,tilewidth,tileheight
End Method
End Class
Class player
Field x:Float,y:Float
Method update()
If KeyDown(KEY_RIGHT)
If pushbox(1,0) = True Then x+=1
End If
If KeyDown(KEY_LEFT)
If pushbox(-1,0) = True Then x-=1
End If
If KeyDown(KEY_UP)
If pushbox(0,-1) = True Then y-=1
End If
If KeyDown(KEY_DOWN)
If pushbox(0,1) = True Then y+=1
End If
End Method
Method pushbox:Bool(x1:Int,y1:Int)
For Local i:=Eachin boxes
If rectsoverlap(x+x1,y+y1,tilewidth,tileheight,
i.x,i.y,tilewidth,tileheight)
For Local ii:=Eachin boxes
If i<>ii
If rectsoverlap(i.x+x1,i.y+y1,tilewidth,tileheight,
ii.x,ii.y,tilewidth,tileheight)
Return False
End If
End If
Next
i.x+=x1
i.y+=y1
End If
Next
Return True
End Method
Method draw()
SetColor 255,255,255
DrawRect x,y,tilewidth,tileheight
End Method
End Class
Global p:player = New player
Global boxes:List<box> = New List<box>
Class MyGame Extends App
Method OnCreate()
SetUpdateRate(60)
p.x = 320
p.y = 240
boxes.AddLast(New box(360,240))
boxes.AddLast(New box(400,240))
boxes.AddLast(New box(400,300))
End Method
Method OnUpdate()
p.update
End Method
Method OnRender()
Cls 0,0,0
SetColor 255,255,255
DrawText "Block Pushing example. Cursors move player.",0,0
For Local i:=Eachin boxes
i.draw
Next
p.draw
End Method
End Class
Function rectsoverlap:Bool(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
Function Main()
New MyGame()
End Function