Lesson 7

Animating the homescreen

In this lesson, we will learn how to make things move on the homescreen, by having the calculator randomly do things, or having the calculator gather input and then move stuff. First, we will make it look for input.


Up til now, I have just been giving you little snippets of code to work with, but here is where we will put it all together. This will show you how to make the letter X move across the screen.

Create a program, call it MOVEX. Insert the below code:

:While W=1
:If K=24 and B>1:Then
:Output(A,B," "
:If K=25 and A>1:Then
:Output(A,B," "
:If K=26 and B<16:Then
:Output(A,B," "
:If K=34 and A<8:Then
:Output(A,B," "

The above code will allow you to move an X around the screen, and keep it from going off the screen by the boundries that we programmed in. The boundries are B>1, A>1, B<16, A<8. These are just the screen dimensions, nothing special. Well, this is all fine and good, you say, but what if I want to make it go to another section of the map? Well, I will explain map loading and hit detection in the next tutorial. :)


One of the more useful commands that the TI-OS gives us is the ability to create a random number. There are several commands, each with its own purpose.


rand, by itself, will return a random number that is greater than 0, but less than 1. Basically, it gives you a random decimal number for whatever decimal mode that you have it set at. Normally, you should have it set to Float, but it is your calc, so you can have it set to what you want. Press MODE to set the decimal place, or you can press it within your program editor, and have the program set it as well. :)

If you want to change the values that rand outputs, simply add rand to an equation. You can use something like this: rand*100

This will produce a number between 0 and 100. You can also tell rand how many "trials", or random numbers you want it to create. This can be helpful if you are looking to get an average on random things. The values are stored into Ans in list form, and can then be saved to a list. To get this set up, use rand(20). This use in command is also fairly handy to use as a temporary pause in your game or program, simply change the 20 to any desired number, and the calculator will pause for a time while it calculates all the random values and stores them to Ans.


randInt( can be a very important command for programmers, as it gives us the ability to define the minimum and maximum values we want to search between, and can also do the number of trials.

randInt(low boundary,high boundary,optional trials amount

The above commands can be found in the math menu, by pressing MATH, and then <.

Bringing it all together

Now that we have seen how to get random numbers, it is time to create a program that will automatically move our little X-Man around the screen. To do this, we need to clear the screen, set up the variables that will place the X-man in the original place, and the code that will randomly give a direction.

There are two ways to accomplish this next feat. I will first show you how to do it in a simple way, then I will explain an advanced way of using the mighty If to cut some memory used, as well as speed up the overall game.

Create a program, call it anything you want. Below is the code for you to enter:
:DelVar θ
:Repeat getKey
:If θ>1:Then
:If θ=2 and A>1:A-1→A
:If θ=3 and A<6:A+1→A
:If θ=4 and B>1:B-1→B
:If θ=5 and B<8:B+1→B

Now run the above program! This should clear the screen, Display an X, and make it go crazy. If that is not what your program did, please make sure you put in the right code before askin on the forum. This is what you should see:

Here is a breakdown of the above code. You clear the homescreen; you set X to display in the top left corner; you delete the variable theta, just for good measure; You then start a loop that looks for key presses, it isn't a good thing to always exit out of BASIC programs using the ON key. The next thing you do is to display the X using the Output( command. Then this is where we use the randInt( command.

In this program, I set it to grab a random number from 0 to 5. Why? you might ask? It is simple, I want to give the program an oppurtunity to pause the X so you might be able to see it. If it selects 0 or 1 as the random number, the X will stay still. Selecting 2, and not on the upper boundary, will make the X character move up. 3 and not near the bottom will make it move down. 4 and not on the left side of the screen will let it move left, and 5 and not near the right side will let it move right. Fairly simple, right?

The more advanced way to use If is not using it at all, but using the same principle to get the job done. Below is the code to get this accomplished:

:DelVar θ
:Repeat getKey
:If θ>1:Output(B,A,"   "  ;3 spaces
:B-(θ=3 and B<8)-(θ=2 and B>1→B
:A-(θ=5 and A<16)-(θ=4 and A>1→A

The above code uses the fact that you cannot subtract a negative number, from a positive number. So we set up left and right on one line, and up and down on another.

This is an example. A is the variable that has info for going left and right. This is what the calculator sees when θ=5 and not on the far right of the screen:

The Code:

:A-(θ=5 and A<16)-(θ=4 and A>1→A

The calculator sees: A-(1)-(0)A

As you can see, it takes the boolean logic, and turns it into a simple equation. The same thing applies if θ=4, only you cannot subtract a negative, so you add to it. A word of caution when using this command. If you use only a few of these commands, you will not notice any slow down. If you use a great many of these logical statements, you could see considerable lag. This is because the calculator will continously execute the equations.

Well, that concludes this particular lesson. I hope everything here was understandable, but if you need something explained, please feel free to visit the forums.

rand - This creates a random number between the values of 0 and 1, as default.

randInt( - This creates a random number based off 2 given values, a third optional value will create a list based off of "trials".

<<Previous - Contents - Next>>