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.
