Need help with understanding the code

Monkey Forums/Monkey Beginners/Need help with understanding the code

CanisLupus(Posted May) [#1]
I have been following this tutorial:
http://monkeygameprogramming.blogspot.co.uk/2015/01/monkey-x-2d-scrolling-platformer-with.html

I don't understand the codes below:
'player springy collision
Function psc: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[y2][x2] = 2
Local x3 = (x2-mapx)*tilewidth-32+mapsx
Local y3 = (y2-mapy)*tileheight+mapsy
If rectsoverlap(p.x+offsetx,p.y+offsety,p.w,p.h,x3,y3+tileheight/2,tilewidth,tileheight/2) = True
Return True
End If
End If
End If
Next
Next
Return False
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[y2][x2] = 1
Local x3 = (x2-mapx)*tilewidth-32+mapsx
Local y3 = (y2-mapy)*tileheight+mapsy
If rectsoverlap(p.x+offsetx,p.y+offsety,p.w,p.h,x3,y3,tilewidth,tileheight) = True
Return True
End If
End If
End If
Next
Next
Return False
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

Can someone please explain what each line means


dawlane(Posted May) [#2]
I would suggest that you start with Invader Jim's tutorials first. They maybe old, but they are still relevant.
Pakz code needs more comments to explain what's going on.
With out going through the whole code

This bit is passing an offset to be added to the players position. In other words the area to search to one side of the player. It expects a result of the data type bool (a Boolean true or false value) to be passed back to the calling code.
Function psc:Bool(offsetx:Int=0,offsety:Int=0)

The next two are converting the players screen position with offset to column/row for the map array
Local cx = (p.x+offsetx)/tilewidth+mapx
Local cy = (p.y+offsety)/tileheight+mapy

The next 13 lines are doing a limited scan of the map array from the players offset position within the map array to see if the player has collided with anything.
For Local y2=cy-1 Until cy+4
For Local x2=cx-1 Until cx+4

This line is checking that for loops are within the map array before getting the value stored. If it didn't, then it would generate an index out of range error.
If x2>=0 And x2<mapwidth And y2>=0 And y2<mapheight

This line is checking the map array to see if the value at y2, x2 is equal to 2. This is the code given to whatever is to be checked for.
If map[y2][x2] = 2

This line is now converting the location of the detected map cell into screen coordinates
Local x3 = (x2-mapx)*tilewidth-32+mapsx
Local y3 = (y2-mapy)*tileheight+mapsy

This line is checking if the rectangle that represents the player's offset is in collision with the rectangle that represents whatever the player has collided with.
The rectsoverlap is a function that will return a value of True or False. If it's true, then it breaks out of the for/next loops and returns back the whatever
originally call the function with a result of true
If rectsoverlap(p.x+offsetx,p.y+offsety,p.w,p.h,x3,y3+tileheight/2,tilewidth,tileheight/2) = True
Return True

The rest are clousures for the If statments and For loops
End If
End If
End If
Next
Next

If the program reaches here, then nothing was detected, so return a value of false back to whatever called the function
Return False
End Function

This function would be easy to visualise whats happening if you use graph paper like they use in school to work out shapes and trigonometry.
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

This should help understanding whats happening with the rectsoverlap above code.

Use a calculator to work out what happening with the relationship between the screen and the map.