Okay, since I wanted to do something special for my 1000th post, I decided to write a tutorial on doing platformers. What? Oh, you can't wait? Let's start, then!
The first thing we need, is a tilemap. In this tutorial, I'm using a simple, very awful, but useful tilemap.
Put this in prgmPLATFTLM:
.AXTILEMP
.Map:
[000000000000000000000000→Str1TM
[000000000000000000000000
[000000000000000000000000
[000206050000000000000000
[000300040002060500020605
[000300020605000206000004
[000300030004000300000004
[010101010101010101010101
.Tiles:
[FFAAFF55FFAAFF55→Pic1TL
[0F10204080808080
[8080808080808080
[0101010101010101
[F008040201010101
[FF00000000000000
[0000000000000000
.Now, we can draw the tilemap!
Lbl DMP
.Draw MaP
For(A,0,7
For(B,0,11
If {Str1TL+(A*12)+B}→Θ
Θ -1→Θ
Pt-On(B*8,A*8,Pic1TL+(Θ*8)
End
End
End
Return
If you run this code, you’ll see a not-so-very-bad looking tilemap (uhh, actually nothing, there’s no DispGraph xD).
Now comes the hard part: The collisions. There are 2 ways to do this:
1. Pixel-based collision
2. Tile-based collision
The tile-based collision is the hardest. That’s why I’m doing that first.
Put this code in prgmPLATFCOL:
.COLLISSN
Lbl COL
.COLlission
.Inputs: arg1=X ,arg2=Y ,arg3=dX to check,arg4=dY to check (I’ll explain later)
.Since we want it smooth-scrolling, we divide the X and Y value by 8 to get the corresponding tile
{Str1TL+(((Y+r2)/8)*12)+((X+r1)/8)}
Return
Okay, we can get the tile now, but there’s nothing to actually check something for!
We need an character, and we need to let it move right and left (Jumping and falling come later).
Put this in prgmPLATCHAR:
.CHARACTR
[187E243C7E3C5A66]→Pic1CH
Lbl MOV
.MOVe
If getKey(2) and (X≠0)
X-1→X
End
If getKey(3) and(X≠88)
X+1→X
End
Return
Lbl DCH
.Draw CHaracter
Pt-Off(X,Y,Pic1CH)
.You can also use Pt-Mask(), but come on, this is a tutorial!
Return
Now, that’s done, and we want to actually see something, right?
Put this in prgmPLATFRM:
.PLATFORM
0→X
55→Y
Repeat getKey(15)
ClrDraw
Sub(MOV)
Sub(DMP)
Sub(DCH)
DispGraph
End
Return
prgmPLATFTLM
prgmPLATCHAR
See how easy those sub-routines are?