TI Basic Code

Whenever I'm bored, my beloved TI-83 makes me happy with its TI Basic.

(back to Programming)

Simple Snake Game in TI Basic

This is a very simple Snake game written in TI Basic. The snake starts in the middle of the display and grows very quickly, i.e. its end stays in the same position. You can add obstacles by setting graphs, since they are drawn before the game starts. If you reach three consecutive pixels, you lose.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
: Pxl-On( 1, 1 )
: ClrDraw
: 46 -> X
: 32 -> Y
: While 1
: getKey -> K
: If ( K > 23 and K < 27 ) or K = 34 : K-> D
: If D = 24 : X - 1 -> X
: If D = 25 : Y - 1 -> Y
: If D = 26 : X + 1 -> X
: If D = 34 : Y + 1 -> Y
: If pxl-Test( Y, X ) : Then : A + 1 -> A : If A > 2 : Stop : Else : 0 -> A : End
: Pxl-On( Y, X )
: End

Time for explanation, then:

  • Line 1 puts a pixel on the display in order to bring the graphical window up. If this is not done, line 2 has no effect.
  • Line 2 clears the graph window of any prior snakes.
  • Lines 3-4 sets the initial coordinates of the snake’s head in the center of the display. The display is 92 pixels wide and 64 pixels high.
  • Line 5 starts the game loop.
  • Line 6 gets the key input and stores it into K. This is done so that the same key can be used in the next statement.
  • Line 7 checks whether K is one of the direction keys. If it is, it is stored into the direction variable.
  • Lines 8-11 check what the direction variable is set to, and adjusts the snake’s head accordingly.
  • Line 12 is a nested If statement. It can be rewritten as follows in pseudo-code:
    If ( pxl-Test( Y, X ) ) {
        A + 1 -> A
        If ( A > 2 ) {
            Stop
        }
    } Else {
        0 -> A
    }

    If the snake’s head is on a pixel, the consecutive pixel count — A — is incremented. Then, if A is greater than 2, i.e. this was the third pixel, the game loop (and thereby the game) ends. If it was not a pixel, the pixel count is reset to 0.

  • Line 14 marks the end of the game loop. Here, it starts over at line 5 again.

Calculating Pi in TI Basic Using the Leibniz Formula

Got nothing to do during math class? Then let your calculator calculate Pi! The Leibniz formula states that:

Pi/4 = 1/1 - 1/3 + 1/5 - 1/7 + 1/9 …

This is how to do it on your TI calculator, nicely formated.

: 0 -> N
: 0 -> P
: ClrHome
: Output( 1, 1, "STEP " )
: Output( 2, 3, "PI= " )
: While 1
: 1 / ( (-1)^N(2N+1) ) + P -> P
: Output( 1, 6, N )
: Output( 2, 6, 4P )
: N + 1 -> N
: End

Note that this is a very poor way of calculating Pi, especially with a calculator, since it requires an extremely large amount of steps in order to obtain accurate values. 10,000,000,000 operations are required in order to get only ten correct decimals.

Fibonacci with TI Basic

If you want to calculate the first few numbers in the Fibonacci sequence:

: { 1, 1 } -> L1
: 2 -> A
: While 1
: A + 1 -> A
: L1( A - 2 ) + L1( A - 1 ) -> L1
: End

You will end up with L1, a list of the first few Fibonacci numbers up to nearly 10100, where overflow will occur. If you want to print the numbers too, just add this in the while loop (second last line):

: Disp L1( A )

A Filled Circle in TI Basic

In TI Basic, there already exists a function for drawing a normal circle, though there is none for drawing a filled circle, a circle in which all pixles are on. There is a fast way of drawing a filled circle with TI Basic, however: Use the Pythagorean theorem to find out where the bounds of the circle are for every x value. For each x value, also draw a line between the top and the bottom.

: 30 -> R
: 45 -> M
: 30 -> N
 
: ClrDraw
: 0 -> Xmin
: 96 -> Xmax
: 0 -> Ymin
: 64 -> Ymax
 
: M - R -> X
: While X <= M + R
: sqrt ( R2 - ( X - M ) 2 ) -> A
: Line( X , N + A , X , N - A)
: X + 1 -> X
: End

That’s it. This program draws a circle centered on (M,N) with the radius R.

FireStats iconAnvänder FireStats