-
Notifications
You must be signed in to change notification settings - Fork 8
/
Monkey-X - 2d topdown scrolling rpg with random maps - code example.monkey
958 lines (918 loc) · 27.3 KB
/
Monkey-X - 2d topdown scrolling rpg with random maps - 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
Import mojo
' map things
' 1 = wal, 2 = door, 3 = health, 4 = coin,
' 5 = tree, 6 = water, 7 = rock
'map gen part
Global mapwidth:Int = 70
Global mapheight:Int= 60
Global sx:Int 'path start x
Global sy:Int 'path start y
Global ex:Int=mapwidth/2 'path end x
Global ey:Int=mapheight/2 'path end y
Global monmap:Int[mapwidth][]
Global map:Int[mapwidth][]
Global olmap:Int[mapwidth][]
Global clmap:Int[mapwidth][]
Global pmap:Int[mapwidth][]
' this one refreshes maps
Global waittime:Int=0
' scrolling rpg part
Const tilewidth:Int=20
Const tileheight:Int=20
Global mapx:Int=0
Global mapy:Int=0
Global mapsx:Int=0
Global mapsy:Int=0
Global showmap:Bool=False
Global remakemap:Bool=False
Class tiles
Field walltile:Image
Field rocktile:Image
Field groundtile:Image
Field moneytile:Image
Field pixels:Int[tilewidth*tileheight]
Method New()
DebugLog "creating images"
walltile = CreateImage(tilewidth,tileheight)
rocktile = CreateImage(tilewidth,tileheight)
groundtile = CreateImage(tilewidth,tileheight)
moneytile = CreateImage(tilewidth,tileheight)
DebugLog "drawing in image"
Local s:Int=255/tileheight
Local ss:Int=tileheight/125
For Local y=0 Until tileheight
drawr(0,y,tilewidth,y,y*s,y*s,y*s)
Next
DebugLog "finishing image wall"
walltile.WritePixels(pixels, 0, 0, tilewidth, tileheight, 0)
For Local y=0 Until tileheight
Local col:Int=0
If y<tileheight/2
col=0+((y*s))
Else
col=(255)-((y*s))
End If
drawr(0,y,tilewidth,y,col,col/3,0)
Next
DebugLog "finishing image rock"
rocktile.WritePixels(pixels, 0, 0, tilewidth, tileheight, 0)
For Local y=0 Until tileheight
Local col:Int=0
If y<tileheight/2
col=0+((y*s)/6)
Else
col=(255/6)-((y*s)/6)
End If
drawr(0,y,tilewidth,y,col,col,col)
Next
DebugLog "finishing image ground"
groundtile.WritePixels(pixels, 0, 0, tilewidth, tileheight, 0)
drawo(tilewidth/3,tileheight/3,tilewidth/3,$FFFF0000)
DebugLog "finishing image money"
moneytile.WritePixels(pixels, 0, 0, tilewidth, tileheight, 0)
End Method
Method drawo(x1,y1,radius,col)
For Local y2=-radius To radius
For Local x2=-radius To radius
If (y2*y2+x2*x2) <= radius*radius+radius*0.8
Local x3 = x2+x1
Local y3 = y2+y1
Local pc = y3*tilewidth+x3
If pc>=0 And pc < tilewidth*tileheight
pixels[pc] = col
End If
End If
Next
Next
End Method
Method drawr(x1,y1,w1,h1,r:Int,g:Int,b:Int)
For Local y2=y1 Until y1+h1
For Local x2=x1 Until x1+w1
Local pc = y2*tilewidth+x2
If pc >= 0 And pc < tilewidth*tileheight
pixels[pc] = argb(r,g,b)
End If
Next
Next
End Method
Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255)
Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b
End Function
End Class
Class monster
Field lmap:Int[][]
Field x:Int,y:Int
Field mdelay:Int=0
Field monsterspeed:Int
Field offx:Int,offy:Int
Field offxt:Int,offyt:Int
Field mnx:Int,mny:Int
Method New()
lmap = makearray()
' find place for monster
Local exitloop:Bool=False
While exitloop = False
Local x1:Int=Rnd(5,mapwidth-5)
Local y1:Int=Rnd(5,mapheight-5)
If monmap[x1][y1] = 1 Then
exitloop = True
lmap[x1][y1]=1
Self.x = x1
Self.y = y1
End If
Wend
mnx = Self.x
mny = Self.y
offxt = 0
offyt = 0
offx = 0
offy = 0
monsterspeed = Rnd(1,15)
End Method
Method update()
'Return
mdelay+=1
If mdelay< monsterspeed Then Return
mdelay=0
If offx<offxt
offx+=1
End If
If offx>offxt
offx-=1
End If
If offy<offyt
offy+=1
End If
If offy>offyt
offy-=1
End If
Local set:Bool=False
If offx=offxt And
offy=offyt Then
set=True
End If
If set=False Then Return
' DebugLog offx+":"+offxt+","+offy+":"+offyt
x = mnx
y = mny
lmap[x][y]+=1
If lmap[x][y] > 1000
For Local y1=0 Until mapheight
For Local x1=0 Until mapwidth
lmap[x1][y1] = 0
Next
Next
lmap[x][y]=1
End If
Local exitloop:Bool=False
Local db:Int=0
While exitloop = False
db+=1
If db>200 Then exitloop=True
Local y2:Int=Rnd(-2,2)
Local x2:Int=Rnd(-2,2)
If monmap[x+x2][y+y2] = 1
If lmap[x+x2][y+y2] <= lmap[x][y]
offx = 0
offy = 0
offxt = 0
offyt = 0
If x2<0
offxt -= tilewidth
Elseif x2>0
offxt += tilewidth
End If
If y2<0
offyt -= tileheight
Elseif y2>0
offyt += tileheight
End If
'offxt=x2*tilewidth
'offyt=y2*tileheight
mnx=x+x2
mny=y+y2
exitloop=True
Return
End If
End If
Wend
End Method
Method draw()
SetColor 255,0,0
Local x1 = (x*tilewidth)
Local y1 = (y*tileheight)
x1-=(mapx*tilewidth)
y1-=(mapy*tileheight)
x1+=mapsx
y1+=mapsy
x1+=offx
y1+=offy
DrawRect x1-tilewidth,
y1,
tilewidth,tileheight
SetColor 255,255,255
DrawText "M",
(x1-tilewidth)+(tilewidth/2),
y1+tileheight/2,
0.5,0.5
End Method
End Class
Class player
Field x:Float=3*tilewidth
Field y:Float=3*tileheight
Field width:Int = tilewidth
Field height:Int = tileheight
Field keys:Int = 0
Field coins:Int = 0
Field health:Int = 3
Field maxhealth:Int=10
End Class
Class mapgen
Field gmap:Int[mapwidth][]
Field hmap:Int[mapwidth][]
Field twidth:Float,theight:Float
Method New()
For Local i = 0 Until mapwidth
gmap[i] = New Int[mapheight]
hmap[i] = New Int[mapheight]
Next
twidth=640/mapwidth
theight=480/mapheight
makehmap
makemap
End Method
Method makemap()
DebugLog "make map"
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
map[x][y] = hmap[x][y]
Next
Next
ex = Rnd(10,mapwidth-10)
ey = Rnd(10,mapheight-10)
Local numpaths:Int = (mapwidth*mapheight)/700
DebugLog "make area's"
For Local i=0 Until numpaths
sx = ex
sy = ey
Local exitloop:Bool=False
While exitloop = False
ex = Rnd(10,mapwidth-10)
ey = Rnd(10,mapheight-10)
If sx<>ex And sy<>ey Then
exitloop = True
End If
Wend
findpath
For Local ii:=Eachin path
Local s:Int=2
If Int(Rnd(1,5)) = 2
s=Rnd(2,5)
Elseif Int(Rnd(1,9)) = 2
s=8
End If
For Local y=-s/2 Until s/2
For Local x=-s/2 Until s/2
If ii.x+x >= 0 And ii.x+x <mapwidth And
ii.y+y >= 0 And ii.y+y < mapheight
gmap[ii.x+x][ii.y+y] = 1
pmap[ii.x+x][ii.y+y] = 1
End If
Next
Next
Next
Next
' make monster map
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
If gmap[x][y] = 1
monmap[x][y] = 1
End If
Next
Next
DebugLog "add walls"
' here add the walls
For Local y=1 Until mapheight-1
For Local x=1 Until mapwidth-1
If gmap[x][y] = 0
If gmap[x+1][y] = 1
gmap[x][y]=2
End If
End If
If gmap[x][y] = 1
If gmap[x+1][y] = 0
gmap[x+1][y] = 2
End If
End If
If gmap[x][y] = 0
If gmap[x][y+1] = 1
gmap[x][y] = 2
End If
End If
If gmap[x][y] = 1
If gmap[x][y+1] = 0
gmap[x][y+1] = 2
End If
End If
Next
Next
DebugLog "set player on map and placecoins"
' set player on maptile
setplayerposition
DebugLog "place coins"
placecoins((mapwidth*mapheight)/260)
DebugLog "place life"
placehealth((mapwidth*mapheight)/1000)
' map conversion
DebugLog "turn walkable into rock"
' turn walkable into rock
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
If gmap[x][y] = 0 Then gmap[x][y] = 7
Next
Next
DebugLog "Turn floor into walkable"
' turn floor into walkable
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
If gmap[x][y] = 1 Then
gmap[x][y] = 0
End If
If gmap[x][y] = 2
gmap[x][y] = 1
End If
Next
Next
End Method
Method drawmap()
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
If gmap[x][y] = 1
SetColor 150,150,150
DrawRect x*twidth,y*theight,
twidth,theight
End If
If gmap[x][y] = 2
SetColor 200,200,200
DrawRect x*twidth,y*theight,
twidth,theight
End If
Next
Next
SetColor 255,255,0
drawboxedrect( mapx*twidth,mapy*theight,
(DeviceWidth/tilewidth)*twidth,
(DeviceHeight/tileheight)*theight)
End Method
Method makehmap()
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
hmap[x][y] = 3
Next
Next
For Local i=0 To (mapwidth*mapheight)/10
Local w:Int=Rnd(3,8)
Local h:Int=Rnd(3,8)
Local x:Int=Rnd(3,mapwidth-8)
Local y:Int=Rnd(3,mapheight-8)
For Local y1=-w/2 To w/2
For Local x1=-h/2 To h/2
hmap[x+x1][y+y1]+=1
Next
Next
Next
End Method
Method drawhmap()
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
SetColor hmap[x][y]*10,0,0
DrawRect x*twidth,y*theight,
twidth,theight
Next
Next
End Method
Method placehealth(amount:Int)
For Local i=0 Until amount
Local exitloop:Bool=False
Local exloop:Int=0
While exitloop = False
Local x:Int=Rnd(0,mapwidth)
Local y:Int=Rnd(0,mapheight)
If gmap[x][y] = 1
gmap[x][y] = 3
exitloop = True
End If
exloop+=1
If exloop>100 Then exitloop=True
Wend
Next
End Method
Method placecoins(amount:Int)
For Local i=0 Until amount
Local exitloop:Bool=False
Local exloop:Int=0
While exitloop = False
Local x:Int=Rnd(0,mapwidth)
Local y:Int=Rnd(0,mapheight)
If gmap[x][y] = 1
gmap[x][y] = 4
exitloop = True
End If
exloop+=1
If exloop>100 Then exitloop=True
Wend
Next
End Method
Method setplayerposition()
Local exitloop:Bool=False
Local eo:Int=0
While exitloop = False
eo+=1
If eo>500 Then
remakemap = True
Return
End If
Local x:Int=Rnd(mapwidth)
Local y:Int=Rnd(mapheight)
If gmap[x][y] = 1
mapx = x-(DeviceWidth/tilewidth)/2
mapy = y-(DeviceHeight/tileheight)/2
p.x = (DeviceWidth)/2
p.y = (DeviceHeight)/2
mapsx = 0
mapsy = 0
exitloop = True
End If
Wend
End Method
End Class
Class openlist
Field x:Int
Field y:Int
Field f:Int
Field g:Int
Field h:Int
Field px:Int
Field py:Int
Method New( x:Int=0,y:Int=0,f:Int=0,
g:Int=0,h:Int=0,px:Int=0,
py:Int=0)
Self.x=x
Self.y=y
Self.f=f
Self.g=g
Self.h=h
Self.px=px
Self.py=py
End Method
End Class
Class closedlist
Field x:Int
Field y:Int
Field px:Int
Field py:Int
Method New(x:Int,y:Int,px:Int,py:Int)
Self.x = x
Self.y = y
Self.px = px
Self.py = py
End Method
End Class
Class pathnode
Field x:Int
Field y:Int
Method New(x:Int,y:Int)
Self.x = x
Self.y = y
End Method
End Class
Global mytiles:tiles
Global ol:List<openlist> = New List<openlist>
Global cl:List<closedlist> = New List<closedlist>
Global path:List<pathnode> = New List<pathnode>
Global mymonster:List<monster> = New List<monster>
Global mymapgen:mapgen
' the player class in p
Global p:player = New player
Class MyGame Extends App
Method OnCreate()
mytiles = New tiles
SetUpdateRate(60)
For Local i=0 Until mapwidth
monmap[i] = New Int[mapheight]
map[i] = New Int[mapheight]
olmap[i] = New Int[mapheight]
clmap[i] = New Int[mapheight]
pmap[i] = New Int[mapheight]
Next
mymapgen = New mapgen()
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
map[x][y] = mymapgen.gmap[x][y]
Next
Next
For Local i=0 Until 5
mymonster.AddLast(New monster())
Next
End Method
Method OnUpdate()
waittime+=1
' If waittime>120
' If Rnd(0,100)>98 Then remakemap=True
For Local i:=Eachin mymonster
i.update
Next
showmap=False
If KeyDown(KEY_M)
showmap=True
End If
If KeyHit(KEY_T) Or
remakemap=True' Or
'waittime>180 Then
mymonster.Clear
waittime=0
remakemap=False
Seed=Millisecs()
mapwidth=Rnd(30,200)
mapheight=Rnd(30,200)
monmap = makearray()
map = makearray()
olmap = makearray()
clmap = makearray()
pmap = makearray()
mymapgen = New mapgen()
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
map[x][y] = mymapgen.gmap[x][y]
Next
Next
For Local i=0 Until (mapwidth*mapheight)/500
mymonster.AddLast(New monster())
Next
If ptc(0,0)
remakemap=True
End If
End If
' waittime = 0
' End If
' scrolling rpg part
For Local i=0 Until 2
If p.x > DeviceWidth / 2
If mapx+(DeviceWidth/tilewidth) < mapwidth-1
mapsx-=1
If mapsx < 0 Then
mapsx = tilewidth-1
mapx += 1
Endif
p.x-=1
End If
End If
Next
For Local i=0 Until 2
If p.x < DeviceWidth / 2
If mapx > 0
mapsx+=1
If mapsx > tilewidth Then
mapsx = 0
mapx -= 1
Endif
p.x+=1
End If
End If
Next
' scrolling down
For Local i=0 Until 2
If p.y > DeviceHeight / 2
If mapy+(DeviceHeight/tileheight) < mapheight-1
mapsy-=1
If mapsy < 0 Then
mapsy = tileheight-1
mapy += 1
Endif
p.y-=1
End If
End If
Next
' scrolling up
For Local i=0 Until 2
If p.y < DeviceHeight / 2
If mapy > 0
mapsy+=1
If mapsy > tileheight-1 Then
mapsy = 0
mapy -= 1
Endif
p.y+=1
End If
End If
Next
If KeyDown(KEY_RIGHT)
ptcs(1,0)
For Local i=0 Until 2
If ptc(1,0) = False
p.x+=1
End If
Next
End If
If KeyDown(KEY_LEFT)
ptcs(-1,0)
For Local i=0 Until 2
If ptc(-1,0) = False
p.x-=1
End If
Next
End If
If KeyDown(KEY_UP)
ptcs(0,-1)
For Local i=0 Until 2
If ptc(0,-1) = False
p.y-=1
End If
Next
End If
If KeyDown(KEY_DOWN)
ptcs(0,1)
For Local i=0 Until 2
If ptc(0,1) = False
p.y+=1
End If
Next
End If
End Method
Method OnRender()
Cls(0,0,0)
If remakemap=True Then Return
If showmap=True Then
mymapgen.drawmap
End If
If showmap = False
SetColor 255,255,255
drawmap
SetColor 255,255,0
DrawOval p.x,p.y,p.width,p.height
For Local i:=Eachin mymonster
i.draw
Next
SetColor 0,0,0
DrawRect 0,0,DeviceWidth,32
SetColor 255,255,255
DrawText "Keys : "+p.keys,10,0
DrawText "Coins : "+p.coins,10,16
DrawText "Health : "+p.health+
" of "+p.maxhealth,96,0
DrawText "Use Cursor keys to move.",
DeviceWidth/2,0
DrawText "Collect coins, press t = Teleport"+
" to new cavern.",DeviceWidth/2,16
DrawText "Press m for mapscreen.",
DeviceWidth/2,DeviceHeight-20
End If
End
End
' player collide with special blocks.
Function ptcs:Int(offsetx:Int=0,offsety:Int=0)
Local cx = (p.x+offsetx)/tilewidth+mapx
Local cy = (p.y+offsety)/tileheight+mapy
For Local y2=cy-1 Until cy+4
For Local x2=cx-1 Until cx+4
If x2>=0 And x2<mapwidth And
y2>=0 And y2<mapheight
If map[x2][y2] > 0
Local x3 = (x2-mapx)*tilewidth-tilewidth+mapsx
Local y3 = (y2-mapy)*tileheight+mapsy
If rectsoverlap( p.x+offsetx,
p.y+offsety,
p.width,
p.height,
x3,y3,
tilewidth,
tileheight) = True
Select map[x2][y2]
Case 2
If p.keys > 0
map[x2][y2] = 0
p.keys-=1
End If
Case 3 'health
If p.health < p.maxhealth
map[x2][y2] = 0
p.health+=1
Else
map[x2][y2] = 0
p.maxhealth+=1
p.health+=1
End If
Case 4 'coins
map[x2][y2] = 0
p.coins+=1
End Select
End If
End If
End If
Next
Next
Return 0
End Function
'player collide with solid blocks true/false
Function ptc:Bool(offsetx:Int=0,offsety:Int=0)
Local cx = (p.x+offsetx)/tilewidth+mapx
Local cy = (p.y+offsety)/tileheight+mapy
For Local y2=cy-1 Until cy+4
For Local x2=cx-1 Until cx+4
If x2>=0 And x2<mapwidth And y2>=0 And
y2<mapheight
If map[x2][y2] = 1 Or map[x2][y2] = 2 Or
map[x2][y2] = 5 Or map[x2][y2] = 6 Or
map[x2][y2] = 7
Local x3 = (x2-mapx)*tilewidth-tilewidth+mapsx
Local y3 = (y2-mapy)*tileheight+mapsy
If rectsoverlap(p.x+offsetx,p.y+offsety,
p.width,p.height,x3,y3,
tilewidth,tileheight) = True
Return True
End If
End If
End If
Next
Next
Return False
End Function
Function drawmap:Void()
For Local y=0 To DeviceHeight/tileheight
For Local x=0 To DeviceWidth/tilewidth
Local x1 = ((x*tilewidth)+mapsx)-tilewidth
Local y1 = ((y*tileheight)+mapsy)
Local mw:Int=mapwidth
Local mh:Int=mapheight
Local mx:Int=mapx
Local my:Int=mapy
If x+mapx<mapwidth And
x+mapx>0 And
y+mapy<mapheight And
y+mapy>0
Select map[x+mapx][y+mapy]
Case 0 'ground
DrawImage mytiles.groundtile,x1,y1
Case 1'Wall
' SetColor 100,100,100
' DrawRect x1,y1,tilewidth,tileheight
DrawImage mytiles.walltile,x1,y1
Case 2'Door
SetColor 200,100,0
DrawRect x1,y1,tilewidth,tileheight
Case 3'Health
SetColor 200,0,0
DrawOval x1+4,y1+4,tilewidth-8,tileheight-8
SetColor 255,255,255
DrawText "H",x1+tilewidth/2,y1+tileheight/2,0.5,0.5
Case 4'Coin
'SetColor 255,255,0
'DrawOval x1+4,y1+4,tilewidth-8,tileheight-8
'SetColor 255,255,255
DrawImage mytiles.moneytile,x1,y1
DrawText "$",x1+tilewidth/2,y1+tileheight/2,0.5,0.5
Case 5'tree
SetColor 0,200,0
DrawPoly( [Float(x1+16),y1 ,x1+tilewidth,
y1+tileheight , x1,y1+tileheight])
Case 6'water
SetColor 0,0,200
DrawRect x1,y1,tilewidth,tileheight
Case 7'rock
'SetColor 150,50,0
'DrawRect x1,y1+10,tilewidth,tileheight-10
DrawImage mytiles.rocktile,x1,y1
'DrawPoly([Float(x1+tilewidth/2),y1,x1+tilewidth,
' y1+tileheight,x1,y1+tileheight])
End Select
End If
Next
Next
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
Function drawboxedrect:Void(x:Int,y:Int,w:Int,h:Int)
DrawLine x,y,x+w,y
DrawLine x,y,x,y+h
DrawLine x,y+h,x+w,y+h
DrawLine x+w,y,x+w,y+h
End Function
Function makearray:Int[][]()
Local aa:Int[mapwidth][]
For Local i=0 Until mapwidth
aa[i] = New Int[mapheight]
Next
Return aa
End Function
Function findpath:Bool()
If sx = ex And sy = ey Then Return False
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
olmap[x][y] = 0
clmap[x][y] = 0
Next
Next
ol.Clear
cl.Clear
path.Clear
ol.AddFirst(New openlist(sx,sy))
Local tx:Int
Local ty:Int
Local tf:Int
Local tg:Int
Local th:Int
Local tpx:Int
Local tpy:Int
Local newx:Int
Local newy:Int
Local lowestf:Int
olmap[sx][sy] = 1
While ol.IsEmpty() = False
lowestf = 100000
For Local i:=Eachin ol
If i.f < lowestf
lowestf = i.f
tx = i.x
ty = i.y
tf = i.f
tg = i.g
th = i.h
tpx = i.px
tpy = i.py
End If
Next
If tx = ex And ty = ey
cl.AddLast(New closedlist(tx,ty,tpx,tpy))
findpathback
Return True
Else
removefromopenlist(tx,ty)
olmap[tx][ty] = 0
clmap[tx][ty] = 1
cl.AddLast(New closedlist(tx,ty,tpx,tpy))
For Local y=-1 To 1
For Local x=-1 To 1
newx = tx+x
newy = ty+y
If newx>=0 And newy>=0 And
newx<mapwidth And newy<mapheight
If olmap[newx][newy] = 0
If clmap[newx][newy] = 0
olmap[newx][newy] = 1
Local gg:Int
If pmap[newx][newy] = 1
gg=1
Else
gg = map[newx][newy]+1
End If
Local hh = distance(newx,newy,ex,ey)
Local ff = gg+hh
ol.AddLast(New openlist(newx,
newy,ff,
gg,hh,
tx,ty))
End If
End If
End If
Next
Next
End If
Wend
Return False
End Function
Function findpathback:Bool()
Local x=ex
Local y=ey
path.AddFirst(New pathnode(x,y))
Repeat
For Local i:=Eachin cl
If i.x = x And i.y = y
x = i.px
y = i.py
path.AddFirst(New pathnode(x,y))
End If
Next
If x = sx And y = sy Then Return True
Forever
End Function
Function removefromopenlist:Void(x1:Int,y1:Int)
For Local i:=Eachin ol
If i.x = x1 And i.y = y1
ol.Remove i
Exit
End If
Next
End Function
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
Function Main()
New MyGame()
End