Hey everyone! In my free time lately I've been trying to make a classic snake game, as it's always been a goal of mine. Now, as I want this program to be relatively quick, I've decided to emulate strings and their many uses instead of the slow, clunky list variables offered by TI-basic. Here's my code.
Code:
The problem is, when the user tries to move the snake sequentially within two or more game ticks (t), the snake breaks apart and flies to pieces. I have actually reasoned out the bug. The string I'm using, str1, stores all of the elements of the snake, and which direction they are moving. When an arrow key is pressed, it starts at the first element of the string (the head) and changes the direction of each following element. Now, this is where the bug comes in. If you change the direction of the snake and the elements of the string are not finished updating from the previous direction, then it stops updating the previous direction and updates the string from the first element. (line 16). I need a slice of code that will take a string, update it from the beginning, and if another update is passed while its updating, will update it again from the beginning while finishing the last update process. I know this may be very hard to understand, so I'll try and visualize it with some psuedo-code of my desired effect.
//
Repeat "condition"
detect and change direction of head.
update str1(t)
reset t to cycle through elements of string.
end
//
it should output something like this-
(say 222222 is str1)
I press left arrow, indicating that the direction is changed to 3
the program updates str1
str1 now equals 322222 and t is equal to 1
it cycles through the code
str1 now equals 332222 and t is equal to 2
see where I'm heading? this part is done.
now the string is finished updating and it equals 333333.
but say I press the up arrow, indicating a change of direction to 4.
everything proceeds as usual.
str1 equals 433333 and t equals 1.
but what if it continues to cycle, we get to this point-
str1 equals 444333 and t equals 3-
and THEN we want to change the direction again?
say we press the right arrow, changing the direction to 2.
this is where the error comes in.
the string stops updating the rest of the 3's to 4's and starts changing the entire string to 2's.
I need this solved, and believe me, I've tried. for the life of me I cannot figure this puzzle out! Any help is appreciated, I know this may be a struggle to comprehend...
Code:
ClrHome
2→D
5→Y:13→X:1→T:Y→B:X→S:2→M
"2→Str1:0→C
Repeat K=45:getKey→K
If max(K={25,26,34,24:Then:(K=34)+2(K=26)+3(K=24)+4(K=25)→D:1→T:End
randInt(1,10)→P
randInt(1,26)→U
Output(P,U,"+
Repeat K=45 or (X=U and Y=P) or K=105:getKey→K
Output(1,1,"D="+toString(D)+" M="+toString(M
Output(2,1,Str1
Output(3,1,"T="+toString(T
If max(K={25,26,34,24
Then:(K=34)+2(K=26)+3(K=24)+4(K=25)→D:1→T:End
sub(sub(Str1,1,T-(T≠1))+toString(D)+sub(Str1,T+(T≠length(Str1)),length(Str1)-T+(T=length(Str1))),2(T=1)+(T≠1),length(Str1→Str1
expr(sub(Str1,length(Str1),1→M
Output(Y,X," "
Output(B,S," "
X+(D=2)-(D=3)→X
Y+(D=1)-(D=4)→Y
S+(M=2)-(M=3)→S
B+(M=1)-(M=4)→B
Output(Y,X,"O
Output(B,S,"O
If T=length(Str1):0→T
T+1→T
End
S+(M=3)-(M=2)→S
B+(M=4)-(M=1)→B
Str1+toString(D)→Str1
End
The problem is, when the user tries to move the snake sequentially within two or more game ticks (t), the snake breaks apart and flies to pieces. I have actually reasoned out the bug. The string I'm using, str1, stores all of the elements of the snake, and which direction they are moving. When an arrow key is pressed, it starts at the first element of the string (the head) and changes the direction of each following element. Now, this is where the bug comes in. If you change the direction of the snake and the elements of the string are not finished updating from the previous direction, then it stops updating the previous direction and updates the string from the first element. (line 16). I need a slice of code that will take a string, update it from the beginning, and if another update is passed while its updating, will update it again from the beginning while finishing the last update process. I know this may be very hard to understand, so I'll try and visualize it with some psuedo-code of my desired effect.
//
Repeat "condition"
detect and change direction of head.
update str1(t)
reset t to cycle through elements of string.
end
//
it should output something like this-
(say 222222 is str1)
I press left arrow, indicating that the direction is changed to 3
the program updates str1
str1 now equals 322222 and t is equal to 1
it cycles through the code
str1 now equals 332222 and t is equal to 2
see where I'm heading? this part is done.
now the string is finished updating and it equals 333333.
but say I press the up arrow, indicating a change of direction to 4.
everything proceeds as usual.
str1 equals 433333 and t equals 1.
but what if it continues to cycle, we get to this point-
str1 equals 444333 and t equals 3-
and THEN we want to change the direction again?
say we press the right arrow, changing the direction to 2.
this is where the error comes in.
the string stops updating the rest of the 3's to 4's and starts changing the entire string to 2's.
I need this solved, and believe me, I've tried. for the life of me I cannot figure this puzzle out! Any help is appreciated, I know this may be a struggle to comprehend...