-
Notifications
You must be signed in to change notification settings - Fork 8
/
Monkey-X - 2d map and player infront-behind trees - code example.monkey
123 lines (115 loc) · 3.3 KB
/
Monkey-X - 2d map and player infront-behind trees - 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Import mojo
Class game
Field px:Int,py:Int
Field pw:Int,ph:Int
Field mapwidth:Int
Field mapheight:Int
Field tilewidth:Int
Field tileheight:Int
Field map:Int[][]
Method New(width:Int,height:Int)
mapwidth = width
mapheight = height
tilewidth = DeviceWidth()/mapwidth
tileheight = DeviceHeight()/mapheight
map = New Int[mapwidth][]
For Local i=0 Until mapwidth
map[i] = New Int[mapheight]
Next
For Local i=0 Until 10
Local x:Int=Rnd(0,mapwidth)
Local y:Int=Rnd(0,mapheight)
map[x][y] = 1
Next
pw = 16
ph = 24
Local exitloop:Bool=False
While exitloop=False
Local x:Int=Rnd(0,mapwidth)
Local y:Int=Rnd(0,mapheight)
If map[x][y] <> 1
px = x*tilewidth
py = y*tileheight
exitloop = True
End If
Wend
End Method
Method playerupdate()
If KeyDown(KEY_LEFT)
px-=1
End If
If KeyDown(KEY_RIGHT)
px+=1
End If
If KeyDown(KEY_UP)
py-=1
End If
If KeyDown(KEY_DOWN)
py+=1
End If
px = Clamp(px,0,DeviceWidth()-pw)
py = Clamp(py,ph,DeviceHeight()-ph)
End Method
Method draw()
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
Local dx:Int=x*tilewidth
Local dy:Int=y*tileheight
If map[x][y] = 1
SetColor 100,100,0
DrawRect dx,dy,tilewidth,tileheight
drawtree dx+16,dy-20
End If
Next
' Here we find if we should draw the player
' as to have the trees be correct with
' his location on the screen.
If rectsoverlap( px,py-32,pw,ph,
0,
y*tileheight,
DeviceWidth(),
tileheight) = True
' here we draw the player
SetColor 255,255,0
DrawRect px,py,pw,ph
End If
Next
End Method
Method drawtree(x:Int,y:Int)
SetColor 0,200,0
DrawRect x-32,y-32,64,64
SetColor 200,100,0
DrawRect x-10,y+32,20,10
End Method
End Class
Global mygame:game
Class MyGame Extends App
Method OnCreate()
SetUpdateRate(60)
Seed = GetDate[5]
mygame = New game(20,15)
End Method
Method OnUpdate()
mygame.playerupdate
End Method
Method OnRender()
Cls 0,0,0
mygame.draw
SetColor 255,255,255
DrawText "Monkey-X - 2d Player "+
"behind And infront of"+
" trees - code example",
0,10
DrawText "Press Cursor + left-"+
"right-up-down",0,
DeviceHeight()-20
End Method
End Class
Function Main()
New MyGame()
End Function
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