Игра "Бомбер"

jpg.

Задача: создать игру, в которой необходимо уничтожать бомбами некоторые объекты.

Среда разработки: Delphi 7.

Суть игры - уничтожить наземные объекты, однако этому будут мешать защитные системы в виде ракеты, которая запускается при каждом новом заходе на цели.
Если "жизни" дирижабля опустятся до нуля - Вы проиграли.
В случае уничтожения всех объектов до уничтожения дирижабля - Вы выиграли.

Исходный код содержит комментарии по основным пунктам.

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Buttons, StdCtrls;
 
type
  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    BitBtn1: TBitBtn;
    Anim: TTimer;
    BitBtn2: TBitBtn;
    Bomb: TTimer;
    Enemy: TTimer;
    procedure BitBtn1Click(Sender: TObject); // запуск игры
    procedure AnimTimer(Sender: TObject);    // анимация движения дирижабля
    procedure BitBtn2Click(Sender: TObject); // сброс бомбы
    procedure BombTimer(Sender: TObject);    // анимация полета бомбы
    procedure Proverka(bx: Integer);         // функция, проверяющая поражение мишени
    procedure EnemyTimer(Sender: TObject);   // анимация полета ракеты
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  x,y: Integer; // Координаты дирижабля
  bx, by: Integer; // координаты бомбы
  Health: Integer; // жизни дирижабля
  ax,ay: Integer; // координаты радаров
  score:Integer; // счетчик сбитых радаров
  t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14: Integer; // переменные,
                 // необходимые для счета сбитых радаров
  ex,ey: Integer; // координаты ракеты
 
implementation
 
{$R *.dfm}
 
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  x:=10; y:=100;   // первоначальные координаты дирижабля
  Health:=100;    // жизни дирижабля
  ax:=100; ay:=600;  // стартовые координаты первого из рабаров
  ex:=30; ey:=590;   // координаты ракеты перед запуском
  score:=0;          // счетчик уничтоженных объектов
    t1:=1;  t2:=1; t3:=1;  t4:=1; t5:=1; t6:=1; t7:=1;
    t8:=1;  t9:=1; t10:=1; t11:=1;t12:=1;t13:=1;t14:=1;
  PaintBox1.Canvas.Brush.Color:=rgb(0, 155, 222);
  PaintBox1.Canvas.Pen.Style:=psClear;
  PaintBox1.Canvas.Rectangle(0, 0, 793, 689);
  PaintBox1.Canvas.Pen.Style:=psSolid;
  PaintBox1.Canvas.Brush.Color:=rgb(200, 200, 200);
  PaintBox1.Canvas.Rectangle(0, 600, 793, 689);
  While ax<790 do begin         // построение радаров:
      PaintBox1.Canvas.MoveTo(ax,ay);
      PaintBox1.Canvas.LineTo(ax,ay-20);
      PaintBox1.Canvas.LineTo(ax-10,ay-20);
      PaintBox1.Canvas.LineTo(ax-10,ay-10);
      PaintBox1.Canvas.LineTo(ax+10,ay-10);
      PaintBox1.Canvas.LineTo(ax+10,ay-20);
      PaintBox1.Canvas.LineTo(ax,ay-20);
      ax:=ax+50;
  end;
     // построение дирижабля:
  PaintBox1.Canvas.Brush.Color:=rgb(0, 0, 0);
  PaintBox1.Canvas.Polygon([Point(x+2,y+25), Point(x-2,y-10), Point(x+20,y-10), Point(x+40,y+10)]);
  PaintBox1.Canvas.Polygon([Point(x+2,y+25), Point(x-2,y+60), Point(x+20,y+60), Point(x+40,y+40)]);
  PaintBox1.Canvas.Brush.Color:=rgb(139, 20, 28);
  PaintBox1.Canvas.Rectangle(x+60, y+45, x+90, y+60);
  PaintBox1.Canvas.Brush.Color:=rgb(200, 200, 200);
  PaintBox1.Canvas.Ellipse(x,y,x+150,y+50);
  PaintBox1.Canvas.Arc(x, y+15, x+150, y+35, x, y+25, x+150,y+25);
  PaintBox1.Canvas.Brush.Color:=rgb(0, 0, 0);
  PaintBox1.Canvas.Polygon([Point(x+2,y+25), Point(x-2,y+30), Point(x+20,y+30), Point(x+40,y+25)]);
  Anim.Enabled:=True;  // запуск анимации дирижбля
  end;
 
procedure TForm1.AnimTimer(Sender: TObject);
begin
    PaintBox1.Canvas.Brush.Color:=rgb(0, 155, 222);
    PaintBox1.Canvas.Font.Size:=10;
    PaintBox1.Canvas.TextOut(50, 20, 'Health:  ' + IntToStr(Health));  // вывод кол-ва оставшихся НР
     x:=x+1;
     if x>793 then begin
     x:=10;
     y:=y+10;
     end;
     //перерисовка дирижабля
  PaintBox1.Canvas.Pen.Style:=psClear;
  PaintBox1.Canvas.Rectangle(x-10, y-205, x+150, y+65);
  PaintBox1.Canvas.Brush.Color:=rgb(0, 0, 0);
  PaintBox1.Canvas.Polygon([Point(x+2,y+25), Point(x-2,y-10), Point(x+20,y-10), Point(x+40,y+10)]);
  PaintBox1.Canvas.Polygon([Point(x+2,y+25), Point(x-2,y+60), Point(x+20,y+60), Point(x+40,y+40)]);
  PaintBox1.Canvas.Brush.Color:=rgb(139, 20, 28);
  PaintBox1.Canvas.Rectangle(x+60, y+45, x+90, y+60);
  PaintBox1.Canvas.Brush.Color:=rgb(200, 200, 200);
  PaintBox1.Canvas.Ellipse(x,y,x+150,y+50);
  PaintBox1.Canvas.Pen.Style:=psSolid;
  PaintBox1.Canvas.Arc(x, y+15, x+150, y+35, x, y+25, x+150,y+25);
  PaintBox1.Canvas.Brush.Color:=rgb(0, 0, 0);
  PaintBox1.Canvas.Polygon([Point(x+2,y+25), Point(x-2,y+30), Point(x+20,y+30), Point(x+40,y+25)]);
  if score>=14 then begin // проверка на завершение игры в пользу игрока
  anim.Enabled:=False;
  Bomb.Enabled:=False;
  PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
  PaintBox1.Canvas.Font.Size:=20;
  PaintBox1.Canvas.TextOut(350, 400, 'YOU WIN!!!');  // вывод сообщения об успешном завершении игры
  end;
  if (y>=100) and (x=760) then Enemy.Enabled:=true;  // запуск ракеты
  end;
 
procedure TForm1.BitBtn2Click(Sender: TObject); // сброс бомбы
begin
     Bomb.Enabled:=True;
     bx:=x;
     by:=y;
end;
 
procedure TForm1.BombTimer(Sender: TObject);   // построение бомбы и анимация ее падения
begin
  PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
  PaintBox1.Canvas.Pen.Style:=psClear;
  PaintBox1.Canvas.Rectangle(bx+49,by+50,bx+61,by+81);
  PaintBox1.Canvas.Pen.Style:=psSolid;
  PaintBox1.Canvas.Brush.Color:=rgb(0,0,0);
  PaintBox1.Canvas.Ellipse(bx+50,by+60,bx+60,by+80);
  by:=by+10;
  if by=530 then begin Proverka(bx);  // запуск функции проверки на уничтожение мишени
   Bomb.Enabled:=False;               // и остановка бомбы
  PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
  PaintBox1.Canvas.Pen.Style:=psClear;
  PaintBox1.Canvas.Rectangle(bx+49,by+50,bx+61,by+71);end;
end;
procedure TForm1.Proverka(bx: Integer);    // функция, проверяющая поражение целей
begin                                      // и считающая кол-во сбитых радаров
    if  (bx+60>90) and (bx+50<110) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(80,580, 120,600);
      score:=score+t1;
      t1:=0;
      end;
 
    if  (bx+60>140) and (bx+50<160) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(130,580, 170,600);
      score:=score+t2;
      t2:=0;
      end;
 
    if  (bx+60>190) and (bx+50<210) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(180,580, 220,600);
      score:=score+t3;
      t3:=0;
      end;
 
    if  (bx+60>240) and (bx+50<260) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(230,580, 270,600);
      score:=score+t4;
      t4:=0;
      end;
 
    if  (bx+60>290) and (bx+50<310) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(280,580, 320,600);
      score:=score+t5;
      t5:=0;
      end;
 
    if  (bx+60>340) and (bx+50<360) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(330,580, 370,600);
      score:=score+t6;
      t6:=0;
      end;
 
    if  (bx+60>390) and (bx+50<410) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(380,580, 420,600);
      score:=score+t7;
      t7:=0;
      end;
 
    if  (bx+60>440) and (bx+50<460) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(430,580, 470,600);
      score:=score+t8;
      t8:=0;
      end;
 
    if  (bx+60>490) and (bx+50<510) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(480,580, 520,600);
      score:=score+t9;
      t9:=0;
      end;
 
    if  (bx+60>540) and (bx+50<560) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(530,580, 570,600);
      score:=score+t10;
      t10:=0;
      end;
 
    if  (bx+60>590) and (bx+50<610) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(580,580, 620,600);
      score:=score+t11;
      t11:=0;
      end;
 
    if  (bx+60>640) and (bx+50<660) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(630,580, 670,600);
      score:=score+t12;
      t12:=0;
      end;
 
    if  (bx+60>690) and (bx+50<710) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(680,580, 720,600);
      score:=score+t13;
      t13:=0;
      end;
 
    if  (bx+740>90) and (bx+50<760) then begin
      PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
      PaintBox1.Canvas.Pen.Style:=psClear;
      PaintBox1.Canvas.Rectangle(730,580, 770,600);
      score:=score+t14;
      t14:=0;
      end;
end;
procedure TForm1.EnemyTimer(Sender: TObject); // анимация полета ракеты
begin
PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
PaintBox1.Canvas.Pen.Style:=psClear;
PaintBox1.Canvas.Rectangle(ex-6, ey-45, ex+12, ey+12);
PaintBox1.Canvas.Brush.Color:=rgb(0,0,0);
PaintBox1.Canvas.Pen.Style:=psSolid;
PaintBox1.Canvas.Polygon([Point(ex,ey-5),Point(ex-3,ey), Point(ex, ey-10), Point(ex, ey-35), Point(ex+2, ey-40), Point(ex+4, ey-35), Point(ex+4, ey-10), Point(ex+7, ey), Point(ex+4, ey-5)]);
ey:=ey-10;
if ey-40=120 then begin  // проверка на попадание в дирижабль и уменьшение жизней
  Enemy.Enabled:=False;
  Health:=Health-10;
  ex:=30; ey:=590;
  PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
  PaintBox1.Canvas.Pen.Style:=psSolid;
  if Health=0 then begin
    Anim.Enabled:=False; PaintBox1.Canvas.Brush.Color:=rgb(0,155,222);
    PaintBox1.Canvas.Pen.Style:=psClear;
    PaintBox1.Canvas.Rectangle(ex-6, ey-45, ex+12, ey+12);
    PaintBox1.Canvas.Font.Size:=20;
    PaintBox1.Canvas.TextOut(350, 400, 'Game OVER!!!'); // вывод сообщения, в случае проигрыша
  end;
end;
end;
 
 
 
end.

Ключевые слова: 
Игра, бомбежка, бомбер, анимация
ВложениеРазмер
game.rar165.19 кб