TI-Basic Z80 Programming/Programming Games

From Wikibooks, open books for an open world
Jump to: navigation, search

To begin programming games, a firm basis in the fundamentals of TI-Basic is needed. The explanations for the examples given here are meant to be a simple explanation, not a technical guideline. It is assumed that you will have enough understanding of TI-Basic to follow along with the simple explanation provided.

PONG Demonstration[edit]

The following program is a simple game of Pong programmed in TI-Basic. I will explain every line afterwards, followed by a (more confusing) version optimized for size. Note that "≤" should be replaced with the less-than-or-equal-to sign (in the TEST menu), as well with the "≥".

PROGRAM:PONG
: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,"")

Ok, if you are totally lost right now, then just read on. The explanation:

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

These first lines basically set all our variables to where they need to be before the program starts. Our "A" and "B" variables are the coordinates for our "ball". The "C" variable is what keeps the entire loop running. The "D" variable and "E" variable are the variables that will change our ball coordinates. They allow us to use more efficient coding than using 1 and -1. The "F" coordinate is our horizontal coordinate for our "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.
Same as using While C=0 (Only smaller).

:ClrHome

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

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

This is the stuff you see. It tells the ball and the paddle to appear.

:If A=1
:-D→D

tells the ball to "bounce" off the ceiling.

: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 basically tells the ball to bounce off the right and left walls.

:If A=8
:1→C

This is what makes you lose. 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 stuff is what makes the game tick. It is the user input step. I'll go a bit in depth about this, because its cool how compact this step is. the "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 (right) and is less than 13 (right side of the screen = 16 - 3 (for the size of the paddle))
an equality equation always becomes a "one" if it is true, and a "zero" if false, therefore if it is true, then it will move one in that direction, and if the user presses 24 (left) the same principle applies.


:End

This line is the line that 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 are what happens if you lose- it will display a message, clear the screen and of course prevents "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,"    //")4 spaces
:Ans→F
:Output(8,F,"--")
:End
:ClrHome
:Output(1,1,"U LOSE")

That concludes our "Pong" demonstration, I hope you've learned something.

Note[edit]

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

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


Previous: Advanced Programming
Next: Links
Table of Contents: TI-Basic Z80 Programming