Today, I will be making a tutorial on how to make a very BASIC homescreen collision detection engine.

I will be breaking the following code into different parts and explain in detail what each bit does.

Here is the code I am going to teach you about.

The matrix's dimension is 8x16, which is the size of the whole homescreen.

Part 1: Displaying a visual of the data for the collision detection.

Here is the segment this part will be over.

The following segment is the matrix that will be holding all of the data for the collision detection.

This is storing your starting location.

Please note the biggest you should have is a 8x16 matrix, which covers the entire home screen.

The 0's represent the empty spaces that nothing will be displayed on showing it is walkable.

The 2's however, are non-walkable positions (I will explain more when we get to the movement engine itself)

And the 1 is the "target" location, which is walkable (I will explain how that is walkable and how to adjust it later also)

Now onto the next area: Displaying the data so that the matrix is visible

This is where you tell the program how to display the matrix, since I have the "For(A,1,8)" before the "For(B,1,16)" it is going to display Left to right, 1 row at a time, now if A was after B, It would display Up to down, 1 column at a time.

This right here is telling the program which position in the matrix it is looking at to see what number it is on.

This is going to skip over and 0's it finds so that it renders faster, which is mainly what the last segment I pointed out is for.

Now this is the fun part, and I am going to break it down a bit.

This is going to display on the current location on the Homescreen, and is equivalent to the location in the Matrix.

This is telling the Output() what character to display depending on the number in the current position of the matrix.

Now I will also explain the sub() command for those who don't know how it works.

sub() takes a Strn or text inside quotes, like in this code "WX"

so sub("WX", is going to take the number inside of the current location of the matrix, which is the [A](A,B) bit in it, and in the order of the string, so "WX" is kinda like in the Matrix "12", while the 1 followed by the matrix is telling it to use only 1 character from the string.

This is just closing all the loops that are running as they finish.

Part 2: The rest of the movement code

Here is where it all starts to work out.

This is going to put you in the starting location, which was defined in the last part.

This is a Repeat Loop, it only ends when the function specified is true, in this case, you standing on the spot where the number 1 is at.

If there is no button pushed, it will keep looping until there is one.

Clears your space so that if you moved your guy, he will not stay there and move on, meaning only 1 space will show him at any 1 time.

If K is equal to 24(left) 26(right) 25(up) or 34(down) then it will move your character to according to the following algorithm, which I will explain.

I will break these up to make it easier to understand.

Takes your current location for C, and subtracts it from the answer, which I am about to explain

Checks if you pressed left, if you did, it would return a 1, if not, a 0

This checks to see if the space behind you on the matrix is less then or equal to 1, so if that space is a 1 or a 0, then if returns a 1, if not a 0

so far, we have

in a more simple sense.

Adds the previous answer to the next set of conditions.

Checks if you pressed right, if you did, it would return a 1, if not, a 0

This checks to see if the space in front of you on the matrix is less then or equal to 1, so if that space is a 1 or a 0, then if returns a 1, if not a 0

Stores your new location to C

So in a more simple form(won't work, but for visual aid) the code is pretty much

Now for the second movement conditional.

Takes your current location for C, and subtracts it from the answer, which I am about to explain

Checks if you pressed up, if you did, it would return a 1, if not, a 0

This checks to see if the space above you on the matrix is less then or equal to 1, so if that space is a 1 or a 0, then if returns a 1, if not a 0

so far, we have

in a more simple sense.

Adds the previous answer to the next set of conditions.

Checks if you pressed down, if you did, it would return a 1, if not, a 0

This checks to see if the space in below you on the matrix is less then or equal to 1, so if that space is a 1 or a 0, then if returns a 1,

if, not a 0

Stores your new location to D

So in a more simple form(won't work, but for visual aid) the code is pretty much

Places you on the new location.

Closes the If K≠0:Then loop

Closes the Repeat Loop.

This is what will display if you get your character to the 1 in the matrix, which will make the Repeat function true, and go to the next line after that Loop is closed.

There is my first tutorial, and I hope this help many of you sometime. :D

I will be breaking the following code into different parts and explain in detail what each bit does.

Here is the code I am going to teach you about.

The matrix's dimension is 8x16, which is the size of the whole homescreen.

**Code:**```
:ClrHome
```

:2→C:2→D

:[[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2][2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2][2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,2][2,0,0,0,2,2,2,2,0,0,0,2,1,2,0,2][2,0,2,0,0,0,0,0,0,2,0,0,0,2,0,2][2,0,2,2,2,2,2,2,2,2,2,2,2,2,0,2][2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2][2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]]→[A]

:For(A,1,8)

:For(B,1,16)

:[A](A,B)

:If Ans≠0:Then

:Output(A,B,sub("WX",[A](A,B),1))

:End

:End

:End

Part 1: Displaying a visual of the data for the collision detection.

Here is the segment this part will be over.

**Code:**```
:ClrHome
```

:2→C:2→D

:[[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2][2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2][2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,2][2,0,0,0,2,2,2,2,0,0,0,2,1,2,0,2][2,0,2,0,0,0,0,0,0,2,0,0,0,2,0,2][2,0,2,2,2,2,2,2,2,2,2,2,2,2,0,2][2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2][2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]]→[A]

:For(A,1,8)

:For(B,1,16)

:[A](A,B)

:If Ans≠0:Then

:Output(A,B,sub("WX",[A](A,B),1))

:End

:End

:End

:Output(D,C,"θ")

:Repeat [A](D,C)=1

:getKey→K

:If K≠0:Then

:Output(D,C," ") //1 space

:C-((K=24)([A](D,C-1)≤1))+((K=26)([A](D,C+1)≤1))→C

:D-((K=25)([A](D-1,C)≤1))+((K=34)([A](D+1,C)≤1))→D

:Output(D,C,"θ")

:End

:End

:ClrHome

:Output(1,4,"YOU WIN")

:Pause

:ClrHome

:Return

The following segment is the matrix that will be holding all of the data for the collision detection.

This is storing your starting location.

**Code:**`:2→C:2→D`

Please note the biggest you should have is a 8x16 matrix, which covers the entire home screen.

**Code:**```
:[[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2][2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2][2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,2][2,0,0,0,2,2,2,2,0,0,0,2,1,2,0,2][2,0,2,0,0,0,0,0,0,2,0,0,0,2,0,2][2,0,2,2,2,2,2,2,2,2,2,2,2,2,0,2][2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2]
```

[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]]→[A]

The 0's represent the empty spaces that nothing will be displayed on showing it is walkable.

The 2's however, are non-walkable positions (I will explain more when we get to the movement engine itself)

And the 1 is the "target" location, which is walkable (I will explain how that is walkable and how to adjust it later also)

Now onto the next area: Displaying the data so that the matrix is visible

This is where you tell the program how to display the matrix, since I have the "For(A,1,8)" before the "For(B,1,16)" it is going to display Left to right, 1 row at a time, now if A was after B, It would display Up to down, 1 column at a time.

**Code:**```
:For(A,1,8)
```

:For(B,1,16)

This right here is telling the program which position in the matrix it is looking at to see what number it is on.

**Code:**`:[A](A,B)`

This is going to skip over and 0's it finds so that it renders faster, which is mainly what the last segment I pointed out is for.

**Code:**`:If Ans≠0:Then`

Now this is the fun part, and I am going to break it down a bit.

This is going to display on the current location on the Homescreen, and is equivalent to the location in the Matrix.

**Code:**`:Output(A,B`

This is telling the Output() what character to display depending on the number in the current position of the matrix.

**Code:**`,sub("WX",[A](A,B),1)`

Now I will also explain the sub() command for those who don't know how it works.

sub() takes a Strn or text inside quotes, like in this code "WX"

so sub("WX", is going to take the number inside of the current location of the matrix, which is the [A](A,B) bit in it, and in the order of the string, so "WX" is kinda like in the Matrix "12", while the 1 followed by the matrix is telling it to use only 1 character from the string.

This is just closing all the loops that are running as they finish.

**Code:**```
:End
```

:End

:End

Part 2: The rest of the movement code

Here is where it all starts to work out.

**Code:**```
:Output(D,C,"θ")
```

:Repeat [A](D,C)=1

:getKey→K

:If K≠0:Then

:Output(D,C," ") //1 space

:C-((K=24)([A](D,C-1)≤1))+((K=26)([A](D,C+1)≤1))→C

:D-((K=25)([A](D-1,C)≤1))+((K=34)([A](D+1,C)≤1))→D

:Output(D,C,"θ")

:End

:End

:ClrHome

:Output(1,4,"YOU WIN")

:Pause

:ClrHome

:Return

This is going to put you in the starting location, which was defined in the last part.

**Code:**`:Output(D,C,"θ")`

This is a Repeat Loop, it only ends when the function specified is true, in this case, you standing on the spot where the number 1 is at.

**Code:**`:Repeat [A](D,C)=1`

**Code:**`:getKey→K`

If there is no button pushed, it will keep looping until there is one.

**Code:**`:If K≠0:Then`

Clears your space so that if you moved your guy, he will not stay there and move on, meaning only 1 space will show him at any 1 time.

**Code:**`:Output(D,C," ") //1 space`

If K is equal to 24(left) 26(right) 25(up) or 34(down) then it will move your character to according to the following algorithm, which I will explain.

I will break these up to make it easier to understand.

Takes your current location for C, and subtracts it from the answer, which I am about to explain

**Code:**`:C-`

Checks if you pressed left, if you did, it would return a 1, if not, a 0

**Code:**`((K=24)`

This checks to see if the space behind you on the matrix is less then or equal to 1, so if that space is a 1 or a 0, then if returns a 1, if not a 0

**Code:**`([A](D,C-1)≤1))`

so far, we have

**Code:**`C-(0 or 1)*(0 or 1)`

Adds the previous answer to the next set of conditions.

**Code:**`+`

Checks if you pressed right, if you did, it would return a 1, if not, a 0

**Code:**`((K=26)`

This checks to see if the space in front of you on the matrix is less then or equal to 1, so if that space is a 1 or a 0, then if returns a 1, if not a 0

**Code:**`([A](D,C+1)≤1))`

Stores your new location to C

**Code:**`→C`

So in a more simple form(won't work, but for visual aid) the code is pretty much

**Code:**`C-(0 or 1)*(0 or 1)+(0 or 1)*(0 or 1)→C`

Now for the second movement conditional.

**Code:**`:D-((K=25)([A](D-1,C)≤1))+((K=34)([A](D+1,C)≤1))→D`

Takes your current location for C, and subtracts it from the answer, which I am about to explain

**Code:**`:D-`

Checks if you pressed up, if you did, it would return a 1, if not, a 0

**Code:**`((K=25)`

This checks to see if the space above you on the matrix is less then or equal to 1, so if that space is a 1 or a 0, then if returns a 1, if not a 0

**Code:**`([A](D-1,C)≤1))`

so far, we have

**Code:**`D-(0 or 1)*(0 or 1)`

Adds the previous answer to the next set of conditions.

**Code:**`+`

Checks if you pressed down, if you did, it would return a 1, if not, a 0

**Code:**`((K=34)`

This checks to see if the space in below you on the matrix is less then or equal to 1, so if that space is a 1 or a 0, then if returns a 1,

if, not a 0

**Code:**`([A](D+1,C)≤1))`

Stores your new location to D

**Code:**`→D`

So in a more simple form(won't work, but for visual aid) the code is pretty much

**Code:**`D-(0 or 1)*(0 or 1)+(0 or 1)*(0 or 1)→D`

Places you on the new location.

**Code:**`:Output(D,C,"θ")`

Closes the If K≠0:Then loop

**Code:**`:End`

Closes the Repeat Loop.

**Code:**`:End`

This is what will display if you get your character to the 1 in the matrix, which will make the Repeat function true, and go to the next line after that Loop is closed.

**Code:**```
:ClrHome
```

:Output(1,4,"YOU WIN")

:Pause

:ClrHome

:Return

There is my first tutorial, and I hope this help many of you sometime. :D