User clicks on two adjacent boxes to swap them, and using a little timer-based animation, they move into each other's places. The only thing affected by the timer is the positions of the two boxes, the drawing is independent.
Seemed simple enough to me, but the movement is slower when I cap the frame rate at 60 versus leaving it unlimited (which is around 6600fps for me).
My only thought is that by running at a higher frame rate, the timer check happens more frequently and thus stays more accurate to the 10ms delay I set.
#CONSTANT cBLUE = rgb(0,128,255)
#CONSTANT cRED = rgb(255,0,0)
#CONSTANT cYELLOW = rgb(255,255,0)
#CONSTANT cGREEN = rgb(0,255,0)
#CONSTANT cPURPLE = rgb(255,0,255)
dim colors(5) as dword
colors(1) = cBLUE
colors(2) = cRED
colors(3) = cYELLOW
colors(4) = cGREEN
colors(5) = cPURPLE
dim map(10, 10)
for x = 0 to 9
for y = 0 to 9
map(x,y) = rnd(4)+1
next y
next x
mx = -1
my = -1
mx2 = -1
my2 = -1
sync on
sync rate 60
do
cls
rem user control to swap tiles
if mouseclick() = 1 and flag = 0 and swap = 0
flag = 1
x = mousex() / 64
y = mousey() / 64
if mx = x and my = y
mx = -1
my = -1
else
if mx = -1 and my = -1
mx = x
my = y
else
if (abs(mx-x) = 1 and my-y = 0) or (mx-x = 0 and abs(my-y) = 1)
swap = 1
t# = 0
timestamp = timer()
mx2 = x
my2 = y
endif
endif
endif
endif
if mouseclick() = 0 then flag = 0
rem draw the map and tiles
for x = 0 to 9
for y = 0 to 9
ink rgb(128,128,128),0
box x*64+1, y*64+1, x*64+63, y*64+63
if swap = 0
ink colors(map(x,y)), 0
box x*64+4, y*64+4, x*64+60, y*64+60
else
if (x <> mx or y <> my) and (x <> mx2 or y <> my2)
ink colors(map(x,y)), 0
box x*64+4, y*64+4, x*64+60, y*64+60
endif
endif
next y
next x
rem swap two tiles
if swap = 1
x = (mx2 - mx)*64*t#
y = (my2 - my)*64*t#
ink colors(map(mx2, my2)), 0
box mx2*64+4-x, my2*64+4-y, mx2*64+60-x, my2*64+60-y
ink colors(map(mx, my)), 0
box x+mx*64+4, y+my*64+4, x+mx*64+60, y+my*64+60
rem do some animation in swapping the tiles
if timestamp+10 <= timer()
inc t#, 0.04
if t# >= 1
t# = 1
swap = 0
local t as dword
t = map(mx, my)
map(mx, my) = map(mx2, my2)
map(mx2, my2) = t
mx = -1
my = -1
mx2 = -1
my2 = -1
endif
timestamp = timer()
endif
endif
rem draw swapping tiles
a2Box mx*64, my*64, mx*64+64, my*64+64, rgb(255,255,255)
a2Box mx2*64, my2*64, mx2*64+64, my2*64+64, rgb(255,255,255)
sync
loop
"You're all wrong. You're all idiots." ~Fluffy Rabbit