HOME >> Tutorials >> Tutorial 3: Nibbles Game -- Lists
Advanced Game Techniques Tutorials
Advanced BASIC
Tutorial 3
Nibbles Game: Lists

   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 first two tutorials is not required to read through this tutorial. But is recommended as features talked about previously are used in this tutorial without extensive documentation.

Part 2: Intro to the Genre

Nibbles?
Anyone who claims to be an avid calculator gamer knows what nibbles is, but for all the other people... One of the staples of a good archive of games has always been the MoFrog/Nibbles games. I have no idea who came up with this gamming genre, but whoever did is very smart. The general idea is that you have this moving snakelike figure that can grow longer, therefore making it harder to hit the dot or whatever that increases your score. Variations can be thrown in, like obstacles or whatever. If you have never played a MoFrog/Nibbles game then go to www.ticalc.org and download MoFrog for the TI-82.

How is that Possible?
As you have probably guessed (or by reading the title of this tutorial), the secret is arrays. It will be kind of hard to explain but I will try. This is your basic snake:

*****

It is 5 spaces long and as it moves, a new '*' appears and the one on the end dissappears. For the calculator to know this it has to remember the coordinates of every space of the snake. To do this you simply store the vars into two different lists. One for the X-values and one for the Y-values. To explain, here ia a basic nibbles engine, I will explain it after the following:

TI CODE EXPLANATION & DESCRIPTION
ClrHome  
1->X:1->Y
1->S:0->T
We have to know which direction the darn thing is moving. If you look at your calculator keys you will see that S is above X and T is above Y. So, S is change in X, and, T is change in Y. We start out with the snake in the upperleft, moving right.
2->C:1->L
To start out, the position of the current (leading end of the snake) space is the second space in the list. It is one space long. As the program cycles through the loop, C is upped by one, moving the leading end forward. Example with a snake that is 5 long: *****

values in list:
[1234567890123456789012345] - list is 25 spaces long
X-1234500000000000000000000
Y-1111100000000000000000000
leading end [x=5, y=1] [C].
end [x=1, y=1] [C-L].

After it has moved three cycles through main loop: [C+3->C(and list has been updated)
___*****
X-1234567890000000000000000
Y-1111111110000000000000000
leading end [x=?, y=?] [C].
end [x=?, y=?] [C-L].

As you can see, as C increases, the value accesed to be the address of the blank space moves along with the value of C, creating the allusion that the whole snake is moving along.
25->dim(L1
25->dim(L2
These statements simply setup the length of the list.
1->L1(2):1->L2(2)  
1->L1(1):1->L2(1)  
While (X<17)(X>0)(Y<9)(Y>0)
This is exactly the same as saying [While (X<17) and (X>0) and (Y<9) and (Y>0)], it just saves a few bites of memory.
Output(L1(C),L2(C),"*"
This outputs the leading edge of the snake. Note most would write it:
L1(C)->A:L2(C)->B
Output(A,B,"*"
But this would slow down the loop considerably because it is making 2 unnecesary storages each loop.
(C-L)->Z  
If Z<1:Z+25->Z
This prevents an "ERR:INVALID DIM" in the following case:

[1___________________]
[____******_________]
[_________*__________]
[_________*__________]
[_________*__________]
[_________*__________] leading edge
[____________________]
[____________________]
Y-6000000000000002222223456
X-80000000000000056789vvvvvv[v=10]
leading end [x=?, y=?] [C].
end [x=?, y=?] [C-L].

Here C-L would give you -9, which would give you a "ERR:INVALID DIM".
Output(L1(Z),L2(Z),"_"
This deletes the end of the tail.
getKey->K  
If K:Then
As addresed in tutorial one, if it is not 0, it evaluates to true. This is the same as saying [If K>0].
(K=26)-(K=24)->S
(K=34)-(K=25)->T
As addresed in tutorial one, this if K=26, puts a 1 in S (moves right) and stores a 0 in T (there is no vertical movement. This is just an optimised way of getting the key value.
(K=21)+L->L
This is simply for experiments sake, it allows you to extend the length of the snake.
End  
C+1->C  
If C=26:1->C
This keeps the value of C from going into infinity. Note that if you left this statement out, the program would run, but the size of L1 and L2 would grow until you ran out of memory. It has to work because if you go into Stat, Edit.., you can extend the length of the program.
X+S->X:Y+T->Y
This updates the leading edge of the snake with the values that it got the last time you pushed a key.
X->L2(C):Y->L1(C)
This is the key to the movement, it stores the value of the edge for use when the tail gets to that spot and needs to be errased.
End  

Overview:
Run the code. As you see, the Snake thing moves around. It is only 1 space long (even though it looks like two). When you push a 'Control key' it changes direction. If you press 2ND, then an arrow key, it will be one space longer. If you hit 2ND more than 24 times, you will get an error, this is explained above.


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

As this tutorial has become quite big, I will save the hit detection part until the next tutorial.

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.