Урок на делфи 54. Гоночки


задание на delphi. Простые 2D гоночки

unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls,math, Vcl.StdCtrls,mmsystem;
type
TForm1 = class(TForm)
Image2: TImage;
Timer1: TTimer;
razm1: TShape;
razm2: TShape;
razm3: TShape;
razm4: TShape;
car: TImage;
vrag1: TImage;
Timer2: TTimer;
Button1: TButton;
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
skorost:Integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
sndPlaySound('fon.wav',SND_LOOP or SND_ASYNC);
skorost:=10;
car.Proportional:=true;
car.Picture.LoadFromFile('car.bmp');
vrag1.Proportional:=true;
vrag1.Picture.LoadFromFile('car2.bmp');
vrag1.Left:=RandomRange(Image2.Left,Image2.Width);
vrag1.Top:=-vrag1.Height;
Timer1.Enabled:=true;
Timer2.Enabled:=true;
Button1.Enabled:=false;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
sndPlaySound('fon.wav',SND_LOOP or SND_ASYNC);
skorost:=20;
Image2.Stretch:=true;
Image2.Picture.LoadFromFile('asf.bmp');
Image2.Transparent:=true;
car.Stretch:=true;
car.Picture.LoadFromFile('car.bmp');
car.Transparent:=true;
vrag1.Stretch:=true;
vrag1.Picture.LoadFromFile('car2.bmp');
vrag1.Transparent:=true;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key=37) and (car.Left>Image2.Left) then
begin
car.Left:=car.Left-5;
end;
if (key=39) and (car.Left+car.Width begin
car.Left:=car.Left+5;
end;
if (key=38) and (car.top>Image2.Top) then
begin
car.top:=car.top-5;
end;
if (key=40) and (car.top+car.Height begin
car.top:=car.top+5;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
razm1.Top:=razm1.Top+skorost;
razm2.Top:=razm2.Top+skorost;
razm3.Top:=razm3.Top+skorost;
razm4.Top:=razm4.Top+skorost;
if razm1.Top+razm1.Height>Form1.Height then
razm1.Top:=-razm1.Height;
if razm2.Top+razm2.Height>Form1.Height then
razm2.Top:=-razm1.Height;
if razm3.Top+razm3.Height>Form1.Height then
razm3.Top:=-razm1.Height;
if razm4.Top+razm4.Height>Form1.Height then
razm4.Top:=-razm1.Height;
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
vrag1.Top:=vrag1.Top+skorost;
if vrag1.Top+vrag1.Height>Form1.Height then
begin
vrag1.Left:=RandomRange(Image2.Left,Image2.Width);
vrag1.Top:=-vrag1.Height;
skorost:=skorost+5;
Timer2.Interval:=Timer2.Interval-1;
end;
if car.Top+car.Height>vrag1.Top then
if (vrag1.Top+vrag1.Height>car.Top)then
if (vrag1.left+vrag1.Width>car.Left) and (vrag1.left begin
PlaySound(nil, 0, 0);
sndPlaySound('bam.wav',SND_ASYNC) ;
car.Picture.LoadFromFile('fire.bmp');
vrag1.Picture.LoadFromFile('fire.bmp');
Timer1.Enabled:=false;
Timer2.Enabled:=false;
Button1.Enabled:=true;
end;
end;
end.



Скачать