set pSpeed = 1
set pDir = random(8)
set pMv = the mouseV
set pMh = the mouseH
set pOldH = pH
set pOldV = pV
set pAlarmCnt = 0
set pStageW = the stageRight - the stageLeft
set pStageH = the stageBottom - the stageTop
set pStageL = the width of member (the memberNum of sprite pSprite) / 2
set pStageR = pStageW - the width of member (the memberNum of sprite pSprite) / 2
set pStageT = the height of member (the memberNum of sprite pSprite) / 2
set pStageB = pStageH - the height of member (the memberNum of sprite pSprite) / 2
set pH = the locH of sprite pSprite
set pV = the locV of sprite pSprite
set pStartTime = the ticks

set the locH of sprite pSprite to pH
set the locV of sprite pSprite to pV
end beginSprite

set the locH of sprite pSprite = pH
set the locV of sprite pSprite = pV
end mGoPuppet

on mSetNewDir me
if mCheckWall(me) then
set pDir = mCheckWallDir(me)
else
if pSpeed < 5 then
set pDir = mCheckDir()
end if
end if
end mSetNewDir

on mReverseDir me
set pDir = pDir + 4
if pDir > 8 then
set pDir = pDir - 8
end if
end mReverseDir

on mCheckWall me
-- Hit the Wall?
if pH < pStageL OR pH > pStageR OR pV < pStageT OR pV > pStageB then
return TRUE
else
return FALSE
end if
end mCheckWall

on mSetSpeed me
set newDist = mCheckSpeed(me)

if newDist > 100 then
set pSpeed = 20
else
if newDist > 80 then
set pSpeed = 13
else
if newDist > 60 then
set pSpeed = 8
else
if newDist > 40 then
set pSpeed = 5
else
if newDist > 20 then
set pSpeed = 1
else
set pSpeed = 0
end if
end if
end if
end if
end if
end mSetSpeed

on mCheckSpeed me

set pMv = the mouseV
set pMh = the mouseH

set n = pMh - pH
set m = pMv - pV

if n < 0 then
set n = n * (-1)
end if
if m < 0 then
set m = m * (-1)
end if

return n + m
end mCheckSpeed

on mCheckDir me
if pMh > pH then
if pMv > pV then
set newDir = 8
else
if pMv < pV then
set newDir = 6
else
-- pMv = pV
set newDir = 7
end if
end if
else
if pMh < pH then
if pMv > pV then
set newDir = 2
else
if pMv < pV then
set newDir = 4
else
-- pMv = pV
set newDir = 3
end if
end if
else
-- pMh = pH
if pMv > pV then
set newDir = 1
else
if pMv < pV then
set newDir = 5
else
-- pMv = pV
set newDir = 0
end if
end if
end if
end if

-- Tolleranz
if pMv < pV then
if pMh < pH then
if (pH - pMh < 1 AND pV - pMv < 2) then
set newDir = 0
end if
else
if (pMh - pH < 1 AND pV - pMv < 2) then
set newDir = 0
end if
end if
else
if pMh < pH then
if (pH - pMh < 1 AND pMv - pV < 2) then
set newDir = 0
end if
else
if (pMh - pH < 1 AND pMv - pV < 2) then
set newDir = 0
end if
end if
end if
return newDir
end mCheckDir

on mCheckWallDir me
if pH > pStageR then
if pV > pStageB then
set newDir = 8
else
if pV < pStageT then
set newDir = 6
else
set newDir = 7
end if
end if
else
if pH < pStageL then
if pV > pStageB then
set newDir = 2
else
if pV < pStageT then
set newDir = 4
else
set newDir = 3
end if
end if
else
if pV > pStageB then
set newDir = 1
else
if pV < pStageT then
set newDir = 5
else
set newDir = pDir
end if
end if
end if
end if