If you wish to return to the top of this page,
click the little blue arrow thingys that look like this
.
Chapter Nine: Random Numbers
Chapter Ten: Lists
Chapter Eleven: Matrices
Chapter Twelve: Reading Keypresses
Chapters 9-12 Review
Back to tutorial index | Part Two | Part Four
What would a game be without random stuff? I'll tell ya, it'd be the same every time you played it, and it would get old, then it would suck, and you get the picture.
A randOM THOUGHT
So here's how to generate simple random numbers. Use the rand command, found under MATH \ PRB \ 1. In case your mental capacity isn't exactly on par with ours, you get the MATH menu by pressing [MATH]. That's it, just put it in on the home screen (not in the program editor), and press [ENTER].
You'll probably get something like this:
.7925631847
It's a random number between 0 and 1 (including both 0 and 1). If you want, you can specify a number of random numbers to generate in parentheses after the rand command (hehe, I made a rhyme!), like this: rand(5) and you'd get a list of that many random numbers.
Also, if you want a random number like that between 0 and some other number, like 7, you could do this: rand*7
Well now that you've got your nifty random number, what the heck do you do with it?
First thing you
do is put it in a variable like this: rand->R
Then, just use some If/Thens to determine what to do depending on what number was generated. Let's say that you wanted the calculator to randomly pick "YES" or "NO" like a magic 8-ball. Here's the "magic 8-ball" program:
:rand->R
:If R<.5:Then
:Disp "YES"
:Else
:Disp "NO"
:Stop
Basically this program takes the random number, and if it is less than .5, display "YES", and if it is equal or greater than .5, display "NO". So you have a 50/50 chance.
Matt: Will I get a girlfriend this year?
TI-83: MAYBE
Matt: Wait a sec... I didn't put that in the program!
RANDOM INTEGERS
So maybe that's fine, but what if you want whole numbers? Let's say you want to make a guessing game, where you have to guess a number from 1 to 20.
So that means you need a random number command that won't give you all these crazy decimals. The solution? Use the randInt( command! Now where can you find that? Somewhere near rand, right? It's at MATH \ PRB \ 6. If you've been really obsevant then you'll have probably guessed that PRB stands for probability.
So here's how it's used: randInt(low-number,high-number[,number-of-random-numbers]). low-number is the smallest number that can be picked. high-number is the biggest number that can be picked, and if you want, you can tell the calc to make multiple random numbers by adding an extra argument (number-of-random-numbers). This works just like it does for regular old rand. If you don't specify a number-of-random-numbers, one random number will be picked.
IMPORTANT NOTE: I put number-of-random-numbers in square brackets for a reason. This means it's optional. DO NOT TYPE THE BRACKETS!!! I will use this notation from here on in.
So here's the guess-a-number-from-1-to-10 program:
:0->R
:randInt(1,10)->R
:Disp "I AM THINKING OF","A NUMBER FROM","1 TO
10"
:Repeat G=R ----- Loop over and over
until the correct number is found.
:Repeat G>1 and
G<10 ----- Keep asking for a
guess until the number is in the valid range (1 to 10).
:Input G
:iPart(G)->G ----- You've seen this
before. If someone is stupid enough to enter a number with a decimal,
just chop off the decimal. There's a cool word for this:
truncation.
:End
:If G<R:Then
:Disp "TOO LOW"
:Pause
:End
:If G>R:Then
:Disp "TOO HIGH"
:Pause
:End
:End -----
You should be able to figure this out. When the calc gets to
End, it tests for G=R, and
if it is, then exit the loop and display "CORRECT!"
:Disp "CORRECT!"
:Stop
Here's the
program: 
And that's pretty much all you need to know about random numbers. There are other random commands, but I either don't know what they are or they won't be of much use in a game.
Now we get to learn about cool stuff that's really handy but isn't used that often. Lists. (mmm... lists...)
WHAT THE HECK IS A LIST?
A list is simply a variable that holds multiple numbers, just like a list of numbers (wait... that sounds stupid, I take that back) You might have used lists on your calculator in algebra class sometime, that is, if you paid attention, you damn slacker!
There are 6 "L" lists, and they are, you guessed it, L1, L2, L3, L4, L5, and L6. You can also have an unlimited number of custom lists, each with a name of up to 5 characters, for example, LNAME. You'll understand once we get into the specifics.
HOW DO I USE LISTS?
Right now you're probably be thinking this? Why do I need these to make a game? Well let's say you have 10 enemies on the screen. Instead of using 10 letter variables to store say, their damage level, you can put each one in a list. Each number in a list is a list element. You can perform math on an entire list or a single element only.
Before you use a list, you must dimension it, meaning make space for it in the calculator's memory. So if, for example, you wanted to have 5 elements in L1, you would use the dim( command, which can be found under LIST \ OPS \ 3. You get the LIST menu by pressing [2nd] [STAT]. You must use it like this:
number-of-elements
->
dim(list), so for our example you
would use the code 5->dim(L1). Oh, and by the way,
you can type L1 through L6 by pressing
[2nd] [1] -
[6].
There is a maximum of 999 elements per list.
Then type L1 by pressing [2nd] [1]. You should see a screen like mine:

That {0 0 0 0 0} is your list. Lists are enclosed by brackets and their elements are displayed with spaces between them.
HEY! MY LIST HAS NUMBERS IN IT INSTEAD OF ZEROES! (or, HOW TO CLEAR A LIST)
If the list that was displayed had numbers in it other than five zeroes, the list already has stuff in it and must be cleared, or in other words deleted. If you need the list data, you should write it down or transfer it to another list by typing L1->Lx, where x is the number of a free list.
Anyway, to
clear a list, set its dimensions to 0, by typing 0->dim(L1). Now type L1 and you should get an
ERR:UNDEFINED message. This is good, the list no longer exists and
can be used.
HOW TO PUT STUFF IN A LIST
OK, five zeroes makes a stupid list You need stuff in it, right? Luckily, storing numbers into a list is easy. Just type all your list elements, separated by commas, inside curly brackets { }. To type the curly brackets, press [2nd] [ ( ] (left parenthesis) for an opening curly bracket or [2nd] [ ) ] (right parenthesis) for a closing bracket, and then store it to a list variable.
Here's an
example: {1,2,3,4,5}->L3. You can probably
figure out what that means. Now you've got numbers in your list!
Another way of erasing a list is writing over it , so if there was
already stuff in L3, it would all be erased
and the new data would be put in it. The dimensions of the list are
also changed to the number of elements in the new list that you put
in, blah, blah, blah...
DOING MATH WITH LISTS
Although you probably won't need to use this in a game, you can do math with lists like normal variables, but their dimensions must be the same! If you try an add a 3-element list and a 7-element list, you'll get a DIM MISMATCH error.
You can also use lists in normal arithmetic, so you could do 3+L1 and you'd get a list of all the elements in L1 plus 3. However the new number will not be stored to the list unless you tell it to.
CUSTOM LISTS
Yeah, lists may be useful, but L1 through L6 are used very frequently. TI has been kind enough to provide us with the ability to make lists with custom names up to five letters long, theoretically providing the user with an unlimited number of lists (technically 71,270,167 lists, but that's pretty close to unlimited and no TI-83 can hold 71 million lists). And since they have nothing to do with L1-L6 but can do exactly the same things as them and are used in the exact same way, they're perfect for programming, unless you have a fetish with lists whose name contains an L and a subscript number or like pissing people off that keep stuff in them.
Here's how to make a custom list:
Way #1:
Define
a list by typing the numbers in brackets separated by commas (ex.
{1,2,3,4,5}), then press
[STO>] to get the store
symbol, and type a name for the list that is one to five letters
long. Lists must follow the same naming guidelines as programs (see
Chapter 1: Getting Started).
Way #2:
Set the
dimension for the list (dim the list), as explained in "How Do I Use
Lists?" (remember? x->dim(Lxxxxx). However, it must be
preceded by the list symbol, which looks like a little L
(L), telling the
calculator that this is a list. You can type L by getting into the
LIST menu by pressing [2nd]
[STAT]. Its
precise menu location is LIST \
OPS \ B
(way at the bottom!)
Way #3 (this
way cannot be used within a program)
Create
the list in the nifty little stat editor. Press [STAT] and select 1:Edit.... Then press
[up arrow] so the list names are
highlighted. Scroll to the right by pressing [right arrow] until you
get to the empty list column and Name= is displayed in the
bottom of the screen. Press [ENTER], type the name, press
[ENTER] to confirm the name,
and enter your data in it like you'd do with any other list in the
stat editor.
Unfortunately, if you want to create a list within a program, you'll have to use one of the hard ways, #1 or #2.
If you want to access a custom list (use its name in a program), there's a real easy way to do this, assuming the list has already been created. And best of all you don't have to search through the menus to find that damn L.
Press [2nd] [STAT] to get the LIST NAMES menu. Just select the name of your list from the um... list.
Feast your eyes on these lovely screen shots:


MANIPULATING LIST ELEMENTS
All game programmers listen up good! If you use lists in a game, you'll need to know how to screw around with each individual element.
If you want to access a particular element in a list, add the element number in parenthesis after the list name. Here's an example of how to access the first element in the list LMATT: LMATT(1). The first element in LMATT is 9, right? So if I typed this in I'd get the number 9.
Don't type in an element number that is outside the range or you'll get a nasty INVALID DIM error message.
Now I will talk about the significance of lists in programming. A list is technically called an array, or to be more precise, a one-dimensional array. I've already mentioned this, but if you want to have multiple enemies, for example, in your game, you can put, for example, their shield level, in a list. So the first element would be enemy #1's shield, the second would be enemy 2's shield, etc. There are at least two benefits of this way. You don't have to waste all those letter variables, and, when coupled with a For( loop, you've got one of the greatest structures in programming ever. Let's say you've got 10 enemies and you want to draw them all. Each enemy's horizontal position is an element in a list. Instead of having to write the drawing commands ten times, you can use a For( loop and only write the drawing commands once. This significantly improves efficiency and probably sounds confusing to you, but it won't after you see this example.
:ClrHome
:0->dim(LX)
:{8,1,4,10,5,14}->LX
:For(I,1,dim(LX) ----- If you use
dim( by itself, it returns
the number of elements in the specified list.
:Output(4,LX(I),"*"
:End
Try writing this program and running it. You should see some stars on your screen in a row. The list LX contains the horizontal coordinates of the stars. This is how the For( loop works:
The number of
times it does the stuff is the number of elements in LX, or, more simply, how
many stars there are. I is the counter variable.
Then, when the program goes to draw the star, it reads the horizontal
coordinate in the element specified by I, and displays a star at that
position. So each time around a star is placed in the column
corresponding to the number in the element specified by I.
I'm sorry if that was confusing. I don't know how to explain it that well but it works.
Go ahead and mess with LX. Change elements, add elements, remove elements. Try putting a Pause before End. Soon you'll get it.
Here's the output of that program as it is written. Anything in red was never on the screen and is used for clarification.

LIST MATH OPERATIONS
Handy math operations you can do with lists that sometimes come in handy in game programming.
These commands can be found in the LIST OPS menu, by pressing [2nd] [STAT] [right arrow].
Fill(value,list)
Replaces
every element in list with value. The changes are
automatically stored to the list.
SortA(list)
,
SortD(list)
SortA( puts
the elements in list in order from lowest
value to highest value (ascending order, hence the "A").
SortD( puts the elements in
list in order from highest
value to lowest value (ascending order, hence the "D").
The changes made by both commands are automatically stored back into
the list.
dim(list)
Returns the
number of elements in list.
These commands can be found in the LIST MATH menu, by pressing [2nd] [STAT] [right arrow] [right arrow].
min(list)
,
max(list)
min(
returns the value of the smallest element in list.
max( returns the value of
the largest element in list.
mean(list)
,
median(list)
mean( returns the mean
(average) value of list.
median( returns the median value
of list.
sum(list[,start-element,list-element])
,
prod(list[,start-element,list-element])
sum( returns the sum of all
the elements in list. If you want you can
specify a range of elements to add by using start-element and end-element.
prod( returns the product of
all the elements in list. If you want you can
specify a range of elements to add by using start-element and end-element.
augment(listA,listB)
augment(
takes listB and tacks it onto the
end of listA. There's a really big
and cool word for this: concatenation.
DELETING LISTS
Now that you know how to create lists and use them, now it's time to learn how to get rid of them.
The ClrList command clears the specified list. Examples: ClrList L6, ClrList LMATT. If you try to view the values of a cleared list you will get an INVALID DIM error. It can be found by pressing [STAT] to get the STAT EDIT menu. Its menu location is STAT \ EDIT \ 4.
The DelVar command permanently deletes the specified variable from memory. You can use this for any kind of variable: letter variables, lists, pictures, etc, but NOT PROGRAMS. Examples: DelVar L4, DelVar LMATT. Its menu location is PRGM \ CTL \ G.
ClrAllLists sets the dimension of lists L1 - L6 to 0, effectively clearing them. Custom lists are not affected.
OK. Now hopefully you have some understanding of lists. The next chapter will answer that age-old question: What is the matrix?
Let me get this straight. As you know, the singular of matrices is matrix. Although that movie was awesome, this has nothing to do with it.
A matrix, as
you might know from algebra class (if you pay attention), is a way of
organizing numbers. It's kind of like a table. This is a matrix:
Each number in the matrix is also called an element, just like with a list. It also has dimensions, just like a list, but there are two of them, one for the number of rows and one for the number of columns. Matrix dimensions are indicated like this: number-of-rows x number-of-columns. So the matrix above is a 4x2 matrix. It has four rows and two columns of elements.
There are ten matrices available for use on the TI-83 and TI-83 Plus, named [A] through [J]. The maximum dimensions of a matrix are 99x99, although that matrix would be so big it would fill up almost two calculators' worth of memory! Matrices are very big, so if you create a matrix from within a program, be sure to delete it afterward unless you really need it.
WHY DO I NEED MATRICES IN PROGRAMMING?
If you're making a game, like an RPG or graphical adventure game, they come in quite handy for making level maps. And also, if you have the patience to wait, they can also be used for making sprites (animated characters made of pixels), but since the BASIC interpreter is quite slow, you might as well forget about sprites.
Matrices are the same as two-dimensional arrays in other programming languages. If there's any situation where you need a two-dimensional array, by all means use a matrix.
HOW DO I CREATE A MATRIX?
Matrices can be created in one of two ways: from the home screen or in a program, which can get somewhat ugly, or through the nifty matrix editor. I will explain both ways.
From the home screen or in a program:
This is kinda complicated, so try and stay with me. The first thing you need is a left square bracket, which looks like this [ . You can type one by pressing [2nd] [x]. This signifies the start of a matrix. Then, one by one, type in each row, enclosed in square brackets and separated by commas. Then type a right square bracket to signify the end of the matrix. You've probably already figured this out, but you can type a right square bracket by pressing [2nd] [-].
So, if you wanted to enter the matrix above, you'd type [[3,2][-1,10][.5,0][-324,9]].
NOTE: WHEN TYPING A NEGATIVE SIGN, DO NOT PRESS MINUS [-]! YOU MUST USE THE NEGATIVE KEY [(-)] , OR THE CALCULATOR WILL REAM YOU ON SYNTAX.
Here's a screen
shot to help you:
Now that matrix
is nothing. If you want to use it, you'll have to store it to one of
the matrix variables, [A] through [J]. So type your little store
symbol, ->, then one of [A]
through [J]. DO NOT TYPE EACH
CHARACTER BY ITSELF! IF YOU TYPE A LEFT BRACKET, THEN A LETTER, AND A
RIGHT BRACKET, YOU WILL GET A SYNTAX ERROR! YOU MUST USE THE
FOLLOWING METHOD. The fine folks at TI
have added a designated matrix button to the keypad, [MATRX]. For those who use a
TI-83 Plus, you will have to press [2nd]
[x-1]. Either way, you should
come up with the MATRX
NAMES
menu:
. Variables
with dimensions next to them ([A] and [B] in this example) are
matrices that have been defined already. The rest are free. You can
overwrite matrices.
So just select the matrix you want to use from this menu. And be sure you are in the NAMES submenu, and not EDIT!
If you wanted
to store the above matrix to [C], you'd type this:
If you wanted to create a matrix from the program you would use the same method.
From the Matrix Editor:
This way is much easier. Get into the MATRX menu using the appropriate method depending on your calculator (see above). Then press [right arrow] twice so that EDIT is highlighted. Now select the matrix you want to edit and press [ENTER]. You should now get this spiffy matrix editor up on your screen:

This is the editor for matrix [D]. The cursor should be positioned on top of the row dimension (the first number "1"). Enter the number of rows and press [ENTER]. The cursor should now be over the column dimension. Type the number of columns and press [ENTER]. Now you can edit the elements in your matrix. You might have to scroll down or to the right if the matrix is too big. When you have highlighted an element, begin typing the number and you should see it at the bottom of the screen. Press [ENTER] when you are finished with that element. Press [2nd] [MODE] (QUIT) to exit the editor. Changes are saved automatically.

NOTE: If you write a program that requires an already-made matrix (or list, or whatever) to run, be aware that if you send it to someone you will have to send them the matrix (or list or whatever) along with the program or your program will spit out an ERR:UNDEFINED or screw up because the data in that matrix on that calculator is bad. That's why it's good programming practice to create these necessary things in the program and delete them when it is done to reclaim the used memory.
DOING MATH WITH MATRICES
You can perform math on matrices. You can add or subtract two or more matrices, given they all have the same dimensions. You can perform scalar multiplication on a matrix by typing number*[matrix]. There are other ways to do multiplication and other operations but I haven't learned them in algebra yet.
You can also use these handy commands:
These can all be found in the MATRX MATH menu.
matrixT
T
(transpose)
rearranges the elements of a matrix so the elements in the first row
are in the first column, the elements in the second row are put in
the second column, etc. Therefore the dimensions reverse, because the
rows become columns and the columns become rows. So a 2x3 would be a
3x2 matrix, etc. Changes are automatically stored back into the
matrix.
dim(matrix)
dim( returns the dimensions
of matrix in a list. The list is
in this format:
{rows columns}. If you want to extract
the row dimension, store the result to a list and then take element 1
of that list (type list(1)). To extract the column
dimension, do the same, but use element 2.
{rows,columns}->dim(matrix)
This is
used for setting the dimensions of a matrix, or creating a new
matrix. It can be used the same way as the dim( command for lists, but
you must provide a mini-list which contains the number of
rows and columns in the matrix.
Fill(value,matrixname)
Fill( replaces every value in
matrixname with value. Changes are
automatically stored back into the matrix.
randM(rows,columns)
randM( creates a matrix whose
dimensions are rows,columns and fills it with
random integers from -9 to 9. If you want a
wider range you can perform scalar multiplication on it afterward if
you wish. It does not store the matrix to a variable.
augment(matrixA,matrixB)
augment(
tacks matrixB onto the end of
matrixA as new columns. The two
matrices must have the same number of rows. No changes are made to
the matrices in memory.
rowSwap(matrix,rowA,rowB)
rowSwap( returns a matrix with
rowA swapped with
rowB. The changes are not
saved to the matrix.
These are a few of the matrix commands. There are more, but I either see no use for them or don't understand them.
MANIPULATING MATRIX ELEMENTS
If you haven't already, go back and read the section "Manipulating List Elements" in Chapter 10. It explains what accessing individual elements is for. The indication of a matrix element is almost the same, it is the row and column separated by a comma in parentheses after the matrix name. Example: [A](1,5) returns the element in the first row and fifth column of matrix [A].
However, if you want to do the For( loop thing, you will need to use two nested For( loops. Consider the following code:
:For(R,1,4)
:For(C,1,6)
do stuff with matrix element [A](R,C)
:End
:End
You get that? R is the row counter and C is the column counter (ooh... alliteration). What this does is that inside the R loop it does the stuff in the C loop 6 times, then goes back to the beginning of the R loop, increments R by one, and does C from 1 to 6 again, etc.
An example of this method will be in the review after Chapter 12.
DELETING MATRICES
Matrices are deleted with DelVar just like a list or letter variable. NOTE: If you are not in the program editor, you will have to get DelVar from the catalog. The catalog is a list of every command on the calculator. You get into the catalog by pressing [2nd] [0] (CATALOG). Then press [ALPHA] [D] to jump down to the D's, and select DelVar.
OK. An action game would really suck if you had to type in what you wanted to do and press [ENTER] every time. Ever wonder how those guys make games that use the arrow keys to move around, and [2nd] to fire, and all that good stuff? Now you're gonna learn!
IT'S TIME FOR getKey!
getKey is a command that returns the key code of any keys that were pressed at the time the calculator got to it. You must use getKey in a loop. By the way, it can be found at PRGM \ I/O \ 7. It is normally stored into a variable, the most typical of these being K. Make a new program and put this code into your calc:
:ClrHome
:While 1
:getKey->K
:If K/=0
----- Remember /= ? It's the not-equal-to sign. See chapter 5 for
details. And getKey returns 0 if no keys
were pressed.
:Disp K
:End
So play around. Press different keys and look at the numbers that come up. Note that you will have to press [ON] to end this program.

Now you might
be wondering, what do I do with those numbers? Well here's what you
do. Inside a loop in your program, you make a keyboard
handler, which is a routine
that handles keypresses and does the appropriate action. It's
basically a getKey->variable
followed by
a bunch of If/Then/End blocks.
(It's too bad they don't have anything like a C++ switch statement or a QBasic SELECT CASE statement! Darn, now I'm mixing in other programming languages!)
Anyway, here's an example:
:While 1
:getKey->K
:If K=24:Then
stuff-to-do-if-LEFT-is-pushed
:End
:If K=26:Then
stuff-to-do-if-RIGHT-is-pushed
:End
:If K=21:Then
stuff-to-do-if-2nd-is-pushed
:End
:If K=45:Then
stuff-to-do-if-CLEAR-is-pushed
:End
:End
All set? Now we're gonna make a program! You're gonna want to show this one to all your friends! It displays a crosshair on the home screen that you can move with the arrow keys. Press [CLEAR] to quit.
:ClrHome
:5->R:9->C ----- R is the row
coordinate of the crosshair, C is the column coordinate of the
crosshair.
:Output(R,C,"+" ----- Display the
crosshair at the appropriate coordinates.
:While 1
:getKey->K
:If K=24 and C>1:Then ----- A check to make
sure the c.h. moves only if it won't go off the screen.
:C-1->C ----- Decrement the
column by one. (move left).
:Output(R,C+1," ") ----- Display a space
where the old c.h. was to clear it.
:Output(R,C,"+") ----- Draw the newly
moved crosshair.
:End
:If K=26 and C<16:Then
:C+1->C
:Output(R,C-1," ")
:Output(R,C,"+")
:End
:If K=25 and R>1:Then
:R-1->R
:Output(R+1,C," ")
:Output(R,C,"+")
:End
:If K=34 and R<8:Then
:R+1->R
:Output(R-1,C," ")
:Output(R,C,"+")
:End
:If K=45
:Stop
:End
I don't need to show you the output of this one. It's a plus sign + that moves around the screen.
If you can run this program, and you fully understand the code, CONGRATULATIONS! You're now ready to make a full-blown game!!!
Yes, as mentioned before, now you are going to make a real live action game. I call it COLLECTOR, and the object is to move around the level and collect all the stars. It is played on the home screen. The stars are asterisks *, the walls are exclamation points !, hyphens -, and plus signs +, and the player is a theta O.
All right,
partner.
Keep on rollin', baby.
You know what time it is.
PROGRAM:COLLECT
The first thing we're gonna have to do is define the level map. We will use matrix [J] because it is not commonly used.
COPY THIS EXACTLY, AND PUT THIS ALL ON ONE LINE!!!
:[[0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0]
[0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0]
[0,1,0,2,2,2,2,3,0,0,0,0,0,0,0,0]
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0]
[0,0,0,0,0,0,0,0,3,2,2,2,2,2,3,0]
[0,0,0,0,3,2,2,0,1,0,0,0,0,0,1,0]
[0,1,0,2,3,0,0,0,0,0,0,0,0,0,1,0]
[0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0]]->[J]
My God! That was long!
Now this next bit of code we write is called the level parser. It takes that matrix, analyzes each element, and puts the right symbol in the right place. In other words, it translates and draws the level. Note: 1 is an exclamation point, 2 a hyphen, and 3 a plus sign.
:ClrHome
:For(I,1,8)
:For(J,1,16)
:If [J](I,J)=1
:Output(I,J,"!")
:If [J](I,J)=2
:Output(I,J,"-")
:If [J](I,J)=3
:Output(I,J,"+")
:End
:End
What this code does is that on each time through it takes what is in the current element of [A], and checks to see if it is 0, 1, 2, or 3. Then it places the appropriate symbol on the screen at that location. Since this is an 8x16 matrix, and the screen is 8x16 characters, each element represents a character position on the screen.
Run that program, and see what you get. You should come up with this:

You will only need to draw the level once in this entire program. It would take half of forever if you had to.
Now we will need to set up some variables.
:1->R:1->C ----- R and C are once
again the row and column of the player. We wanna put him in the upper
left corner of the screen.
:0->S ----- S is the number
of stars you've gotten so far.
:18->dim(LSTARS) ----- STARS is the list
that stores the stars data. Since there are 6 stars, each one gets 3
elements. The first is either 0 or 1 and tells if the star is on the
screen. The second is the row, the third is the column.
Now we will draw the stars on the screen.
:For(I,1,16,3)
:1->LSTARS(I) ----- What this loop
does is load a 1 in every third element. This is the element that
says if the star is on the screen.
:End
:For(I,2,17,3) ----- This loop loads
the coordinates of each star into the list.
:Repeat [J](M,N)=0 ----- This is a nested
loop that makes sure none of the stars are placed on a
wall.
:randInt(1,8)->M ----- Load random
coordinates into the temp variables M and N.
:randInt(1,16)->N
:End -----
Since this is a Repeat loop, it tests the
condition at the End. If the coordinates
picked are on a wall, pick new ones.
:M->LSTARS(I) ----- If the
coordinates are good, load the row into element I.
:N->LSTARS(I+1) ----- Then load the
column into the element after I. This saves us from
having to do another For( loop.
:End
:For(I,1,16,3) ----- This loop will
draw the stars on the screen.
:If LSTARS(I)=1 ----- If the star is on
the screen (element I is a 1) draw
it.
:Output(LSTARS(I+1),LSTARS(I+2),"*" ----- Draw the star.
Remember the row is offset 1 element from I, and the column is
offset 2 elements from I.
:End
You can run this program as many times as you want but I guarantee you you will never see a star on top of a wall.
Now we're going to start making the main game loop and make the little dude move.
:Output(R,C,"O" ----- You type the
theta by pressing [ALPHA]
[3].
:While 1
:getKey->K
:If K=24 and C>1:Then
:If [J](R,C-1)=0:Then ----- Check the
destination of movement against the corresponding matrix element and
make sure there isn't a wall there.
:C-1->C
:Output(R,C+1," ")
:Output(R,C,"O")
:End
:End
:If K=26 and C<16:Then
:If [J](R,C+1)=0:Then
:C+1->C
:Output(R,C-1," ")
:Output(R,C,"O")
:End
:End
:If K=25 and R>1:Then
:If [J](R-1,C)=0:Then
:R-1->R
:Output(R+1,C," ")
:Output(R,C,"O")
:End
:End
:If K=34 and R<8:Then
:If [J](R+1,C)=0:Then
:R+1->R
:Output(R-1,C," ")
:Output(R,C,"O")
:End
:End
:If K=45:Then
:DelVar [J]
:DelVar LSTARS
:Stop
:End
You're probably gonna be shocked when you see this program running. Try as much as you want, but the guy will never go through a wall!!! He can walk around just like in real life!
OK. Now we're gonna make the collision detection routine, which checks to see if the player has run into a star.
Delete the End at the end of our code so far.
:For(I,1,16,3) ----- The
collision-checking loop.
:If LSTARS(I)=1:Then ----- If the star
hasn't been collected...
:If R=LSTARS(I+1) and C=LSTARS(I+2):Then ----- ...then test to
see if its coordinates are the same as the player's.
:S+1->S
----- If, so, add one to the star count.
:1->LSTARS(I) ----- Then turn that
star off.
:End -----
End of the coordinate-checking If block.
:End -----
End of the star-collected checking If block.
:End ----- End of the For( loop.
:If S=6:Then ----- If all the stars
have been collected...
:Output(4,5,"YOU WIN!" ----- Display "YOU
WIN!",
:DelVar [J]
----- delete our matrix and our list to reclaim that precious
space,
:DelVar LSTARS
:Stop -----
and stop the program.
:End -----
End of the all-stars-collected If block.
:End -----
End of the While loop, and end of the
program!
