I'm trying to create a snake game in livecode, but I found a very strange bug.
First of all this is my code:
########CODE to copy and paste with your mouse#######
on arrowKey pKey
switch pkey
case "left"
changedir "-1,0"
break
case "right"
changedir "1,0"
break
case "up"
changedir "0,-1"
break
case "down"
changedir "0,1"
break
end switch
end arrowKey
on moveSnake
lock screen
put the number of graphics into tnum
repeat with i=1 to tnum
#movimento
put the loc of graphic i into tLoc
put the pdir of graphic i into tpdir
add item 1 of tpdir to item 1 of tLoc
add item 2 of tpdir to item 2 of tLoc
set the loc of graphic i to tLoc #new loc
#cambio direzione
if the short name of graphic i is not "head" then
put the pdirlist of graphic i into tpdirlist
if item 1 to 2 of tpdirList = the loc of graphic i then
put line 1 of tpdirList into tpdirList1
set the pdir of graphic i to (item 3 to 4 of tpdirList1)
set the pdirlist of graphic i to (line 2 to -1 of tpdirList)
end if
end if
end repeat
unlock screen
send movesnake to me in 0.02sec
end moveSnake
on changedir tDir
set the pdir of graphic "head" to tdir
put the number of graphics into tnum
repeat with i=1 to tnum
if the short name of graphic i = "head" then next repeat
put the pdirList of graphic i into temp
put the loc of graphic "head" & comma & tdir & comma & return after temp #loc,dir
set the pdirList of graphic i to temp
end repeat
end changedir
########END OF CODE generated by this livecode app: http://tinyurl.com/j8xf3xq ########
########Code tested with livecode 9.0.1########
The code works this way at the present, there is a graphic head, all the rest of the graphics are the body parts. When the user use the arrow keys, the snake change movement direction. This change is added to each graphic with custom properties.
Each graphics have these custom properties:
- pdir: where to go (one pixel at time), it's like "-1,0"
- pdirlist: when to change diection and where. It's a list like
Code: Select all
168,200,-1,0 200,50,0,1 300,50,1,0
Well, if the user play slow, all work. If the user play fast, I mean hit the arrow keys very close one after another, then the pdirlist lose the return chars, so sometime the list become:
Code: Select all
168,200,-1,0200,50,0,1
300,50,1,0
Do you have an idea of the problem causes?