Talking about Racing game, Actionscript 3, Flash and Game development.
I promised I’d introduced artificial intelligence in a racing game, and I am starting with artificial stupidity.
Anyway, I managed the car to somehow run around the track. I said “stupidity” but I should have said “blindness”, because the CPU driver is actually blind.
How would you react if you were blind and crazy enough to try to run around a track with the pedal to the metal?
I would run as fast as I can until I realize I am out of the track. Then I would decelerate until I realize whether I am out on the left or on the right side of the track, and keep on steering until I am on the track again.
This is what happens if you change car_mc
class you can find in this example with this one:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
public class car_mc extends Sprite {
public var acceleration:Number=0.4;
public var speed_decay:Number=0.96;
public var rotation_step:Number=10;
public var max_speed:Number=10;
public var back_speed:Number=1;
public var speed:Number=0;
public function car_mc(posx:int,posy:int):void {
x=posx;
y=posy;
addEventListener(Event.ENTER_FRAME,on_enter_frame);
}
public function on_enter_frame(e:Event):void {
if (speed0.3) {
speed*=speed_decay;
} else {
speed=0;
}
}
}
}
As you can see, car completes the track but performances are affected by the blindness of the driver who tries to adjust the vehicle only when he hits something.
Next time, I’ll show you how to make the CPU see the track. No need to download anything, just replace car_mc
content with the one on this post.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.