Author Topic: SNAKE GAME ti basic  (Read 6582 times)

0 Members and 1 Guest are viewing this topic.

Offline Naoki Heginbotham

  • LV0 Newcomer (Next: 5)
  • Posts: 1
  • Rating: +0/-0
    • View Profile
SNAKE GAME ti basic
« on: April 13, 2016, 12:27:11 pm »
Hello, I am new at the Ti-basic programming language, and am still learning. Does anyone know a simple code for the snake game? (No menu or anything. Just a snake moving around, no food or menu.) Please also include an explanation if possible.
Thanks!

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: SNAKE GAME ti basic
« Reply #1 on: April 13, 2016, 06:01:27 pm »
Code: [Select]
FnOff
AxesOff
ClrDraw
randInt(5,90 -> A
randInt(5,60 -> B
SetUpEditor LSNY
SetUpEditor LSNX
5 -> W
W -> dim(LSNX
W -> dim(LSNY
26 -> U:U -> K:1 -> C
Repeat K=45
getkey -> K
If K:Then
K -> U
Else:U->K
End
A-(k=24)+(K=26) -> A
B-(k=25)+(K=34) -> B
If A>93:1 -> A
If A<1:93 -> A
If B>62:1 -> B
If B<1:62 -> B
Pxl-Off(LSNY(C)),LSNX(C)
Pxl-On(B,A)
A -> LSNX(C
B -> LSNX(C
C+1 -> C
IF C>dim(LSNX
1 -> C
End
ClrHome

That's all!
It is adapted from my snake code (with food and growth) so some code might seem odd.
-> is the sto key
In LSNX/Y the starting L is the list character (small capital L)
Changing the code that sets the value of W will change the length of the snake. (changing it to 15 -> W would set the length to 15)
I don't have time unfortunately to add a full explanation, but here is a list of what each variable represents;
A: snake's head's current x location (in pixels)
B: snake's head's current y location (in pixels)
LSNX list of the snakes's body's x coords
LSNY list of the snakes's body's y coords
C keeps track of which body slot should be modified
K is the key pressed
U is the last key pressed (assuming no key was pressed)
W sets the starting length of the snake (changing it doesn't do anything once the game has started)

Let me know if it doesn't work I may have messed up typing it in.

By the way, welcome!  ;D
« Last Edit: April 14, 2016, 07:38:38 pm by Eeems »
I'm still around... kind of.