home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / hra.exe / swatmain.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  6KB  |  208 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include <stdlib.h>
  10. #include "swatmain.h"
  11. #include "about.h"
  12. #include "options.h"
  13. //---------------------------------------------------------------------------
  14. #pragma resource "*.dfm"
  15. #pragma resource "extrares.RES"
  16. TSwatForm *SwatForm;
  17.  
  18. TPoint Holes[5] = {
  19.   { 10, 10 }, { 200, 10 }, { 100, 100 }, { 10, 200 }, { 200, 200 }
  20. };
  21.  
  22. //---------------------------------------------------------------------------
  23. __fastcall TSwatForm::TSwatForm(TComponent* Owner)
  24.   : TForm(Owner)
  25. {
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TSwatForm::DisplayAbout(TObject *Sender)
  29. {
  30.     AboutBox->ShowModal();
  31. }
  32. //---------------------------------------------------------------------
  33. void __fastcall TSwatForm::FormCreate(TObject *Sender)
  34. {
  35.     Screen->Cursors[crMaletUp] = LoadCursor((void *)HInstance, "Malet");
  36.     Screen->Cursors[crMaletDown] = LoadCursor((void *)HInstance, "MaletDown");
  37.     Screen->Cursor = TCursor(crMaletUp);
  38.  
  39.     randomize();
  40.  
  41.     Live = new Graphics::TBitmap;
  42.     Live->LoadFromResourceName((int)HInstance, "Live");
  43.     Dead = new Graphics::TBitmap;
  44.     Dead->LoadFromResourceName((int)HInstance, "Dead");
  45.  
  46.     IsGameOver = true;
  47.     IsPause = false;
  48.     LiveTime = 10;
  49.     Frequence = 20;
  50.     GameTime = 150;        // fifteen seconds
  51.  
  52.     Application->OnMinimize = Pause1Click;
  53.     Application->OnRestore = Pause1Click;
  54. }
  55. //---------------------------------------------------------------------
  56. void __fastcall TSwatForm::TimerTick(TObject *Sender)
  57. {
  58.     Timer1->Tag++;
  59.     int i = random(Frequence);
  60.     if (i < 5)
  61.     {
  62.         if (HoleInfo[i].Time == 0)
  63.         {
  64.             HoleInfo[i].Time = Timer1->Tag + LiveTime;
  65.             HoleInfo[i].Dead = false;
  66.             Canvas->Draw(Holes[i].x, Holes[i].y, Live);
  67.         }
  68.     }
  69.     for (i = 0; i < 5; i++)
  70.     {
  71.         if (Timer1->Tag > HoleInfo[i].Time && HoleInfo[i].Time != 0)
  72.         {
  73.             HoleInfo[i].Time = 0;
  74.             if (!HoleInfo[i].Dead)
  75.             {
  76.                 Score += MissedCritter;
  77.                 Escaped++;
  78.             }
  79.             Canvas->FillRect(Rect(Holes[i].x, Holes[i].y,
  80.                             Holes[i].x + Dead->Width, Holes[i].y + Dead->Height));
  81.         }
  82.     }
  83.     WriteScore();
  84.     if (Timer1->Tag >= GameTime)
  85.         Stop1Click(this);
  86. }
  87. //---------------------------------------------------------------------------
  88. void __fastcall TSwatForm::WriteScore(void)
  89. {
  90.     TimeLabel->Caption = IntToStr(GameTime - Timer1->Tag);
  91.     HitsLabel->Caption = IntToStr(Hits);
  92.     MissLabel->Caption = IntToStr(Miss);
  93.     EscapedLabel->Caption = IntToStr(Escaped);
  94.     ScoreLabel->Caption = IntToStr(Score);
  95. }
  96. void __fastcall TSwatForm::New1Click(TObject *Sender)
  97. {
  98.     Timer1->Enabled = true;
  99.     Timer1->Tag = 0;
  100.     Score = 0;
  101.     Hits = 0;
  102.     Miss = 0;
  103.     Escaped = 0;
  104.     if (IsPause)
  105.     {
  106.         IsPause = false;
  107.         Pause1->Caption = "&Pause";
  108.     }
  109.     GameOverImage->Visible = false;
  110.     IsGameOver = false;
  111.     memset(HoleInfo, 0, sizeof(HoleInfo));
  112.     New1->Enabled = false;
  113.     Options1->Enabled = false;
  114.     Stop1->Enabled = true;
  115. }
  116. //---------------------------------------------------------------------
  117. void __fastcall TSwatForm::Stop1Click(TObject *Sender)
  118. {
  119.     Timer1->Enabled = false;
  120.     IsPause = false;
  121.     GameOverImage->Visible = true;
  122.     IsGameOver = true;
  123.     Timer1->Tag = GameTime;
  124.     New1->Enabled = true;
  125.     Options1->Enabled = true;
  126.     Stop1->Enabled = false;
  127.     for (int i = 0; i < 5; i++)
  128.     {
  129.         if (HoleInfo[i].Time != 0)
  130.         {
  131.             Canvas->FillRect(Rect(Holes[i].x, Holes[i].y,
  132.                           Holes[i].x + Dead->Width, Holes[i].y + Dead->Height));
  133.         }
  134.     }
  135. }
  136. //---------------------------------------------------------------------
  137. void __fastcall TSwatForm::Pause1Click(TObject *Sender)
  138. {
  139.     if (IsGameOver)
  140.         return;
  141.  
  142.     if (IsPause)
  143.     {
  144.         IsPause = false;
  145.         Pause1->Caption = "&Pause";
  146.         Stop1->Enabled = true;
  147.         Timer1->Enabled = true;
  148.     }
  149.     else
  150.     {
  151.         IsPause = true;
  152.         Pause1->Caption = "&Continue";
  153.         Stop1->Enabled = false;
  154.         Timer1->Enabled = false;
  155.     }
  156. }
  157. //---------------------------------------------------------------------
  158. void __fastcall TSwatForm::Options1Click(TObject *Sender)
  159. {
  160.     OptionsDlg->ShowModal();
  161. }
  162. //---------------------------------------------------------------------
  163. void __fastcall TSwatForm::FormDestroy(TObject *Sender)
  164. {
  165.     delete Live;
  166.     delete Dead;
  167. }
  168. //---------------------------------------------------------------------
  169. void __fastcall TSwatForm::FormMouseDown(TObject *Sender, TMouseButton Button,
  170.     TShiftState Shift, int X, int Y)
  171. {
  172.     Screen->Cursor = TCursor(crMaletDown);
  173.  
  174.     if (IsGameOver || IsPause)
  175.         return;
  176.  
  177.     bool hit = false;
  178.     for (int i = 0; i < 5; i++)
  179.     {
  180.         if (!HoleInfo[i].Dead && HoleInfo[i].Time != 0)
  181.         {
  182.             if (X > Holes[i].x && X < (Holes[i].x + Live->Width) &&
  183.                 Y > Holes[i].y && Y < (Holes[i].y + Live->Height))
  184.             {
  185.                 Score += HitPoints;
  186.                 HoleInfo[i].Dead = true;
  187.                 HoleInfo[i].Time = Timer1->Tag + 2 * LiveTime;
  188.                 Hits++;
  189.                 hit = true;
  190.                 Canvas->Draw(Holes[i].x, Holes[i].y, Dead);
  191.             }
  192.         }
  193.     }
  194.     if (!hit)
  195.     {
  196.         Score += MissedPoints;
  197.         Miss++;
  198.     }
  199.     WriteScore();    
  200. }
  201. //---------------------------------------------------------------------------
  202. void __fastcall TSwatForm::FormMouseUp(TObject *Sender, TMouseButton Button,
  203.     TShiftState Shift, int X, int Y)
  204. {
  205.     Screen->Cursor = TCursor(crMaletUp);    
  206. }
  207. //---------------------------------------------------------------------------
  208.