HOME >> Tutorials >> Tutorial 4: Nibbles Game -- Hit Detection
Advanced Game Techniques Tutorials
Advanced BASIC
Tutorial 4
Nibbles Game: Hit Detection

   Advanced BASIC Tutorials

Part 1: Introduction
First of all, your probably wondering what exactly advanced programming is. For our purposes, we can define advanced programming as the ability to use all the available facits of the TI-Basic to create quality and technicaly complex games. In order to do this you must understand how the calculator uses the commands on a very low level. To use TI-Basic to make quality games you have to know what your doing. Since ASM programming is very fast, you have to be able to program very intelligently to make quality games that will compete with them. The purpose of these tutorials is to explain several advanced programming techniques. By starting at game design and ending at 3-D programming. It explains mainly the theorys behind these techniques and not the exact syntax of things.

For these tutorials it is assumed that you already know how basic basically works. If you don't know the syntax and how to use things like 'If' statements or 'Lbl's' then you should go read the many other Basic Tutorials out there on the web.

Reading of the previous tutorial is not required to read through this tutorial. But is recommended as features talked about previously are used in this tutorial without extensive documentation.

Nibbles Hit Detection?
As everyone noticed after typing in the code from the last tutorial, the game was not complete. It still requried some hit detection. It's not right if you can run through yourself. How could we store all that data you ask? A matrix of course. We simply store one into a matrix which represents the screen and then we can know where the snake body and the piece of 'food' is.

Source Code:
Here is the source for the updated nibbles engine. As you will notice, most of it is the same as before, but with slight changes:

TI CODE EXPLANATION & DESCRIPTION
ClrHome  
3->X:2->Y  
1->S:0->T
2->C:0->L
 
50->dim(L1)
50->dim(L2)
 
{10,18}->dim([J])
This sets up the matrix, the reason it is bigger than the screen is explained in 2.
Fill(0,[J])  
Fill(0,L1)
Fill(0,L2
 
For(A,1,10)
1->[J](A,1)
1->[J](A,18)
End

For(A,1,18)
1->[J](1,A)
1->[J](10,A)
End

This sets up the matrix. Here is an ASCII representation of the matrix:

111111111111111111
100000000000000001
100000000000000001
100000000000000001
100000000000000001
100000000000000001
100000000000000001
100000000000000001
100000000000000001
111111111111111111
Screen Bounds

The reason for this is to avoid costly if statements. Instead of having an if x>16 or x<1 or y>8 or y<0 statement. You can just use the statement that you use to check if you hit the snake to check if you went outside the bounds. This is just a technique to speed up the game just a little bit.
2->L1(2):3->L2(2)
2->L1(1):2->L2(1)
 
0->M  
While [J](Y,X)=0 or [J](Y,X)=2
This loop keeps the game going when you hit a piece of 'food'. It has to reset the current location to 0 so that the second while statement will evaluate to true. There is a better way of doing that but I can't give away all of my secrets...
0->M  
0->[J](Y,X)  
Output(1,1,L+1)  
While not([J](Y,X)  
If not(M:Then
Lbl 1
randInt(1,8)->J
randInt(1,16->I
If [J](J+1,I+1)
Goto 1
2->[J](J+1,I+1
Output(J,I,"+")
1->M:L+1->L
End
This is the block of code that creates the 'food' for the snake. It uses the M as a flag to see whether or not there is already piece of 'food' on the screen.
LABELS ARE PREVENTABLE
Don't say I'm hypocritical because I am using lbl's. The proper way to do that piece of code is with a DO WHILE statement. Unfortunately, the TI-83 does not offer that command. It then stores the location of the 'food' in the matrix and outputs it to the screen.
(C-L)->Z
If Z<1:Z+50->Z
Output(L1(Z)-1,L2(Z)-1," ")
0->[J](L1(Z),L2(Z)
Output(Y-1,X-1,"O")
1->[J](Y,X)
 
getKey->K
If (K>21):Then
(K=26)-(K=24)->S
(K=34)-(K=25)->T:End
If K=21:Pause
Determines if key is pressed.
C+1->C
If C=51
1->C
X+S->X:Y+T->Y
X->L2(C):Y->L2(C)
End
End
 
For(Q,L,1,-1)
(C-Q)->Z
If Z<1:Z+50->Z
Output(L1(Z)-1,L2(Z)-1," "
For(A,1,50)
End
End
This part is just for jazz, it starts at the tail and erases every piece of the snake.

Overview:
This is of course, longer. Since you understand the basics from the last tutorial I have skiped those.

If you read the last tutorial then everything else is pretty much self explanatory.

Game Release:
If you think that your ready to release a game when you just have the code complete, you have another thing comming. At this state the above code is in the code complete stage, but it still has to go through the content complete stage. Where you add menus, documentation, and all the stuff that makes a good game.


If you don't understand this stuff:
  1. Reread the tutorial.
  2. Reread it again (throughly, and experiment with the code)
  3. Then I can't write good tutorials, or
  4. You should rethink wanting to program Advanced BASIC

Next Tutorial: Scrolling Screen Left & Right.

Written by Brandon Green.

Tutorial is property of Brandon Green, © 2000. Do not use without permision. Algorithms are property of Brandon Green, e-mail for permision to use.





Problems with this page?
Contact the Webmaster.