home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / progs / CB / DATA.Z / SWATMAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-10  |  5.1 KB  |  175 lines

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