飞机大战小游戏来源:[https://github.com/WindrunnerMax/AirplaneWar]
运行环境:visual studio 2019
运行截图如下:
对源文件的各项代码,我进行了一些修改和创新,Bomb.cpp文件中,我发现以下问题:
魔法数值硬编码:在Draw函数中出现了数字12和10,应该将这些魔法数值定义成常量或者宏以提高代码的可读性和可维护性。
潜在的bug:在Draw函数中,m_ptPos.x的更新逻辑可能会导致炸弹在横向上移动,但这个移动逻辑并没有在构造函数中初始化。应该确保在构造函数中对xx进行初始化。
源代码如下:
点击查看代码
#include "StdAfx.h"
#include "Bomb.h"
#include "resource.h"
CImageList CBomb::m_Images;
CBomb::CBomb(int x, int y) :CGameObject(x, y) {xx = 0;}
CBomb::CBomb(int x, int y,int z) : CGameObject(x, y),xx(z)
{
}
CBomb::~CBomb(void)
{
}
BOOL CBomb::LoadImage()
{
return CGameObject::LoadImage(m_Images,IDB_BOMB,RGB(0,0,0),10,20,1);
}
BOOL CBomb::Draw(CDC* pDC,BOOL bPause)
{
if(!bPause)
{
m_ptPos.y = m_ptPos.y - 12;
m_ptPos.x = m_ptPos.x - xx;
}
if(m_ptPos.y < -BOMB_HEIGHT)
return FALSE;
m_Images.Draw(pDC,0,m_ptPos,ILD_TRANSPARENT);
return TRUE;
}
点击查看代码
#include "StdAfx.h"
#include "Bomb.h"
#include "resource.h"
CImageList CBomb::m_Images;
CBomb::CBomb(int x, int y, int z) : CGameObject(x, y), xx(z)
{
}
CBomb::~CBomb(void)
{
}
BOOL CBomb::LoadImage()
{
return CGameObject::LoadImage(m_Images, IDB_BOMB, RGB(0, 0, 0), 10, 20, 1);
}
BOOL CBomb::Draw(CDC* pDC, BOOL bPause)
{
const int BOMB_MOVE_SPEED = 12;
if (!bPause)
{
m_ptPos.y -= BOMB_MOVE_SPEED;
m_ptPos.x -= xx;
}
if (m_ptPos.y < -BOMB_HEIGHT)
{
return FALSE;
}
m_Images.Draw(pDC, 0, m_ptPos, ILD_TRANSPARENT);
return TRUE;
}
点击查看代码
#include "StdAfx.h"
#include "Enemy.h"
#include "resource.h"
CImageList CEnemy::m_Images;
CEnemy::CEnemy(void)
{
//随机确定X位置
m_ptPos.x = rand()%(GAME_WIDTH-ENEMY_HEIGHT)+1;
m_ptPos.y=-ENEMY_HEIGHT;
m_nMotion = 1;
//随机确定速度
m_V = rand()%6+1;
enemyHP = 30;
m_nWait=0;
}
CEnemy::~CEnemy(void)
{
}
BOOL CEnemy::LoadImage()
{
return CGameObject::LoadImage(m_Images,IDB_ENEMY,RGB(0,0,0),35,35,2);
}
BOOL CEnemy::Draw(CDC* pDC,BOOL bPause)
{
m_nWait++;
if(m_nWait>10)
m_nWait=0;
if(!bPause)
{
m_ptPos.y = m_ptPos.y + m_nMotion* m_V;
}
if(m_ptPos.y > GAME_HEIGHT+ENEMY_HEIGHT )
return FALSE;
if(m_ptPos.y < -ENEMY_HEIGHT)
return FALSE;
m_Images.Draw(pDC,0,m_ptPos,ILD_TRANSPARENT);
pDC->FillSolidRect(m_ptPos.x + 2, m_ptPos.y - 6, 30, 2, RGB(255, 0, 0));
pDC->FillSolidRect(m_ptPos.x + 2, m_ptPos.y - 6,enemyHP, 2, RGB(0, 255, 0));
return TRUE;
}
BOOL CEnemy::Fired()
{
if(m_nWait==0)
return TRUE;
else
return FALSE;
}
点击查看代码
#include "StdAfx.h"
#include "Enemy.h"
#include "resource.h"
CImageList CEnemy::m_Images;
CEnemy::CEnemy()
{
m_ptPos.x = rand() % (GAME_WIDTH - ENEMY_HEIGHT) + 1;
m_ptPos.y = -ENEMY_HEIGHT;
m_nMotion = 1;
m_V= rand() % 6 + 1; // 随机速度范围为1到6
enemyHP = 30;
m_nWait = 0;
}
CEnemy::~CEnemy()
{
}
BOOL CEnemy::LoadImage()
{
return CGameObject::LoadImage(m_Images, IDB_ENEMY, RGB(0, 0, 0), 35, 35, 2);
}
BOOL CEnemy::Draw(CDC* pDC, BOOL bPause)
{
m_nWait = (m_nWait + 1) % 10;
if (!bPause)
{
m_ptPos.y = m_ptPos.y + m_nMotion * m_V;
}
if (m_ptPos.y > GAME_HEIGHT + ENEMY_HEIGHT || m_ptPos.y < -ENEMY_HEIGHT)
{
return FALSE;
}
m_Images.Draw(pDC, 0, m_ptPos, ILD_TRANSPARENT);
pDC->FillSolidRect(m_ptPos.x + 2, m_ptPos.y - 6, 30, 2, RGB(255, 0, 0));
pDC->FillSolidRect(m_ptPos.x + 2, m_ptPos.y - 6, enemyHP, 2, RGB(0, 255, 0));
return TRUE;
}
BOOL CEnemy::Fired()
{
return m_nWait == 0;
}
GameObject.cpp文件中缺少对资源的释放:在LoadImage函数中加载了位图资源,但没有对资源进行释放,可能导致内存泄漏问题。
错误处理不完善,在LoadImage函数中,没有对位图加载失败的情况进行处理,添加相应的错误处理逻辑。
参数传递不够灵活,LoadImage函数的参数列表较长,可以考虑将一些参数封装成结构体或者类,以提高代码的可读性和可维护性。
源代码如下:
点击查看代码
// GameObject.cpp : 实现文件
//
#include "stdafx.h"
#include "GameObject.h"
// CGameObject
CGameObject::CGameObject(int x,int y):m_ptPos(x,y)
{
}
CGameObject::~CGameObject()
{
}
BOOL CGameObject::LoadImage(CImageList& imgList,UINT bmpID,COLORREF crMask,int cx,int cy,int nInitial)
{
CBitmap bmp;
if(!bmp.LoadBitmap(bmpID))
return FALSE;
if(!imgList.Create(cx, cy, ILC_COLOR24|ILC_MASK, nInitial, 0))
return FALSE;
imgList.Add(&bmp, crMask);
return TRUE;
}
点击查看代码
#include "stdafx.h"
#include "GameObject.h"
CGameObject::CGameObject(int x, int y) : m_ptPos(x, y)
{
}
CGameObject::~CGameObject()
{
}
BOOL CGameObject::LoadImage(CImageList& imgList, UINT bmpID, COLORREF crMask, int cx, int cy, int nInitial)
{
CBitmap bmp;
if (!bmp.LoadBitmap(bmpID))
{
return FALSE;
}
if (!imgList.Create(cx, cy, ILC_COLOR24 | ILC_MASK, nInitial, 0))
{
return FALSE;
}
if (imgList.Add(&bmp, crMask) == -1)
{
return FALSE;
}
bmp.DeleteObject(); // 释放位图资源
return TRUE;
}