# TI-Basic Z80 Programming/Programming Games

It is possible to program simple games using TI-Basic. The explanations for the examples given here are meant to be a simple explanation, not a technical guideline.

## PONG Demonstration

The following program is a simple game of Pong programmed in TI-Basic. We begin by defining the code for the game:

:DelVar C :DelVar G :7→A :8→B :1→D :-1→E :6→F :While not(C :ClrHome :Output(A,B,".") :Output(8,F,"--") :If A=1 :-D→D :If A=7 :Then :If B≥F and B≤F+1 :-D→D :End :If B=1 or B=16 :-E→E :(A=8)→C :A+D→A :B+E→B :getKey→K :F+(K=26 and F<13)-(K=24 and F>1)→F :End :Pause "U LOSE" :ClrHome :Output(1,1,"")

Here, we begin explaining the code:

:Delvar C :Delvar G :7→A :8→B :1→D :-1→E :6→F

These first lines initialize variables to certain values. The `A` and `B` variables are the coordinates for the ball. `C` keeps the entire loop running. `D` and `E` will change our ball coordinates. They allow us to use more efficient coding than using 1 and -1. `F` is our horizontal coordinate for the paddle, so that it can move back and forth.

:While not(C

The not( command returns 1 (true) if the conditional is equal to 0. This is equivalent to using `While C=0`, but reduces a byte.

:ClrHome

Refreshes the screen each time something moves (or at least each time the loop repeats).

:Output(A,B,".") :Output(8,F,"--")

This prints the symbols to the screen.

:If A=1 :-D→D

This tells the ball to "bounce" off the ceiling by reversing its direction.

:If A=7 :Then :If B>=F and B<=F+1 :-D→D :End

This step right here does a bunch of stuff in a little space. The first line checks if the ball is at the bottom of the screen. If the ball is, then it checks if the ball is on the paddle. If the paddle is there, then the ball bounces off.

:If B=1 or B=16 :-E→E

This tells the ball to bounce off the right and left walls.

:If A=8 :1→C

This is what triggers the loss. This checks if the ball has hit the bottom of the screen. If it has, then the program terminates the loop.

:A+D→A :B+E→B

This step makes the ball move around the screen, by changing the coordinates.

:getKey→K :F+(K=26 and F<13)-(K=24 and F>1)→F

This is what makes the game tick. It is the user input step. `getkey→K` stores the user keypress into `K`. Then, in the next line, `F` (the horizontal coordinate for the paddle) gets one added to it if the user presses (26) and is less than 13 (right side of the screen = 16 - 3 (for the size of the paddle))

An equality equation always becomes 1 if it is true, and 0 if false, therefore if it is true, then it will move one in that direction, and if the user presses (24) the same principle applies.

:End

This line defines the end of the loop that the main part of the program is running in.

:Pause "U LOSE" :ClrHome :Output(1,1,"")

These lines run when you loose. It will display a message, clear the screen, and prevent "Done" from being displayed.

Here is the more optimized version:

PROGRAM:PONG :7→A:8→B :DelVar C1→D :-1→E:6→F :Repeat C :If A=1 :-D→D :E-2Emax({A=7,B>=F,B<=F+3→ :E-2E(B=1 or B=16)→E :A=8→C :Output(A,B," //")1 space :A+D→A :B+E→B :Output(A,B,"." :getKey :F+(Ans=26)-(Ans=24 :If 15<=Ans :1 :If 0>=Ans :13 :Output(8,F," //") :Ans→F :Output(8,F,"--") :End :ClrHome :Output(1,1,"U LOSE")

### Note

A scoring system and other features can be added, but will be explained in later articles. This program works very well, except that the cursor doesn't move fast enough. In order to make the platform move faster, change:

:getKey→K :F+(K=26 and F<13)-(K=24 and F>1)→F

to:

:getKey→K :If (K=26 and F<13) :F+2→F :If (K=24 and F>1) :F-2→F

Which could also be written as the following:

:getKey→K :F+2(K=26 and F<13)-2(K=24 and F>1)→F

This will make the platform skip over one more space, making the platform faster.