Page 1 of 1

[SOLVED] Code problem, or event problem?

Posted: Tue Oct 02, 2018 2:00 pm
by MaxV
Hello,
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
When the body graphics reach their first point, their pdir changes that line is deleted.

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
with horrible results :?
Do you have an idea of the problem causes?

Re: Code problem, or event problem?

Posted: Tue Oct 02, 2018 4:43 pm
by dunbarx
Hi,

I built much of your gadget, with about a dozen extra graphics apart from the one named "head"

I could not make the custom property fail to include the return no matter how fast I tried to press an arrow key. I even had the property build into a field so I could watch it grow.

It is what I would have expected, Livecode being faster than my finger, with a very short handler to run through.

Craig Newman

Re: Code problem, or event problem?

Posted: Wed Oct 03, 2018 10:41 pm
by MaxV
This video show the bug: http://www.maxvessi.net/temp/bug.mp4

Please can you try it?

Re: Code problem, or event problem?

Posted: Sun Oct 21, 2018 2:51 am
by capellan
Hi All,

Today, Bogs posted a link to this forum thread.
I was trying to understand why this bug happens
but when I test the code, could not make it
work as show in MaxV video.

Where do you call the handler "moveSnake"
in the arrowkey and "changeDir" handlers?

Thanks in advance!

Al
LC9_MaxV_moveSnake_game.livecode.zip

Re: Code problem, or event problem?

Posted: Mon Oct 22, 2018 12:14 pm
by MaxV
I put the code in the card.

Re: [SOLVED] Code problem, or event problem?

Posted: Thu Dec 20, 2018 8:39 am
by MaxV
I found the problem and the solution.
If a graphic has a even number of pixel size (4x4), the loc is rounded (2,2 or 3,3 else), and some time is not rounded equally for all graphics, so along the movement path some graphic can't pass on the pixel on the other graphics and they can't activate the turn code and so they are lost.
EXAMPLE, 4x4 pixels graphic, WHICH PIXEL IS THE LOC?

Code: Select all

****
****
****
****
If a graphic has a even number of pixel size (5x5), the loc is precise:

EXAMPLE, 5x5 pixel graphic, WHICH IS THE LOC?

Code: Select all

*****
*****
**L**
*****
*****

Re: [SOLVED] Code problem, or event problem?

Posted: Thu Dec 20, 2018 11:13 am
by bogs
That is pretty clever Max, I would never have caught it.