09 C++¿Î³ÌÉè¼Æ - ̹¿Ë´óÕ½ ÏÂÔر¾ÎÄ

3¡¢ÕâÊÇÒòΪ£¬ÎÒÃÇ´´½¨µÄÀ༯³ÉÁËCSpriteÀ࣬µ«ÊÇȴûÓаÑÕâ¸öÀàµÄÉùÃ÷Îļþinclude

½øÀ´¡£Òò´ËÎÒÃÇÔÚн¨ÀàµÄÍ·ÎļþÖУ¬×öÈçϲÙ×÷£º #include \4¡¢±àÒë³ÌÐò£¬·¢ÏÖÈçÏÂÌáʾ£º

´ò¿ªCommonClass.hÎļþ£¬ÕÒµ½CSpriteÀàµÄ¶¨Ò壬¿ÉÒÔ·¢ÏÖ¸ÃÀàûÓÐȱʡ¹¹Ô캯Êý£¬Ö»ÓÐÒ»¸ö´ø²ÎÊýµÄ¹¹Ô캯Êý¡£Òò´Ë£¬×÷ΪËüµÄ×ÓÀ࣬¹¹Ô캯ÊýÒ²±ØÐë´ø²ÎÊý£¬²¢ÇÒ½«²ÎÊý´«µÝ¸ø¸¸Àà¡£¹¹Ô캯ÊýµÄÉùÃ÷ºÍ¶¨ÒåÐÞ¸ÄÈçÏ£º CTankPlayer(const char* szName);

CTankPlayer::CTankPlayer(const char* szName):CSprite(szName) { }

5¡¢ÎªCTankPlayerÀàÌí¼Óm_iDir,m_fSpeedX,m_fSpeedY£¬m_iHpËĸö³ÉÔ±±äÁ¿£¨³ÉÔ±±ä

Á¿¡£ÓÃÀ´±íʾ̹¿ËÔÚXÖáºÍYÖá·½ÏòÉϵÄËÙ¶ÈÒÔ¼°ÔËÐеķ½Ïò£¬²¢ÇÒÔÚ¹¹Ô캯ÊýÖгõʼ»¯Îª0¡£ÕâÀï¹æ¶¨£¬m_iDirµÄֵΪ0¡¢1¡¢2¡¢3£¬·Ö±ð±íʾÉÏ¡¢ÓÒ¡¢Ï¡¢×ó¡£ ³ÉÔ±º¯ÊýµÄÉùÃ÷ºÍ¶¨ÒåÈçºÎÌí¼Ó£¬·ÃÎÊȨÏÞÊÇʲô£¬¿É²Î¿¼ÊµÑéÒ»£¬ÏÂÎIJ»ÔÙ¼ÌÐøÌáʾ£©£¬·Ö±ð±íʾÔ˶¯·½Ïò¡¢XÖá¡¢YÖáËÙ¶ÈÒÔ¼°ÑªÁ¿Öµ¡£±¾ÎĵµµÄÃüÃû²ÉÓÃÐÙÑÀÀûÃüÃû·¨£¬m_±íʾÀà³ÉÔ±±äÁ¿£¬i±íʾÕûÐÍ£¬f±íʾfloatÐÍ£¬sz±íʾ×Ö·ûÖ¸Õ룬g_±íʾȫ¾Ö±äÁ¿µÈ¡£

ͬʱÐèÒªÔÚpublicȨÏÞ϶¨Òå»ñÈ¡ºÍÉèÖÃÕâЩ±äÁ¿µÄGetºÍSet·½·¨£¬¿ÉÔÚLessonX.hÎļþÖÐÍê³É£º

//set·½·¨

void SetHp(int hp) {m_iHp = hp;} void SetDir(int dir) {m_iDir = dir;} void SetSpeedX(float speedX) {m_fSpeedX = speedX;} void SetSpeedY(float speedY) {m_fSpeedY = speedY;} //get·½·¨

8

ÉϺ£Èñ¸ñÈí¼þÓÐÏÞ¹«Ë¾ int GetHp() {return m_iHp;} int GetDir() {return m_iDir;} float GetSpeedX() {return m_fSpeedX;} float GetSpeedY() {return m_fSpeedY;}

6¡¢ÔÚCTankPlayerÀàµÄ¹¹Ô캯ÊýÍê³ÉÉÏÃæËĸö³ÉÔ±±äÁ¿µÄ³õʼ»¯¡£

CTankPlayer::CTankPlayer(const char* szName):CSprite(szName) //¶Ô¹¹Ô캯Êý½øÐÐʵÏÖ {

m_iDir=0; m_fSpeedX=0.f; m_fSpeedY=0.f;

m_iHp=2;

}

×ÓÀà¶ÔÏó´´½¨Ê±£¬ÒªÏȵ÷Óø¸ÀàµÄ¹¹Ô캯ÊýÍê³É¸¸Àಿ·ÖµÄ¹¹Ôì¡£Èç¹û¸¸ÀàûÓÐĬÈϹ¹Ô캯Êý£¬×ÓÀàµÄ¹¹Ô캯Êý±ØÐëÏÔʾµ÷Óø¸ÀàµÄ¹¹Ô캯Êý¡£CTankPlayer¹¹Ô캯Êýµ÷ÓÃCSpriteÀ๹Ô캯Êý£¬²¢½«²ÎÊýszNameµÄÖµ´«µÝ¸øËü£¬´Ó¶ø½«Ãû³ÆΪszNameµÄ¾«ÁéͼƬÓëCTankPlayer¶ÔÏó°ó¶¨ÆðÀ´¡£

7¡¢ÎªCTankPlayerÀàÌí¼ÓInitº¯Êý£¬¸Ãº¯ÊýÖ÷ÒªÓÃÀ´Íê³É¸ÃÀàµÄ³õʼ»¯¹¤×÷¡£ÕâÀÏÈ

µ÷ÓÃsetHp·½·¨ÉèÖÃѪÁ¿¡£È»ºóµ÷Óø¸ÀàµÄSetSpritePositionº¯Êý½«Ì¹¿Ë¾«ÁéÉèÖÃÔÚÆÁÄ»ÖÐÑë(0,0)´¦¡£½ÓÏÂÀ´¸ø̹¿Ë¾«ÁéÉèÖÃʱ¼ä±ß½çµÄ´óС£¬ÓëÊÀ½ç±ß½çµÄÅöײģʽΪWORLD_LIMIT_NULL£¬±íʾ¾«ÁéÓëÊÀ½ç±ß½çÅöײµÄÏìÓ¦ÓÉ´úÂëÍê³É£¬Í¬Ê±ÉèÖÃ̹¿ËÅöײģʽΪ·¢ËÍÅöײºÍ½ÓÊÕÅöײ¡£

void CTankPlayer::Init() {

SetHp(2);

SetSpritePosition(0.f,0.f);

SetSpriteWorldLimit(WORLD_LIMIT_NULL, -26, -22, 26, 22);

SetSpriteCollisionActive(1,1);//ÉèÖÃΪ¿ÉÒÔ½ÓÊܺͷ¢ÉúÅöײ

SetSpriteVisible(true); }

8¡¢Íê³ÉCTankPlayerÀàÏà¹Ø¶¨Òåºó£¬ÔÚCGameMainÀàÖÐÔö¼ÓÒ»¸ö˽ÓгÉÔ±±äÁ¿

m_pTankplayer£¬ÀàÐÍΪCTankPlayer*¡£ ×¢ÒâÔÚLessonX.hÌí¼ÓÍ·Îļþ£º

#include\

9¡¢°´Ï¿ոñ¼üºó£¬³ÌÐò»áµ÷ÓüüÅÌ°´ÏÂʼþÏìÓ¦º¯Êý£¬½«m_iGameStateµÄÖµ¸ÄΪ1£¬

ÓÎÏ·½øÈ뿪ʼ״̬£¨¼ûʵÑéÒ»£©¡£³ÌÐòÔÙ´ÎÖ´ÐÐCGameMain::GameMainLoopº¯Êýʱ£¬¸ù¾Ým_iGameStateµÄÖµµ÷Óò¢Ö´ÐÐCGameMain::GameInitº¯Êý¡£Òò´Ë£¬¿ÉÒÔÔڸú¯ÊýÖд´½¨ÎÒ·½Ì¹¿Ë£¬²¢µ÷ÓÃCTankPlayer::Initº¯ÊýÀ´Íê³É¸ÃÀà¶ÔÏóµÄ³õʼ»¯¹¤×÷¡£

m_pTankPlayer=new CTankPlayer(\н¨Ò»¸öÃû×ÖÊÇmyPlayerµÄÎÒ·½Ì¹¿Ë¶ÔÏó

m_pTankPlayer->CloneSprite(\ÎÒ·½Ì¹¿Ë¿Ë¡ÔÚfuncodeÄ£°åÖдæÔÚµÄÃû×ÖΪplayerµÄ̹¿Ë£¬±íʾн¨µÄ̹¿Ë¶ÔÏóÓÐÏÖÔÚ¾«ÁéµÄËùÓÐÊôÐÔ m_pTankPlayer->Init();

10¡¢½ÓÏÂÀ´£¬ÎÒÃÇΪCTankPlayerÀàÌí¼ÓOnMoveº¯Êý£¬²ÎÊýiKey¡¢bPress·Ö±ð±íʾ°´

ϵÄÊÇÄĸö°´¼üºÍ°´¼üÊÇ·ñ°´Ï¡£Ê×ÏÈÉùÃ÷¸Ãº¯Êý£¬·ÃÎÊȨÏÞΪpublic£º

9

void OnMove(int iKey, bool bPress);

11¡¢½Ó×Å£¬Íê³ÉOnMove·½·¨¡£

void CTankPlayer::OnMove(int iKey, bool bPress) {

if(bPress) {

switch (iKey) {

case KEY_W: SetDir(0); SetSpeedX(0); SetSpeedY(-8); break; case KEY_D: SetDir(1); SetSpeedX(8); SetSpeedY(0); break; case KEY_S: SetDir(2); SetSpeedX(0); SetSpeedY(8); break; case KEY_A: SetDir(3); SetSpeedX(-8); SetSpeedY(0); break; }

SetSpriteRotation(float(90*GetDir())); //Ó÷½ÏòÖµ³ËÓÚ90µÃµ½¾«ÁéÐýת¶ÈÊý SetSpriteLinearVelocity(GetSpeedX(),GetSpeedY()); } else {

if(iKey == KEY_W || iKey == KEY_D || iKey == KEY_S || iKey == KEY_A) {

SetSpeedX(0); SetSpeedY(0);

SetSpriteLinearVelocity(GetSpeedX(),GetSpeedY()); } } }

ÓòÎÊýbPressÀ´ÅжϼüÅÌÊÇ°´Ï»¹Êǵ¯Æð¡£Èç¹ûbPressΪfalse£¬±íʾ¼üÅ̵¯Æð£¬ÇÒµ¯ÆðµÄ¼üÊÇWASD¼üʱ£¬ÉèÖÃ̹¿ËµÄXÖáºÍYÖáÒƶ¯Ëٶȶ¼Îª0¡£Èç¹ûbPressΪ

10

ÉϺ£Èñ¸ñÈí¼þÓÐÏÞ¹«Ë¾

true£¬±íʾ¼üÅÌ°´Ï£¬¸ù¾Ý°´Ïµļü£¬Îªm_iDir,m_fSpeedX,m_fSpeedY¸³ÓèÏàÓ¦µÄÖµ¡£SetSpriteRotationºÍSetSpriteLinearVelocityº¯ÊýÓÃÀ´ÉèÖþ«ÁéµÄ½Ç¶ÈºÍÏßÐÔËٶȣ¬¾ßÌ庬Òå²Î¿¼CommonClass.hÎļþ¡£ 12¡¢ÔÚCGameMainÀàµÄOnKeyDownº¯ÊýÖУ¬Í¨¹ýµ÷ÓÃCTankPlayerÀàµÄOnMove·½·¨£¬

¸ù¾Ý°´Ïµļü£¬¿ØÖÆ̹¿Ë³¯Ö¸¶¨·½ÏòÔ˶¯¡£Ö»ÓÐÓÎÏ·½øÈ뿪ʼ״̬£¬°´¼ü²ÅÓÐЧ¡£×¢Ò⣺OnMove²ÎÊýbPressµÄֵΪtrue¡£ÔÚOnKeyDownº¯ÊýÖÐÌí¼Ó´úÂëÈçÏ£º

if(m_iGameState == 2) {

m_pTankPlayer->OnMove(iKey, true); }

13¡¢±àÒë²¢ÔËÐгÌÐò¡£°´¿Õ¸ñ¼ü¿ªÊ¼£¬°´WASD¼ü£¬Ì¹¿Ë»áÏò¹æ¶¨µÄ·½ÏòÔ˶¯¡£µ«ÊÇËÉ

¿ª°´¼üºó£¬Ì¹¿Ë¼ÌÐøÇ°½ø£¬²»»áÍ£Ö¹ÏÂÀ´¡£

14¡¢ÔÚCGameMain::OnKeyUpº¯ÊýÖе÷ÓÃCTankPlayerµÄOnMove·½·¨£¬²ÎÊýbPressµÄ

ֵΪfalse¡£

void CGameMain::OnKeyUp(const int iKey) {

if(m_iGameState == 2) {

m_pTankPlayer->OnMove(iKey, false); } }

15¡¢±àÒë²¢ÔËÐгÌÐò£¬°´ÏÂWASD¼ü£¬Ì¹¿ËÔËÐУ»ËÉ¿ª°´¼ü£¬Ì¹¿ËÍ£Ö¹¡£½ÓÏÂÀ´£¬ÎÒÃÇ

À´ÊµÏÖ̹¿ËÔËÐе½ºÚÉ«ÇøÓò±ß½çÍ£Ö¹Ô˶¯µÄЧ¹û¡£

16¡¢µ±Ì¹¿Ë¾«ÁéÅöµ½±ß½çʱ£¬½«¾«ÁéµÄËٶȶ¼ÉèΪ0£¬¾«ÁéÍ£Ö¹Ô˶¯¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒª

¸ø¸Ã¾«ÁéÉèÖÃÊÀ½ç±ß½çµÄÅöײģʽ¡£¿ÉÒÔÔÚCTankPlayer::GameInit()ÖÐÍê³É¡£ 17¡¢ÓÃ×Ö·û´®´¦Àíº¯Êýstrstr()À´ÅжÏÅöµ½ÊÀ½ç±ß½çµÄ¾«ÁéÊÇ·ñΪÎÒ·½Ì¹¿Ë¡£µ±Åöµ½ÊÀ½ç

±ß½çµÄ¾«ÁéÃû×ÖÖк¬ÓС±player¡±µÄʱºò£¬strstr»á·µ»ØÒ»¸ö·Ç¿ÕÖµ¡£Òò´Ë´úÂëÈçÏ£º

void CGameMain::OnSpriteColWorldLimit( const char *szName, const int iColSide ) {

if(strstr(szName,\ÅжÏÅöµ½ÊÀ½ç±ß½çµÄ̹¿ËÊÇ·ñΪÎÒ·½Ì¹¿Ë {

m_pTankPlayer->SetSpriteLinearVelocity(0,0); } }

ʵÑéÈý ̹¿Ë¿ªÅÚ

¡¾ÊµÑéÄÚÈÝ¡¿

1¡¢´´½¨×Óµ¯ÀàCBullet£»

2¡¢Í¨¹ý°´¼üJ¿ØÖÆ̹¿Ë¿ªÅÚ£» ¡¾ÊµÑéÔËÐнá¹û¡¿

11