To skip this chapter use the following link
Is it possible for the character to go around in a smoother fashion? Sure you can! You need the program to keep track of a new state, the velocity. So we need to add the following global state variables at the top of our program:
X,Y=60,60
DX,DY=0,0The DX and DY variables keep track of the velocity (or the distance taken per frame). The player will now instead control these directly instead of the sprite's X and Y values. So lets change the _UPDATE function to do this:
FUNCTION _UPDATE()
IF BTN(0) THEN
DX-=0.6
END
IF BTN(1) THEN
DX+=0.6
END
IF BTN(2) THEN
DY-=0.6
END
IF BTN(3) THEN
DY+=0.6
END
-- MAX VELOCITY 4 PIXELS PER FRAME
DX=MID(-4,DX,4)
DY=MID(-4,DY,4)
-- UPDATE THE X,Y WITH DX,DY
X+=DX
Y+=DY
-- CLAMP THE X,Y
X=MID(0,X,120)
Y=MID(0,Y,120)
ENDRunning the program and controlling the character will look a bit like this:
It is a bit out of control, and this is because we don't slow down when we release the keys. Lets correct this by "applying some breaks" at the end of our _UPDATE function. Easiest is to reduce the DX and DY values for each frame with a factor:
-- AT THE END OF _UPDATE FUNCTION
DX*=0.8
DY*=0.8When you're not touching the keys the DX and DY values will go towards zero, giving the impression of a slow down. When running the program it'll look like this:
- Creating games is an iterative process. Don't be afraid of a little math to make things smoother.
- It's common to use
XandYto track position,DXandDYto track the change per frame (aka. the velocity).

