TI-BASIC Wiki
Advertisement

We will be learning about the getKey function and how to create a controlled movement in this program.

Starting Out[]

There are a few steps to ensure that your program will run smoothly:

Check if you have the RAM to hold a program[]

Click on 2nd then MEM above the + sign. Make Sure that you have at least 500 RAM too use. If you don't i suggest you archive something by pressing 2 then 1 then press enter to anything that doesn't have a star next to it.

Creating the program[]

Click the PRGM key then use the right arrow until NEW is highlighted. You should get a screen like this:

PROGRAM
NAME=

Go ahead and enter any name you want. Must be only A-Z, no numbers.

The Program[]

PROGRAM:MOVER
:ClrHome
:1→Y
:1→X
:Lbl 1
:Output(Y,X,"O"
:getKey→G
:If G=0
:Goto 1
:ClrHome
:If G=25
:Y-1→Y
:If G=34
:Y+1→Y
:If G=24
:X-1→X
:If G=26
:X+1→X
:If X=0
:X+1→X
:If Y=0
:Y+1→Y
:If X=17
:X-1→X
:If Y=9
:Y-1→Y
:Goto 1

Explanation[]

PROGRAM:MOVER
:ClrHome
:1→Y
:1→X

Setup the variables and clear the screen.

:Lbl 1

This is used to create a loop.

:Output(Y,X,"O"

This prints an "O" at the O's current position.

:getKey→G

Get a key and store it in G.

:If G=0
:Goto 1

If G is 0, a key wasn't pressed, so go back and wait for a key to be pressed.

:ClrHome

Clear the screen so a new "O" can be printed.

:If G=25
:Y-1→Y
:If G=34
:Y+1→Y
:If G=24
:X-1→X
:If G=26
:X+1→X

Move the O in a direction depending on which key was pressed.

:If X=0
:X+1→X
:If Y=0
:Y+1→Y
:If X=17
:X-1→X
:If Y=9
:Y-1→Y

If the O would have been off-screen, put it back on the screen.

:Goto 1

Go back to the begining of the loop.

Result[]

The user will be able to move a little circle around the screen.


Optionals[]

You can make your circle go through the side of the screen if you change this part of the program (changes are boldened).

...
:If X=0
:16→X
:If Y=0
:8→Y
:If X=17
:1→X
:If Y=9
:1→Y
:Goto 1

Alternative[]

This is a much shorter program that does the exact same thing. It uses the properties of booleans to eliminate several of the If statements in the original.

PROGRAM:MOVER 
:ClrHome 
1→A 
1→B 
:While 1  
:Output(A,B,"O 
:Repeat Ans
:getKey→K 
:End
:Output(A,B,"                            //1 space to erase the O for update
:B+(Ans=26 and B<16)-(Ans=24 and B>1→B 
:A+(K=34 and A<8)-(K=25 and A>1→A 
:End
Advertisement