Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [Delphi]как правильно переделать игру в ООП


Автор: vmagdn 7.12.2008, 20:26
Код

unit UMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, jpeg;

type
  TMainForm = class(TForm)
    Car: TImage;
    Block1: TImage;
    Block2: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Bevel3: TBevel;
    Timer1: TTimer;
    Timer2: TTimer;
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  Bonus:integer;

implementation

{$R *.dfm}

procedure GameOver;
begin
 MainForm.Timer1.Enabled:=false;
 MainForm.Timer2.Enabled:=false;
 ShowMessage('Аварія');
end;

procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 //Управление машиной кнопками мышки
 If (Button=mbLeft) and (Car.left>0) then
    Car.left:=Car.left-(Car.width div 2);

 If (Button=mbRight) and (Car.Left<(450-Car.Width)) then
    Car.Left:=Car.Left+(Car.Width div 2);
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
close;
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
 //Располагаем препятсвия
 Block1.Left:=128;
 Block1.Top:=288;
 Block2.Left:=320;
 Block2.Top:=50;
 Bonus:=0;
 Label1.Caption:='0';
 //Показываем препятствия
 Block1.Show;
 Block2.Show;
 Car.Show;
 //Включаем все таймеры;
 Timer1.Enabled:=true;
 Timer2.Enabled:=true;
end;

procedure TMainForm.Timer1Timer(Sender: TObject);
begin
 //Отображаем очки игрока
 Label1.Caption:=IntToStr(Bonus);

 //Задаем движение препятствия1
  Block1.Top:=Block1.Top+(block1.height div 2);

 //Проверяем не произошла ли авария
 if (Block1.Top+Block1.Height)>(Car.Top) then
   if (Block1.Left+1<=Car.Left) and (Car.Left<=(Block1.Left+Block1.Width-1))
    then  GameOver;

 if (Block1.Top+Block1.Height)>(Car.Top) then
   if (Block1.Left+1<=(Car.Left+Car.Width)) and ((Car.Left+Car.Width)<=(Block1.Left+Block1.Width-1))
       then GameOver;

//Дошло ли препятствие до нижней границы
 If  Block1.Top>=(MainForm.ClientHeight) then
 begin
        Bonus:=Bonus+10;
        Block1.Hide;
        Block1.Top:=1;

        //препятствие не должно вылазить за левую границу
        repeat
          Block1.Left:=Random(450-Car.Width);
        until (Block1.Left>10);
        Block1.Show;
 end;
end;

procedure TMainForm.Timer2Timer(Sender: TObject);
begin
//Отображаем очки игрока
 Label1.Caption:=IntToStr(Bonus);

 //Задаем движение препятствия1
  Block2.Top:=Block2.Top+(Block2.Height div 2);

 //Проверяем не произошла ли авария
 if (Block2.Top+Block2.Height)>(Car.Top) then
   if (Block2.Left+1<=Car.Left) and (Car.Left<=(Block2.Left+Block2.Width-1))
    then GameOver;

 if (Block2.Top+Block2.Height)>(Car.Top) then
   if (Block2.Left+1<=(Car.Left+Car.Width)) and ((Car.Left+Car.Width)<=(Block2.Left+Block2.Width-1))
       then GameOver;

 //Дошло ли препятствие до нижней границы
 If  Block2.Top>=(MainForm.ClientHeight) then
 begin
        Bonus:=Bonus+10;
        Block2.Hide;
        Block2.Top:=1;

       //препятствие не должно вылазить за левую границу
        repeat
          Block2.Left:=Random(450-Car.Width);
        until (Block2.Left>10);
        Block2.Show;
 end;
end;

end. 

Автор: SneG0K 7.12.2008, 20:47
Код

unit UMain;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, jpeg;
type
  TMainForm = class(TForm)
    Car: TImage;
    Block1: TImage;
    Block2: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Bevel3: TBevel;
    Timer1: TTimer;
    Timer2: TTimer;
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure GameOver;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  MainForm: TMainForm;
  Bonus:integer;
implementation
{$R *.dfm}
procedure TMainForm.GameOver;
begin
 MainForm.Timer1.Enabled:=false;
 MainForm.Timer2.Enabled:=false;
 ShowMessage('Аварія');
end;
procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 //Управление машиной кнопками мышки
 If (Button=mbLeft) and (Car.left>0) then
    Car.left:=Car.left-(Car.width div 2);
 If (Button=mbRight) and (Car.Left<(450-Car.Width)) then
    Car.Left:=Car.Left+(Car.Width div 2);
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
close;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
 //Располагаем препятсвия
 Block1.Left:=128;
 Block1.Top:=288;
 Block2.Left:=320;
 Block2.Top:=50;
 Bonus:=0;
 Label1.Caption:='0';
 //Показываем препятствия
 Block1.Show;
 Block2.Show;
 Car.Show;
 //Включаем все таймеры;
 Timer1.Enabled:=true;
 Timer2.Enabled:=true;
end;
procedure TMainForm.Timer1Timer(Sender: TObject);
begin
 //Отображаем очки игрока
 Label1.Caption:=IntToStr(Bonus);
 //Задаем движение препятствия1
  Block1.Top:=Block1.Top+(block1.height div 2);
 //Проверяем не произошла ли авария
 if (Block1.Top+Block1.Height)>(Car.Top) then
   if (Block1.Left+1<=Car.Left) and (Car.Left<=(Block1.Left+Block1.Width-1))
    then  GameOver;
 if (Block1.Top+Block1.Height)>(Car.Top) then
   if (Block1.Left+1<=(Car.Left+Car.Width)) and ((Car.Left+Car.Width)<=(Block1.Left+Block1.Width-1))
       then GameOver;
//Дошло ли препятствие до нижней границы
 If  Block1.Top>=(MainForm.ClientHeight) then
 begin
        Bonus:=Bonus+10;
        Block1.Hide;
        Block1.Top:=1;
        //препятствие не должно вылазить за левую границу
        repeat
          Block1.Left:=Random(450-Car.Width);
        until (Block1.Left>10);
        Block1.Show;
 end;
end;
procedure TMainForm.Timer2Timer(Sender: TObject);
begin
//Отображаем очки игрока
 Label1.Caption:=IntToStr(Bonus);
 //Задаем движение препятствия1
  Block2.Top:=Block2.Top+(Block2.Height div 2);
 //Проверяем не произошла ли авария
 if (Block2.Top+Block2.Height)>(Car.Top) then
   if (Block2.Left+1<=Car.Left) and (Car.Left<=(Block2.Left+Block2.Width-1))
    then GameOver;
 if (Block2.Top+Block2.Height)>(Car.Top) then
   if (Block2.Left+1<=(Car.Left+Car.Width)) and ((Car.Left+Car.Width)<=(Block2.Left+Block2.Width-1))
       then GameOver;
 //Дошло ли препятствие до нижней границы
 If  Block2.Top>=(MainForm.ClientHeight) then
 begin
        Bonus:=Bonus+10;
        Block2.Hide;
        Block2.Top:=1;
       //препятствие не должно вылазить за левую границу
        repeat
          Block2.Left:=Random(450-Car.Width);
        until (Block2.Left>10);
        Block2.Show;
 end;
end;
end. 

Автор: vmagdn 11.12.2008, 20:17
а как именно через компоненты?

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)