一、游戲內(nèi)容及特點描述 游戲內(nèi)容:玩家自己控制一輛黃色的坦克,而敵方坦克為白色,一秒發(fā)射一顆炮彈,玩家通過鍵盤的方向鍵來控制黃色坦克的前進方向,空格鍵來控制發(fā)射炮彈,當炮彈與白色的敵方坦克相撞時,白色坦克與炮彈一起消失,玩家積十分,當所有白色坦克被消滅時,玩家進入下一關(guān),當白色坦克發(fā)射的炮彈與玩家控制的黃色坦克累積相撞三次時,黃色坦克與炮彈一起消失,游戲結(jié)束,玩家獲得最終分數(shù)。 特點描述:1*玩家可自主控制坦克進行游戲,規(guī)避敵人子彈。 2*白色的敵方坦克方向隨機變動。 3*玩家壓Q鍵時可以向四面發(fā)射密集的炮彈。 4*游戲?qū)嵭嘘P(guān)卡制,當所有白色坦克都被消滅時,游戲進入下一關(guān)。 5*玩家有三次被地方坦克擊中而不結(jié)束游戲的機會。 6*游戲采用分數(shù)積分制,使游戲更富有挑戰(zhàn)性。
C++語言源程序如下:
- Bomb.cpp
- #include "Bomb.h"
- Bomb::Bomb()
- {
- this->m_bDisappear = false;
- this->m_color = YELLOW;
- this->m_dir = UP;
- }
- Bomb::Bomb(Point pos, BombType type) : Bomb()
- {
- this->m_pos = pos;
- this->m_type = type;
- switch (m_type)
- {
- case LARGE:
- m_timer = 8;
- break;
- case SMALL:
- m_timer = 4;
- break;
- default:
- break;
- }
- }
- Bomb.h
- void Bomb::Display()
- {
- COLORREF fill_color_save = getfillcolor();
- COLORREF color_save = getcolor();
- setfillcolor(m_color);
- setcolor(RED);
- fillcircle(m_pos.GetX(), m_pos.GetY(), 8 - m_timer);
- setcolor(color_save);
- setfillcolor(fill_color_save);
- }
- void Bomb::Move()
- {
- m_timer -= 2;
- if (m_timer < 0)
- {
- m_bDisappear = true;
- }
- }
- bool Bomb::IsDisappear()
- {
- return m_bDisappear;
- }
- void Bomb::Boom(list<Object*>& lstBombs)
- {
- // Do nothing
- }
- void Bomb::CalculateSphere()
- {
- // Do nothing
- }
- #ifndef __BOMB_H__
- #define __BOMB_H__
- #include "Object.h"
- enum BombType
- {
- LARGE,
- SMALL
- };
- class Bomb : public Object
- {
- public:
- Bomb();
- Bomb(Point pos, BombType type);
- ~Bomb(){}
- void Display();
- void Move();
- void Boom(list<Object*>& lstBombs);
-
- bool IsDisappear();
- void SetDisappear(){}
- Rect GetSphere()
- {
- return m_rectSphere;
- }
- protected:
- void CalculateSphere();
-
- BombType m_type;
- int m_timer;
- };
- #endif
- Bullet.cpp
- #include "Bullet.h"
- #include "Bomb.h"
- Bullet::Bullet()
- {
- }
- Bullet::Bullet(Point pos, Dir dir, COLORREF color)
- {
- m_pos = pos;
- m_dir = dir;
- m_color = color;
- m_step = 6;
- m_bDisappear = false;
- CalculateSphere();
- }
- Bullet::~Bullet()
- {
- }
- // 繪圖
- void Bullet::Display()
- {
- COLORREF fill_color_save = getfillcolor();
- COLORREF color_save = getcolor();
- setfillcolor(m_color);
- setcolor(m_color);
- fillcircle(m_pos.GetX(), m_pos.GetY(), 4);
-
- setcolor(color_save);
- setfillcolor(fill_color_save);
- }
- // 移動
- void Bullet::Move()
- {
- switch (m_dir)
- {
- case UP:
- m_pos.SetY(m_pos.GetY() - m_step);
- CalculateSphere();
- if (m_rectSphere.GetStartPoint().GetY() < Graphic::GetBattleGround().GetStartPoint().GetY())
- {
- m_pos.SetY(Graphic::GetBattleGround().GetStartPoint().GetY());
- m_bDisappear = true;
- }
- break;
- case DOWN:
- m_pos.SetY(m_pos.GetY() + m_step);
- CalculateSphere();
- if (m_rectSphere.GetEndPoint().GetY() > Graphic::GetBattleGround().GetEndPoint().GetY())
- {
- m_pos.SetY(Graphic::GetBattleGround().GetEndPoint().GetY());
- m_bDisappear = true;
- }
- break;
- case LEFT:
- m_pos.SetX(m_pos.GetX() - m_step);
- CalculateSphere();
- if (m_rectSphere.GetStartPoint().GetX() < Graphic::GetBattleGround().GetStartPoint().GetX())
- {
- m_pos.SetX(Graphic::GetBattleGround().GetStartPoint().GetX());
- m_bDisappear = true;
- }
- break;
- case RIGHT:
- m_pos.SetX(m_pos.GetX() + m_step);
- CalculateSphere();
- if (m_rectSphere.GetEndPoint().GetX() > Graphic::GetBattleGround().GetEndPoint().GetX())
- {
- m_pos.SetX(Graphic::GetBattleGround().GetEndPoint().GetX());
- m_bDisappear = true;
- }
- break;
- default:
- break;
- }
- }
- void Bullet::Boom(list<Object*>& lstBombs)
- {
- lstBombs.push_back(new Bomb(m_pos, SMALL));
- }
- void Bullet::CalculateSphere()
- {
- m_rectSphere.Set(m_pos.GetX() - 2, m_pos.GetY() - 2, m_pos.GetX() + 2, m_pos.GetY() + 2);
- }
- Bullet.h
- #ifndef __BULLET_H__
- #define __BULLET_H__
- #include "Object.h"
- class Bullet : public Object
- {
- public:
- Bullet();
- Bullet(Point pos, Dir dir, COLORREF color);
- ~Bullet();
- void Display();
- void Move();
- void Boom(list<Object*>& lstBombs);
- bool IsDisappear()
- {
- return m_bDisappear;
- }
- Rect GetSphere()
- {
- return m_rectSphere;
- }
- void SetStep(int nStep)
- {
- m_step = nStep;
- }
- void SetDisappear()
- {
- m_bDisappear = true;
- }
- protected:
- void CalculateSphere();
- };
- #endif
- EnemyTank.cpp
- #include "EnemyTank.h"
- #include "Bullet.h"
- void EnemyTank::RandomTank()
- {
- m_pos.SetX(rand() % (Graphic::GetBattleGround().GetWidth() - 30) + 15);
- m_pos.SetY(rand() % (Graphic::GetBattleGround().GetHeight() - 30) + 15);
- m_color = WHITE;
- m_dir = (Dir)(Dir::UP + (rand() % 4));
- m_step = 1;
- m_stepCnt = rand();
- }
- void EnemyTank::RandomDir(int type)
- {
- if (type == 1)
- {
- Dir dir;
- while ((dir = (Dir)(Dir::UP + (rand() % 4))) == m_dir)
- {
- // Do nothing
- }
- m_dir = dir;
- }
- else
- {
- m_dir = (Dir)(Dir::UP + (rand() % 4));
- }
- }
- void EnemyTank::Display()
- {
- COLORREF fill_color_save = getfillcolor();
- COLORREF color_save = getcolor();
- setfillcolor(m_color);
- setcolor(m_color);
-
- fillrectangle(m_pos.GetX() - 6, m_pos.GetY() - 6, m_pos.GetX() + 6, m_pos.GetY() + 6);
- fillrectangle(m_rectSphere.GetStartPoint().GetX(), m_rectSphere.GetStartPoint().GetY(),
- m_rectSphere.GetStartPoint().GetX() + 4, m_rectSphere.GetStartPoint().GetY() + 4);
- fillrectangle(m_rectSphere.GetEndPoint().GetX() - 4, m_rectSphere.GetStartPoint().GetY(),
- m_rectSphere.GetEndPoint().GetX(), m_rectSphere.GetStartPoint().GetY() + 4);
- fillrectangle(m_rectSphere.GetStartPoint().GetX(), m_rectSphere.GetEndPoint().GetY() - 4,
- m_rectSphere.GetStartPoint().GetX() + 4, m_rectSphere.GetEndPoint().GetY());
- fillrectangle(m_rectSphere.GetEndPoint().GetX() - 4, m_rectSphere.GetEndPoint().GetY() - 4,
- m_rectSphere.GetEndPoint().GetX(), m_rectSphere.GetEndPoint().GetY());
-
- switch (m_dir)
- {
- case UP:
- line(m_pos.GetX(), m_pos.GetY(), m_pos.GetX(), m_pos.GetY() - 15);
- break;
- case DOWN:
- line(m_pos.GetX(), m_pos.GetY(), m_pos.GetX(), m_pos.GetY() + 15);
- break;
- case LEFT:
- line(m_pos.GetX(), m_pos.GetY(), m_pos.GetX() - 15, m_pos.GetY());
- break;
- case RIGHT:
- line(m_pos.GetX(), m_pos.GetY(), m_pos.GetX() + 15, m_pos.GetY());
- break;
- default:
- break;
- }
- setcolor(color_save);
- setfillcolor(fill_color_save);
- }
- void EnemyTank::Move()
- {
- switch (m_dir)
- {
- case UP:
- m_pos.SetY(m_pos.GetY() - m_step);
- if (m_rectSphere.GetStartPoint().GetY() < Graphic::GetBattleGround().GetStartPoint().GetY())
- {
- m_pos.SetY(m_pos.GetY() + m_step);
- this->RandomDir(1);
- }
- break;
- case DOWN:
- m_pos.SetY(m_pos.GetY() + m_step);
- if (m_rectSphere.GetEndPoint().GetY() > Graphic::GetBattleGround().GetEndPoint().GetY())
- {
- m_pos.SetY(m_pos.GetY() - m_step);
- this->RandomDir(1);
- }
- break;
- case LEFT:
- m_pos.SetX(m_pos.GetX() - m_step);
- if (m_rectSphere.GetStartPoint().GetX() < Graphic::GetBattleGround().GetStartPoint().GetX())
- {
- m_pos.SetX(m_pos.GetX() + m_step);
- this->RandomDir(1);
- }
- break;
- case RIGHT:
- m_pos.SetX(m_pos.GetX() + m_step);
- if (m_rectSphere.GetEndPoint().GetX() > Graphic::GetBattleGround().GetEndPoint().GetX())
- {
- m_pos.SetX(m_pos.GetX() - m_step);
- this->RandomDir(1);
- }
- break;
- default:
- break;
- }
- CalculateSphere();
- m_stepCnt++;
- if (m_stepCnt % MAX_STEP_TURN == 0)
- {
- //m_stepCnt = 0;
- this->RandomDir(0);
- }
- if (m_stepCnt % MAX_STEP_SHOOT == 0)
- {
- m_bNeedShoot = true;
- }
- }
- void EnemyTank::CalculateSphere()
- {
- switch (m_dir)
- {
- case UP:
- case DOWN:
- m_rectSphere.Set(m_pos.GetX() - 13, m_pos.GetY() - 10, m_pos.GetX() + 13, m_pos.GetY() + 10);
- break;
- case LEFT:
- case RIGHT:
- m_rectSphere.Set(m_pos.GetX() - 10, m_pos.GetY() - 13, m_pos.GetX() + 10, m_pos.GetY() + 13);
- break;
- default:
- break;
- }
- }
- void EnemyTank::Shoot(list<Object*>& lstBullets)
- {
- Bullet* pBullet = new Bullet(m_pos, m_dir, m_color);
- lstBullets.push_back(pBullet);
- m_bNeedShoot = false;
- }
- EnemyTank.h
- #ifndef __ENEMY_TANK__
- #define __ENEMY_TANK__
- #include "Tank.h"
- #define MAX_STEP_TURN 20
- #define MAX_STEP_SHOOT 15
- class EnemyTank : public Tank
- {
- public:
- EnemyTank()
- {
- RandomTank();
- }
- ~EnemyTank(){}
- void Display();
- void Move();
- void Shoot(list<Object*>& lstBullets);
- protected:
- void CalculateSphere();
- void RandomTank();
- // 隨機產(chǎn)生坦克方向 type: 1, 新方向必須與之前方向不同 2, 任意一個新方向
- void RandomDir(int type);
- int m_stepCnt;
- };
- #endif
- Graphic.h
- #ifndef __GRAPHIC_H__
- #define __GRAPHIC_H__
- #include <graphics.h>
- #include "model/Rect.h"
- #define SCREEN_WIDTH 1024
- #define SCREEN_HEIGHT 768
- #define BATTLE_GROUND_X1 5
- #define BATTLE_GROUND_Y1 5
- #define BATTLE_GROUND_X2 800
- #define BATTLE_GROUND_Y2 (SCREEN_HEIGHT - BATTLE_GROUND_Y1)
- class Graphic
- {
- public:
- static void Create();
- static void Destroy();
- static void DrawBattleGround();
- static int GetScreenWidth();
- static int GetScreenHeight();
- static Rect GetBattleGround();
- static void ShowScore();
- static void ShowGameLevel(int nLevel);
- static void ShowGameOver();
- private:
- static Rect m_rectScreen;
- static Rect m_rectBattleGround;
- static char m_pArray[100];
- };
- #endif
- Graphic.cpp
- #include "Graphic.h"
- #include "Setting.h"
- Rect Graphic::m_rectScreen;
- Rect Graphic::m_rectBattleGround;
- char Graphic::m_pArray[100];
- void Graphic::Create()
- {
- m_rectScreen.Set(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
- initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
- setbkcolor(DARKGRAY);
- m_rectBattleGround.Set(BATTLE_GROUND_X1, BATTLE_GROUND_Y1, BATTLE_GROUND_X2, BATTLE_GROUND_Y2);
- }
- void Graphic::Destroy()
- {
- closegraph();
- }
- void Graphic::DrawBattleGround()
- {
- rectangle(m_rectBattleGround.GetStartPoint().GetX(), m_rectBattleGround.GetStartPoint().GetY(),
- m_rectBattleGround.GetEndPoint().GetX(), m_rectBattleGround.GetEndPoint().GetY());
- }
- int Graphic::GetScreenWidth()
- {
- return SCREEN_WIDTH;
- }
- int Graphic::GetScreenHeight()
- {
- return SCREEN_HEIGHT;
- }
- Rect Graphic::GetBattleGround()
- {
- return m_rectBattleGround;
- }
- const int SCORE_LEFT = 810;
- const int SCORE_TOP = 5;
- void Graphic::ShowScore()
- {
- COLORREF fill_color_save = getfillcolor();
- COLORREF color_save = getcolor();
- rectangle(SCORE_LEFT, SCORE_TOP, SCORE_LEFT + 200, SCORE_TOP + 40);
- RECT r = { SCORE_LEFT, SCORE_TOP, SCORE_LEFT + 200, SCORE_TOP + 40 };
- wsprintf((LPWSTR)m_pArray, _T("第 %d 關(guān)"), Setting::GetGameLevel());
- drawtext((LPWSTR)m_pArray, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- r.top += 50;
- r.bottom += 50;
- wsprintf((LPWSTR)m_pArray, _T("分 數(shù) : %d"), Setting::GetSumScore());
- drawtext((LPWSTR)m_pArray, &r, DT_VCENTER | DT_SINGLELINE);
- r.top += 50;
- r.bottom += 50;
- wsprintf((LPWSTR)m_pArray, _T("級 別 : %d"), Setting::GetTankLevel());
- drawtext((LPWSTR)m_pArray, &r, DT_VCENTER | DT_SINGLELINE);
- r.top += 50;
- r.bottom += 50;
- wsprintf((LPWSTR)m_pArray, _T("生 命 : %d"), Setting::GetLife());
- drawtext((LPWSTR)m_pArray, &r, DT_VCENTER | DT_SINGLELINE);
- r.top += 50;
- r.bottom += 50;
- wsprintf((LPWSTR)m_pArray, _T("敵人數(shù) : %d"), Setting::GetTankNum());
- drawtext((LPWSTR)m_pArray, &r, DT_VCENTER | DT_SINGLELINE);
- r.top += 50;
- r.bottom += 50;
- line(SCORE_LEFT, r.bottom, SCREEN_WIDTH - 5, r.bottom);
- r.top += 50;
- r.bottom += 50;
- wsprintf((LPWSTR)m_pArray, _T("共擊毀敵人數(shù) : %d"), Setting::GetTankSum());
- drawtext((LPWSTR)m_pArray, &r, DT_VCENTER | DT_SINGLELINE);
- setcolor(color_save);
- setfillcolor(fill_color_save);
- }
- void Graphic::ShowGameLevel(int nLevel)
- {
- COLORREF fill_color_save = getfillcolor();
- COLORREF color_save = getcolor();
- rectangle(BATTLE_GROUND_X1 + 100, BATTLE_GROUND_Y1 + 200, BATTLE_GROUND_X1 + 700, BATTLE_GROUND_Y1 + 380);
- LOGFONT fontBak;
- gettextstyle(&fontBak); // 獲取當前字體設(shè)置
- LOGFONT f = fontBak;
- f.lfHeight = 48; // 設(shè)置字體高度為 48
- _tcscpy_s(f.lfFaceName, _T("黑體")); // 設(shè)置字體為“黑體”
- f.lfQuality = ANTIALIASED_QUALITY; // 設(shè)置輸出效果為抗鋸齒
- settextstyle(&f); // 設(shè)置字體樣式
- wsprintf((LPWSTR)m_pArray, _T("第 %d 關(guān)"), nLevel);
- outtextxy(BATTLE_GROUND_X1 + 300, BATTLE_GROUND_Y1 + 250, (LPWSTR)m_pArray);
- f.lfHeight = 18;
- settextstyle(&f);
- wsprintf((LPWSTR)m_pArray, _T("按 Enter 鍵開始"), nLevel);
- outtextxy(BATTLE_GROUND_X1 + 550, BATTLE_GROUND_Y1 + 350, (LPWSTR)m_pArray);
- settextstyle(&fontBak);
- setcolor(color_save);
- setfillcolor(fill_color_save);
- }
- void Graphic::ShowGameOver()
- {
- COLORREF fill_color_save = getfillcolor();
- COLORREF color_save = getcolor();
- rectangle(BATTLE_GROUND_X1 + 100, BATTLE_GROUND_Y1 + 200, BATTLE_GROUND_X1 + 700, BATTLE_GROUND_Y1 + 380);
- LOGFONT fontBak;
- gettextstyle(&fontBak); // 獲取當前字體設(shè)置
- LOGFONT f = fontBak;
- f.lfHeight = 48; // 設(shè)置字體高度為 48
- _tcscpy_s(f.lfFaceName, _T("黑體")); // 設(shè)置字體為“黑體”
- f.lfQuality = ANTIALIASED_QUALITY; // 設(shè)置輸出效果為抗鋸齒
- settextstyle(&f); // 設(shè)置字體樣式
- wsprintf((LPWSTR)m_pArray, _T("GAME OVER"));
- outtextxy(BATTLE_GROUND_X1 + 300, BATTLE_GROUND_Y1 + 250, (LPWSTR)m_pArray);
- f.lfHeight = 18;
- settextstyle(&f);
- wsprintf((LPWSTR)m_pArray, _T("按 Enter 鍵退出"));
- outtextxy(BATTLE_GROUND_X1 + 550, BATTLE_GROUND_Y1 + 350, (LPWSTR)m_pArray);
- settextstyle(&fontBak);
- setcolor(color_save);
- setfillcolor(fill_color_save);
- }
- Main.cpp
- #pragma warning(disable:4996)
- #include <iostream>
- #include <conio.h>
- #include <time.h>
- #include <list>
- #include "Graphic.h"
- #include "MainTank.h"
- #include "EnemyTank.h"
- #include "Utils/Shape.h"
- #include "Setting.h"
- using namespace std;
- MainTank mainTank;
- // Bullet list
- list<Object*> lstMainTankBullets;
- list<Object*> lstBullets;
- // Bomb List
- list<Object*> lstBombs;
- // Tank list
- list<Tank*> lstTanks;
- void CheckCrash()
- {
- // Check enermy tank damage
- for (list<Object*>::iterator it = lstMainTankBullets.begin(); it != lstMainTankBullets.end(); it++)
- {
- for (list<Tank*>::iterator itt = lstTanks.begin(); itt != lstTanks.end(); itt++)
- {
- if (Shape::CheckIntersect((*it)->GetSphere(), (*itt)->GetSphere()))
- {
- (*itt)->SetDisappear();
- (*it)->SetDisappear();
- }
- }
- }
- // Check main tank damage
- for (list<Object*>::iterator it = lstBullets.begin(); it != lstBullets.end(); it++)
- {
- if (Shape::CheckIntersect((*it)->GetSphere(), mainTank.GetSphere()))
- {
- Setting::Die();
- if (Setting::GetLife() > 0)
- {
- (*it)->SetDisappear();
- }
- else
- {
- mainTank.SetDisappear();
- }
- }
- }
- }
- void Init()
- {
- srand((unsigned)time(NULL));
- Graphic::Create();
-
- lstMainTankBullets.clear();
- lstBullets.clear();
- lstBombs.clear();
- lstTanks.clear();
- }
- void Dispose()
- {
- for (list<Tank*>::iterator it = lstTanks.begin(); it != lstTanks.end(); it++)
- {
- delete *it;
- }
- lstTanks.clear();
- for (list<Object*>::iterator it = lstMainTankBullets.begin(); it != lstMainTankBullets.end(); it++)
- {
- delete *it;
- }
- for (list<Object*>::iterator it = lstBullets.begin(); it != lstBullets.end(); it++)
- {
- delete *it;
- }
- lstBullets.clear();
- for (list<Object*>::iterator it = lstBombs.begin(); it != lstBombs.end(); it++)
- {
- delete *it;
- }
- lstBombs.clear();
- Graphic::Destroy();
- }
- void main()
- {
- Init();
- bool loop = true;
- bool skip = false;
- bool bGameOver = false;
- while (loop)
- {
- if (kbhit())
- {
- int key = getch();
- if (skip && key != 13)
- {
- continue;
- }
- Dir dirBak;
- switch (key)
- {
- // Up
- case 72:
- mainTank.SetDir(Dir::UP);
- break;
- // Down
- case 80:
- mainTank.SetDir(Dir::DOWN);
- break;
- // Left
- case 75:
- mainTank.SetDir(Dir::LEFT);
- break;
- // Right
- case 77:
- mainTank.SetDir(Dir::RIGHT);
- break;
- case 224: // 方向鍵高8位
- break;
- // Esc
- case 27:
- loop = false;
- break;
- // Space
- case 32:
- mainTank.Shoot(lstMainTankBullets);
- break;
- // Q
- case 113:
- dirBak = mainTank.GetDir();
- mainTank.SetDir(Dir::UP);
- mainTank.Shoot(lstMainTankBullets);
- mainTank.SetDir(Dir::DOWN);
- mainTank.Shoot(lstMainTankBullets);
- mainTank.SetDir(Dir::LEFT);
- mainTank.Shoot(lstMainTankBullets);
- mainTank.SetDir(Dir::RIGHT);
- mainTank.Shoot(lstMainTankBullets);
- mainTank.SetDir(dirBak);
- break;
- // Enter
- case 13:
- if (skip)
- skip = false;
- else
- skip = true;
- break;
- default:
- break;
- }
- }
-
- if (!skip)
- {
- if (bGameOver)
- {
- break;
- }
- // Draw Background
- cleardevice();
- Graphic::DrawBattleGround();
-
- CheckCrash();
- Graphic::ShowScore();
- // New Game Level
- if (Setting::m_bNewLevel)
- {
- Setting::m_bNewLevel = false;
- Setting::NewGameLevel();
- Graphic::ShowGameLevel(Setting::GetGameLevel());
- for (int i = 0; i < Setting::GetTankNum(); i++)
- {
- EnemyTank* p = new EnemyTank();
- lstTanks.push_back(p);
- }
- // 設(shè)置暫停,按Enter開始
- skip = true;
- continue;
- }
-
- if (mainTank.IsDisappear())
- {
- skip = true;
- bGameOver = true;
- Graphic::ShowGameOver();
- continue;
- }
- mainTank.Move();
- mainTank.Display();
- /* Draw Tanks */
- for (list<Tank*>::iterator it = lstTanks.begin(); it != lstTanks.end();)
- {
- (*it)->Move();
- if ((*it)->IsDisappear())
- {
- Setting::TankDamaged();
- // Add a bomb
- (*it)->Boom(lstBombs);
- // Delete the tank
- delete *it;
- it = lstTanks.erase(it);
- continue;
- }
- (*it)->Display();
- if ((*it)->NeedShoot())
- {
- EnemyTank* p = (EnemyTank*)*it;
- p->Shoot(lstBullets);
- }
- it++;
- }
- /* Draw Bullets */
- for (list<Object*>::iterator it = lstMainTankBullets.begin(); it != lstMainTankBullets.end();)
- {
- (*it)->Move();
- if ((*it)->IsDisappear())
- {
- // Add a bomb
- (*it)->Boom(lstBombs);
- // Delete the bullet
- delete *it;
- it = lstMainTankBullets.erase(it);
- continue;
- }
- (*it)->Display();
- it++;
- }
- for (list<Object*>::iterator it = lstBullets.begin(); it != lstBullets.end();)
- {
- (*it)->Move();
-
- if ((*it)->IsDisappear())
- {
- // Add a bomb
- (*it)->Boom(lstBombs);
- // Delete the bullet
- delete *it;
- it = lstBullets.erase(it);
- continue;
- }
- (*it)->Display();
- it++;
- }
- /* Draw Bombs */
- for (list<Object*>::iterator it = lstBombs.begin(); it != lstBombs.end();)
- {
- (*it)->Move();
- if ((*it)->IsDisappear())
- {
- delete *it;
- it = lstBombs.erase(it);
- continue;
- }
-
- (*it)->Display();
- it++;
- }
- }
- Sleep(100);
- }
-
- // Destroy
- Dispose();
- }
- MainTank.h
- #ifndef __MAIN_TANK__
- #define __MAIN_TANK__
- #include "Tank.h"
- class MainTank : public Tank
- {
- public:
- MainTank() : Tank()
- {
- m_pos.Set(300, 300);
- this->CalculateSphere();
- m_color = YELLOW;
- m_dir = Dir::UP;
- m_step = 4;
- }
- ~MainTank(){}
- void SetDir(Dir dir);
- Dir GetDir()
- {
- return m_dir;
- }
- void Display();
- void Move();
- void Shoot(list<Object*>& lstBullets);
- void Boom(list<Object*>& lstBombs);
- protected:
- void CalculateSphere();
- // 繪制坦克主體
- void DrawTankBody();
- };
- #endif
- MainTank.cpp
- #include "MainTank.h"
- #include "Bullet.h"
- void MainTank::SetDir(Dir dir)
- {
- m_dir = dir;
- }
- void MainTank::DrawTankBody()
- {
- fillrectangle(m_pos.GetX() - 6, m_pos.GetY() - 6, m_pos.GetX() + 6, m_pos.GetY() + 6);
- switch (m_dir)
- {
- case UP:
- case DOWN:
- fillrectangle(m_rectSphere.GetStartPoint().GetX(), m_rectSphere.GetStartPoint().GetY(),
- m_rectSphere.GetStartPoint().GetX() + 4, m_rectSphere.GetEndPoint().GetY());
- fillrectangle(m_rectSphere.GetEndPoint().GetX() - 4, m_rectSphere.GetStartPoint().GetY(),
- m_rectSphere.GetEndPoint().GetX(), m_rectSphere.GetEndPoint().GetY());
- break;
- case LEFT:
- case RIGHT:
- fillrectangle(m_rectSphere.GetStartPoint().GetX(), m_rectSphere.GetStartPoint().GetY(),
- m_rectSphere.GetEndPoint().GetX(), m_rectSphere.GetStartPoint().GetY() + 4);
- fillrectangle(m_rectSphere.GetStartPoint().GetX(), m_rectSphere.GetEndPoint().GetY() - 4,
- m_rectSphere.GetEndPoint().GetX(), m_rectSphere.GetEndPoint().GetY());
- break;
- default:
- break;
- }
- }
- void MainTank::Display()
- {
- COLORREF fill_color_save = getfillcolor();
- COLORREF color_save = getcolor();
- setfillcolor(m_color);
- setcolor(m_color);
- DrawTankBody();
- switch (m_dir)
- {
- case UP:
- line(m_pos.GetX(), m_pos.GetY(), m_pos.GetX(), m_pos.GetY() - 15);
- break;
- case DOWN:
- line(m_pos.GetX(), m_pos.GetY(), m_pos.GetX(), m_pos.GetY() + 15);
- break;
- case LEFT:
- line(m_pos.GetX(), m_pos.GetY(), m_pos.GetX() - 15, m_pos.GetY());
- break;
- case RIGHT:
- line(m_pos.GetX(), m_pos.GetY(), m_pos.GetX() + 15, m_pos.GetY());
- break;
- default:
- break;
- }
- setcolor(color_save);
- setfillcolor(fill_color_save);
- }
- void MainTank::Move()
- {
- switch (m_dir)
- {
- case UP:
- m_pos.SetY(m_pos.GetY() - m_step);
- if (m_rectSphere.GetStartPoint().GetY() < Graphic::GetBattleGround().GetStartPoint().GetY())
- m_pos.SetY(m_pos.GetY() + m_step);
- break;
- case DOWN:
- m_pos.SetY(m_pos.GetY() + m_step);
- if (m_rectSphere.GetEndPoint().GetY() > Graphic::GetBattleGround().GetEndPoint().GetY())
- m_pos.SetY(m_pos.GetY() - m_step);
- break;
- case LEFT:
- m_pos.SetX(m_pos.GetX() - m_step);
- if (m_rectSphere.GetStartPoint().GetX() < Graphic::GetBattleGround().GetStartPoint().GetX())
- m_pos.SetX(m_pos.GetX() + m_step);
- break;
- case RIGHT:
- m_pos.SetX(m_pos.GetX() + m_step);
- if (m_rectSphere.GetEndPoint().GetX() > Graphic::GetBattleGround().GetEndPoint().GetX())
- m_pos.SetX(m_pos.GetX() - m_step);
- break;
- default:
- break;
- }
- CalculateSphere();
- }
- void MainTank::CalculateSphere()
- {
- switch (m_dir)
- {
- case UP:
- case DOWN:
- m_rectSphere.Set(m_pos.GetX() - 13, m_pos.GetY() - 10, m_pos.GetX() + 13, m_pos.GetY() + 10);
- break;
- case LEFT:
- case RIGHT:
- m_rectSphere.Set(m_pos.GetX() - 10, m_pos.GetY() - 13, m_pos.GetX() + 10, m_pos.GetY() + 13);
- break;
- default:
- break;
- }
- }
- void MainTank::Shoot(list<Object*>& lstBullets)
- {
- Bullet* pBullet = new Bullet(m_pos, m_dir, m_color);
- pBullet->SetStep(20);
- lstBullets.push_back(pBullet);
- }
- void MainTank::Boom(list<Object*>& lstBombs)
- {
- }
- Object.h
- #ifndef __OBJECT_H__
- #define __OBJECT_H__
- #include <list>
- #include "Graphic.h"
- using namespace std;
- enum Dir { UP, DOWN, LEFT, RIGHT };
- class Object
- {
- public:
- // 繪圖
- virtual void Display() = 0;
- // 移動
- virtual void Move() = 0;
- // 爆炸
- virtual void Boom(list<Object*>& lstBombs) = 0;
- // 設(shè)置消失
- virtual void SetDisappear() = 0;
- // 判斷是否消失
- virtual bool IsDisappear() = 0;
- virtual Rect GetSphere() = 0;
- protected:
- // 計算勢力范圍
- virtual void CalculateSphere() = 0;
- // 位置
- Point m_pos;
- // 勢力范圍
- Rect m_rectSphere;
- // 顏色
- COLORREF m_color;
- // 方向
- Dir m_dir;
- // 存在狀態(tài)
- bool m_bDisappear;
- // 單次前進步長
- int m_step;
- };
- #endif
- Point.cpp
- #include "Point.h"
- void Point::Set(int x, int y)
- {
- m_x = x;
- m_y = y;
- }
- void Point::SetX(int x)
- {
- m_x = x;
- }
- void Point::SetY(int y)
- {
- m_y = y;
- }
- int Point::GetX() const
- {
- return m_x;
- }
- int Point::GetY() const
- {
- return m_y;
- }
- Point.h
- #ifndef __POINT_H__
- #define __POINT_H__
- class Point
- {
- public:
- Point(int x = 0, int y = 0) : m_x(x), m_y(y){};
- ~Point(){};
- Point& operator=(const Point &p)
- {
- m_x = p.m_x;
- m_y = p.m_y;
- return *this;
- }
- void Set(int x, int y);
- void SetX(int x);
- void SetY(int y);
- int GetX() const;
- int GetY() const;
- private:
- int m_x;
- int m_y;
- };
- #endif
- Rect.h
- #ifndef __RECTANGLE_H__
- #define __RECTANGLE_H__
- #include "Point.h"
- class Rect
- {
- public:
- Rect(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0) : m_startPoint(x1, y1), m_endPoint(x2, y2){}
- Rect(const Point p1, const Point p2) : m_startPoint(p1), m_endPoint(p2){}
- Rect(const Rect& r1) : m_startPoint(r1.GetStartPoint()), m_endPoint(r1.GetEndPoint()){}
- ~Rect(){};
- Rect& operator=(const Rect &rect)
- {
- m_startPoint = rect.GetStartPoint();
- m_endPoint = rect.GetEndPoint();
- return *this;
- }
- void Set(const Point pStart, const Point pEnd);
- void Set(int x1, int y1, int x2, int y2);
- void SetStartPoint(const Point p);
- void SetEndPoint(const Point p);
- Point GetStartPoint() const;
- Point GetEndPoint() const;
- Point GetTRPoint() const; // Get Top Right Point
- Point GetBLPoint() const; // Get Bottom Left Point
- int GetWidth();
- int GetHeight();
- private:
- void Check();
- Point m_startPoint;
- Point m_endPoint;
- };
- #endif
- Setting.cpp
- #include "Setting.h"
- bool Setting::m_bNewLevel = true;
- int Setting::m_nLife = 3;
- int Setting::m_nGameLevel = 0;
- int Setting::m_nTankLevel = 1;
- int Setting::m_nTankNum = 5;
- int Setting::m_nSumScore = 0;
- int Setting::m_nTankScore = 5;
- int Setting::m_nTankSum = 0;
- void Setting::NewGameLevel()
- {
- m_nGameLevel++;
- m_nTankNum = 10 + 5 * (m_nGameLevel - 1);
- m_nTankScore += 5;
- }
- void Setting::TankDamaged()
- {
- m_nTankNum--;
- m_nSumScore += m_nTankScore;
- m_nTankLevel = m_nSumScore / 150 + 1;
- if (m_nSumScore % 500 < m_nTankScore)
- {
- m_nLife++;
- }
- m_nTankSum++;
- if (m_nTankNum == 0)
- {
- m_bNewLevel = true;
- }
- }
- Setting.h
- #ifndef __SETTING_H__
- #define __SETTING_H__
- #include <list>
- using namespace std;
- class Setting
- {
- public:
- static void NewGameLevel();
- static void TankDamaged();
- static int GetLife()
- {
- return m_nLife;
- }
- static void Die()
- {
- m_nLife -= 1;
- }
- static int GetGameLevel()
- {
- return m_nGameLevel;
- }
- static int GetTankLevel()
- {
- return m_nTankLevel;
- }
- static int GetTankNum()
- {
- return m_nTankNum;
- }
- static int GetSumScore()
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
坦克大戰(zhàn).zip
(5.63 KB, 下載次數(shù): 77)
2018-7-8 22:40 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|