1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
program Kars;
{$FRAME_WIDTH 256}
{$FRAME_HEIGHT 256}
uses
Flash8;
const
Radius = 10;
TRACE_COUNT = 100;
type
TCircle = class(MovieClip)
constructor Create(Parent: MovieClip; Name:string; x, y, r: Double);
end;
TWheel = class(MovieClip)
constructor Create(Car: MovieClip; i, x, y: Integer);
end;
TTrace = class(MovieClip)
constructor Create(i: Integer);
end;
TCar = class(MovieClip)
Speed: Double;
Wheels: Array[0..3] of TWheel;
Traces: Array[0..TRACE_COUNT] of TTrace;
constructor Create;
procedure onEnterFrame; override;
end;
var
Help : TextField;
Circle : TCircle;
constructor TCircle.Create(Parent: MovieClip; Name: string; x, y, r: Double);
var
a : Double;
c1,s1: Double;
c2,s2: Double;
r2: Double;
begin
inherited Create(Parent, Name, 1);
_x := x;
_y := y;
a := 3.14/4;
c1 := cos(a);
s1 := sin(a);
a := 3.14 / 8;
c2 := cos(a);
s2 := sin(a);
r2 := 11 * r / 10;
beginFill($FF0000);
moveTo(0, r);
curveTo( r2*s2, r2*c2, r*s1, r*c1);
curveTo( r2*c2, r2*s2, r,0);
curveTo( r2*c2,-r2*s2, r*s1,-r*c1);
curveTo( r2*s2,-r2*c2, 0,-r);
curveTo(-r2*s2,-r2*c2,-r*c1,-r*s1);
curveTo(-r2*c2,-r2*s2, -r,0);
curveTo(-r2*c2, r2*s2,-r*c1, r*s1);
curveTo(-r2*s2, r2*c2, 0,r);
endFill();
end;
constructor TWheel.Create(Car: MovieClip; i, x, y: Integer);
begin
inherited Create(Car, 'Wheel' + IntToStr(i), i);
_x := x;
_y := y;
beginFill(0);
moveTo(-2,-1);
lineTo(+2,-1);
lineTo(+2,+1);
lineTo(-2,+1);
endFill();
end;
constructor TTrace.Create(i :Integer);
begin
inherited Create(nil, 'Trace' + IntToStr(i), i - 1000);
_x := 200;
_y := 123;
_alpha := 100 * i / TRACE_COUNT;
_rotation := -90;
beginFill(0);
moveTo(-2+1,-1-5);
lineTo(+2+1,-1-5);
lineTo(+2+1,+1-5);
lineTo(-2+1,+1-5);
endFill();
beginFill(0);
moveTo(-2+1,-1+5);
lineTo(+2+1,-1+5);
lineTo(+2+1,+1+5);
lineTo(-2+1,+1+5);
endFill();
end;
constructor TCar.Create;
var
i : Integer;
begin
inherited Create(nil, 'Car', 5);
TCircle.Create(Self, 'Driver', 10, 0, 2);
_x := 200;
_y := 123;
_rotation := -90;
beginFill($0000FF);
moveTo(- 2,-5);
lineTo(+18,-4);
lineTo(+18,+4);
lineTo(- 2,+5);
endFill();
Wheels[0] := TWheel.Create(Self,-1, 1, -5);
Wheels[1] := TWheel.Create(Self, 3, 12, -5);
Wheels[2] := TWheel.Create(Self, 4, 12, +5);
Wheels[3] := TWheel.Create(Self, 5, 1, +5);
for i := 0 to TRACE_COUNT do
begin
Traces[i] := TTrace.Create(i);
end;
Speed := 0;
end;
procedure TCar.onEnterFrame;
var
dx, dy, r: Double;
i: Integer;
begin
if (Key.isDown(KEY_UP)) then
Speed := Speed + 1; //acceleration en avant
if (Key.isDown(KEY_DOWN)) then
Speed := Speed - 0.2; //acceleration en arrière
r := 0;
if (Key.isDown(KEY_LEFT)) then
begin
_rotation := _rotation - 8; //_rotation affecte directement le carré bleu
r := -30;
end;
if (Key.isDown(KEY_RIGHT)) then
begin
_rotation := _rotation + 8;
r := +30;
end;
if Speed <> 0 then
begin
Help._visible := False;
if (Math.abs(Speed)>1) then Speed := Speed * 0.9; // limite l'acceleration
if Speed < 0 then r := -r;
Wheels[1]._rotation := r;
Wheels[2]._rotation := r;
dx := Math.cos(_rotation*(Math.PI/180))*Speed; // donne la position x future en fonction de la vitesse
dy := Math.sin(_rotation*(Math.PI/180))*Speed; // donne la position y future en fonction de la vitesse
if (Circle.hitTest(_x + dx, _y + dy, true)) // collision avec le rond
then begin
Speed := Speed * -0.5; // reaction apres collision, rebond du carré bleu
end else begin
dx := _x + dx;
dy := _y + dy;
if dx < -5 then dx := 256;
if dx > 261 then dx := 0;
if dy < -5 then dy := 256;
if dy > 261 then dy := 0;
_x := dx;
_y := dy;
end;
end;
for i := 1 to TRACE_COUNT do
begin
Traces[i - 1]._x := Traces[i]._x;
Traces[i - 1]._y := Traces[i]._y;
Traces[i - 1]._rotation := Traces[i]._rotation;
end;
Traces[TRACE_COUNT]._x := _x;
Traces[TRACE_COUNT]._y := _y;
Traces[TRACE_COUNT]._rotation := _rotation;
end;
begin
Help := TextField.Create(_root, 'help', 100, 20, 50, 220, 21);
Help.Text := "Utilisez les flèches pour déplacer le véhicule";
Circle := TCircle.Create(_root, 'Spot', Stage.Width /2, Stage.Height - 100, Radius);
TCar.Create();
end. |
Partager