⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch29.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 4 页
字号:
{

public:

  __fastcall THero(TGame *AGame): TByzCharacter(AGame) {}

};



class TBadGuy : public TByzCharacter

{

public:

  __fastcall TBadGuy(TGame *AGame): TByzCharacter(AGame) {}

};





class TByzGame : public TGame

{


protected:

  virtual void CreateCharacters(void);

public:

  __fastcall TByzGame(void): TGame() {}

};



extern TByzGame *ByzGame;



#endif

</FONT></PRE>
<H3 ALIGN="CENTER"><FONT COLOR="#0066FF"></FONT></H3>
<P><A NAME="Heading12"></A><FONT 
COLOR="#000077"><B>Listing 29.4. The main source
for the game objects specific to Byzantium.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////

// File: ByzEngine1.cpp

// Project: GameObjects

// Copyright (c) 1997 by 
Charlie Calvert

//

#include &lt;vcl.h&gt;

#pragma hdrstop

#include &quot;ByzEngine1.h&quot;



TByzGame *ByzGame;



void TByzGame::CreateCharacters(void)

{

  Hero = new THero(this);

  BadGuy = new TBadGuy(this);

}



__fastcall 
TByzCharacter::TByzCharacter(TGame *AGame)

  : TCharacter(AGame)

{

}



__fastcall TByzCharacter::~TByzCharacter(void)

{

}



int TByzCharacter::GetArmor(void)

{

  return Creature-&gt;GetCustomInt(&quot;Armor&quot;);

}



void 
TByzCharacter::SetArmor(int Value)

{

  Creature-&gt;SetCustomInt(&quot;Armor&quot;, Value);

}



int TByzCharacter::GetHitPoints(void)

{

  return Creature-&gt;GetCustomInt(&quot;Hit Points&quot;);

}



void TByzCharacter::SetHitPoints(int Value)


{

  Creature-&gt;SetCustomInt(&quot;Hit Points&quot;, Value);

}



int TByzCharacter::GetHunger(void)

{

  return Creature-&gt;GetCustomInt(&quot;Hunger&quot;);

}



void TByzCharacter::SetHunger(int Value)

{

  
Creature-&gt;SetCustomInt(&quot;Hunger&quot;, Value);

}



int TByzCharacter::GetWeapon(void)

{

  return Creature-&gt;GetCustomInt(&quot;Weapon&quot;);

}



void TByzCharacter::SetWeapon(int Value)

{

  
Creature-&gt;SetCustomInt(&quot;Weapon&quot;, Value);

}



int TByzCharacter::GetStrength(void)

{

  return Creature-&gt;GetCustomInt(&quot;Strength&quot;);

}



void TByzCharacter::SetStrength(int Value)

{

  
Creature-&gt;SetCustomInt(&quot;Strength&quot;, Value);

}



void TByzCharacter::SetVisible(bool Value)

{

  Creature-&gt;Visible = Value;

  FVisible = Value;

  if (!Value)

    ByzGame-&gt;UpdateMap();

}



int GetResistanceChance()

{

  int i 
= random(49);

  i -= (24);

  return i;

}



int GetWeaponChance()

{

  return 0;

}



void PlaySound(AnsiString S)

{

  sndPlaySound(S.c_str(), SND_ASYNC);

}



bool TByzCharacter::DefendYourself(TByzCharacter *Attacker)

{

  int Resistance = 
(Strength - Attacker-&gt;Strength) + (Armor - Attacker-&gt;Weapon);



  if (Resistance + GetResistanceChance() &lt; 0)

  {

    HitPoints -= (Attacker-&gt;Weapon - GetWeaponChance());

    PlaySound(&quot;..\\media\\bang.wav&quot;);

    return 
False;

  }

  else

  {

    PlaySound(&quot;..\\media\\rev.wav&quot;);

    return True;

  }

}

</FONT></PRE>
<H3 ALIGN="CENTER"><FONT COLOR="#0066FF"></FONT></H3>
<P><A NAME="Heading13"></A><FONT COLOR="#000077"><B>Listing 29.5. The header file

for the game form.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////

// GameForm1.h

// Byzantium Project

// Copyright (c) 1997 by Charlie Calvert

//

#ifndef GameForm1H

#define GameForm1H

#include 
&lt;Classes.hpp&gt;

#include &lt;Controls.hpp&gt;

#include &lt;StdCtrls.hpp&gt;

#include &lt;Forms.hpp&gt;

#include &quot;globals.h&quot;

#include &quot;Creatures1.hpp&quot;

#include &quot;FightClass1.h&quot;

#include &quot;Mercury2.h&quot;




class TGameForm : public TForm

{

__published:

  THermes *Hermes1;

  THermesChart *HermesChart1;

  TFileCreatureList *FileCreatureList1;

  TScene *Scene1;

  TSpriteScene *SpriteScene1;

  TSprite *Hero1;

  TSprite *BadQueen1;

  void __fastcall 
FormShow(TObject *Sender);

  void __fastcall FormKeyDown(TObject *Sender, WORD &amp;Key, TShiftState Shift);

  

  void __fastcall FormDestroy(TObject *Sender);

  void __fastcall HermesChart1DrawScene(TObject *Sender);

  void __fastcall 
SpriteScene1SetupSurfaces(TObject *Sender);

  void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift, int X,

                                int Y);

  void __fastcall SpriteScene1DrawScene(TObject *Sender);

  void __fastcall 
FormMouseDown(TObject *Sender, TMouseButton Button,

                                TShiftState Shift, int X, int Y);

  

  void __fastcall Scene1DrawScene(TObject *Sender);

  void __fastcall HermesChart1HeroMove(TObject *Sender, const tagPOINT 
&amp;NewPos,

                                       int NewType, bool &amp;MoveOk);

private:

//  TNotifyEvent FHitCreatureProc;

  TFightClass *FFightClass;

  MESSAGE void StartShow(TMessage &amp;Msg);

public:

  virtual __fastcall 
TGameForm(TComponent* Owner);

  void Run(void);

BEGIN_MESSAGE_MAP

  MESSAGE_HANDLER(WM_STARTSHOW, TMessage, StartShow);

END_MESSAGE_MAP(TForm);



};



extern TGameForm *GameForm;



#endif

</FONT></PRE>
<H3 ALIGN="CENTER"><FONT 
COLOR="#0066FF"></FONT></H3>
<P><A NAME="Heading14"></A><FONT COLOR="#000077"><B>Listing 29.6. The main source
for the game form.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////

// GameForm1.cpp

// Byzantium Project


// Copyright (c) 1997 by Charlie Calvert

//

#include &lt;vcl.h&gt;

#pragma hdrstop

#include &quot;Globals.h&quot;

#include &quot;ByzEngine1.h&quot;

#include &quot;GameForm1.h&quot;

#pragma link &quot;Creatures1&quot;

#pragma link 
&quot;Mercury2&quot;

#pragma resource &quot;*.dfm&quot;



TGameForm *GameForm;



__fastcall TGameForm::TGameForm(TComponent* Owner)

  : TForm(Owner)

{

  FFightClass = NULL;

  ByzGame = new TByzGame();

  ByzGame-&gt;CurrentScene = mrIntroMap;


}



void __fastcall TGameForm::FormDestroy(TObject *Sender)

{

  delete ByzGame;

}



///////////////////////////////////////

// Run

///////////////////////////////////////

void TGameForm::Run(void)

{

  if (FFightClass)

  {

    delete 
FFightClass;

    FFightClass = NULL;

  }

  

  switch(ByzGame-&gt;CurrentScene)

  {

    case mrWorldMap:

      Hermes1-&gt;Scene = HermesChart1;

      break;

    case mrIntroMap:

      Hermes1-&gt;Scene = Scene1;

      break;

    case 
mrFightMap:

      Hermes1-&gt;Scene = SpriteScene1;

      FFightClass = new TFightClass(Handle, SpriteScene1);

      break;

  }



  Hermes1-&gt;InitObjects();

  ByzGame-&gt;Initialize(this, Hermes1-&gt;CreatureList);

  Hermes1-&gt;Flip();

}




void __fastcall TGameForm::FormShow(TObject *Sender)

{

  PostMessage(Handle, WM_STARTSHOW, 0, 0);

}



void TGameForm::StartShow(TMessage &amp;Msg)

{

  Run();

}



void __fastcall TGameForm::FormKeyDown(TObject *Sender, WORD &amp;Key,

                                       
TShiftState Shift)

{

  if ((Shift.Contains(ssAlt)) &amp;&amp; (Key=='X'))

  {

    if (Hermes1-&gt;Exclusive)

      Hermes1-&gt;EndExclusive();

    Close();

  }

  else if ((Shift.Contains(ssAlt)) &amp;&amp; (Key=='A'))

  {

    
ByzGame-&gt;CurrentScene = mrIntroMap;

    Run();

  }

  else if ((Shift.Contains(ssAlt)) &amp;&amp; (Key=='B'))

  {

    ByzGame-&gt;CurrentScene = mrWorldMap;

    Run();

  }

  else if (ByzGame)

    
dynamic_cast&lt;THero*&gt;(ByzGame-&gt;Hero)-&gt;Move(Key);

}

void __fastcall TGameForm::HermesChart1DrawScene(TObject *Sender)

{

  AnsiString S;

  S = &quot;Col: &quot; + IntToStr(ByzGame-&gt;Hero-&gt;Col);

//  S = S + &quot; Scr Col: &quot; + 
IntToStr(ByzGame-&gt;Hero-&gt;Creature-&gt;ScreenCol);

//  S = S + &quot; Map Col: &quot; + IntToStr(Hermes1-&gt;CreatureList-&gt;MapCol);

  S = S + &quot;Hit Points: &quot; + dynamic_cast&lt;TByzCharacter*&gt;(ByzGame-&gt;Hero)-&gt;HitPoints;

  
HermesChart1-&gt;WriteXY(370, 410, S);

  S = &quot;Row: &quot; + IntToStr(ByzGame-&gt;Hero-&gt;Row);

//  S = S + &quot; Scr Row: &quot; + IntToStr(ByzGame-&gt;Hero-&gt;Creature-&gt;ScreenRow);

//  S = S + &quot; Map Row: &quot; + 
IntToStr(Hermes1-&gt;CreatureList-&gt;MapRow);

  S = S + &quot; Hunger: &quot; + dynamic_cast&lt;TByzCharacter*&gt;(ByzGame-&gt;Hero)-&gt;Hunger;

  HermesChart1-&gt;WriteXY(370, 430, S);

}



void __fastcall 
TGameForm::SpriteScene1SetupSurfaces(TObject *Sender)

{

  SpriteScene1-&gt;AddSprite(Hero1);

  SpriteScene1-&gt;AddSprite(BadQueen1);

}



void __fastcall TGameForm::FormMouseMove(TObject *Sender, TShiftState Shift,

                                         
int X, int Y)

{

  if (ByzGame-&gt;CurrentScene == mrFightMap)

  {

    if (BadQueen1-&gt;IsHit(X, Y))

      Screen-&gt;Cursor = crCross;

    else

      Screen-&gt;Cursor = crDefault;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -