Advertisement
TimJSwan

Tim Swan's Snake in ti-Basic

Mar 3rd, 2014
2,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. Corresponds to the following YouTube video:
  2. http://www.youtube.com/watch?v=HcyTeswIKZo
  3. Code was copied manually from my calculator and typed to pastebin about midnight of Mar 2, 2014 Central.
  4. As per request of Justin Martone and Tazorac or CreeperKid43
  5. I also decided to add comments.
  6. In the following code, Underscore "_" means subscript.
  7. :CLRHOME // Clear 8 x 16 Screen
  8. :4->X // Starting head coordinates
  9. :4->Y
  10. :{8,16->dim([A] // Matrix data structure for checking intersection
  11. :Fill(0,[A]
  12. :DelVar L_1 // List data structure for deleting tail
  13. :DelVar L_2
  14. :15->L // Initial Length
  15. :For(A,1,L // Snake will begin compressed into one square
  16. :X->L_1(A
  17. :Y->L_2(A
  18. :End
  19. :L->M // 'L' is length available to data structures,
  20. :0->D // while 'M' is the ideal or actual snake length.
  21. :1->C // Index counter for list data structure
  22. :While 1 // Game loop beings (Quits upon death)
  23. :randInt(0,9->B
  24. :If not(B // 1 in 10 chance
  25. :Then
  26. :randInt(1,8->T
  27. :randInt(1,16->S
  28. :If [A](T,S)=0 // If there is nothing at the randomly chosen position
  29. :Then
  30. :2->[A](T,S // 2 is value for food in the matrix data structure
  31. :Output(T,S,". // . is character for food
  32. :End
  33. :End
  34. :Output(Y,X,"O // Draw snake head
  35. :1->[A](Y,X // 1 is value for snake body in matrix data structure
  36. :getkey->A // We get keyboard input (0 if no key is pressed)
  37. :If A=26 // RIGHT ARROW (2nd row, 6th column)
  38. :0->D
  39. :If A=24 // LEFT ARROW (2nd row, 4th column)
  40. :1->D
  41. :If A=34 // DOWN ARROW (3rd row, 4th column)
  42. :2->D
  43. :If A=25 // UP ARROW (2nd row, 5th column)
  44. :3->D
  45. :If D=0 // Update head coordinates
  46. :X+1->X
  47. :If D=1
  48. :X-1->X
  49. :If D=2
  50. :Y+1->Y
  51. :If D=3
  52. :Y-1->Y
  53. :If X=17 // Check for screen wrap
  54. :1->X
  55. :If X=0
  56. :16->X
  57. :If Y=9
  58. :1->Y
  59. :If Y=0
  60. :8->Y
  61. :X->L_1(C // Update head in list data structure
  62. :Y->L_2(C
  63. :If [A](Y,X)=1 // Check for intersection with body
  64. :Then
  65. :Disp "DEATH
  66. :Disp "LENGTH:
  67. :Disp M // Can you figure out how to make a high score counter?
  68. :Stop // For more 'permanent' variables, name a custom list
  69. :End // something like L_theta (L with a subscript theta)
  70. :If [A](Y,X)=2 // If you reach a food particle
  71. :Then
  72. :M+1->M // Add one to your ideal length (Mass? If you want an 'm' word)
  73. :End
  74. // Remove this redundant line of code since we already have it above: ":1->[A](Y,X]"
  75. :If C>L // If counter exceeds current available list length
  76. :C->L // Then we force the length to increase to fit
  77. :C+1->C // Increment counter once per cycle
  78. :C->F // F is used temporarily to remember where we need to erase the tail
  79. :If F>L // We cannot erase something outside our list size
  80. :1->F // So we wrap the index
  81. :If C>M // Our counter is limited by our ideal length
  82. :1->C
  83. :Output(L_2(F),L_1(F)," " // Don't need 2nd quote, but you need the space.
  84. :0->[A](L_2(F),L_1(F) // 0 is value for empty space in matrix data structure
  85. :End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement