Well, you'll want to have two variables: xvelocity and yvelocity
While the car is moving, you'll increase the x/y by xvelocity and yvelocity.
When you press the up key, you'll decrease y velocity by the acceleration until it reaches the speed limit. When it reaches the speed limit, it stops increasing the velocity.
Do the same for the other keys.
PHP Code:
function onCreated() {
this.xvelocity = this.yvelocity = 0;
this.acceleration = .1;
this.speedlimit = 1;
}
function onTimeout() {
for (k = 0; k < 4; k++) {
if (keydown(k)) {
this.xvelocity = limit(this.xvelocity + vecx(k) * this.acceleration,-this.speedlimit,this.speedlimit);
this.yvelocity = limit(this.yvelocity + vecy(k) * this.acceleration,-this.speedlimit,this.speedlimit);
}
}
}
function limit(val,min,max) {
if (val < min) val = min;
if (val > max) val = max;
return val;
}