📄 btplane.cpp
字号:
#include "pge/inc/pgeutil.h"
USE_SOUND
// 定义一个方便计算的点结构
struct sPoint
{
sPoint():x(0),y(0)
{
}
sPoint(short sx, short sy):x(sx),y(sy)
{
}
inline bool operator !=(sPoint v)
{
return v.x!=x || v.y!=y;
}
sPoint operator -(sPoint v)
{
return sPoint(x-v.x, y-v.y);
}
short x,y;
};
// 子弹的结构
struct Bullet
{
enum estat
{
idle = 0,
inst,
show,
};
Bullet()
{
stat = idle;
}
sPoint pos;
sPoint des;
char stat;
int speed;
};
// 游戏实例,从CPGEApplication(pge简单游戏框架)继承
class CBSFYApp : public CPGEApplication
{
public:
CBSFYApp();
~CBSFYApp();
virtual PGE_RESULT OnInitDeviceObjects(); // 初始化
virtual PGE_RESULT OnDeleteDeviceObjects(); // 结束时清理
virtual PGE_RESULT OnFinalCleanup(); // 退出时清理,晚于OnDeleteDeviceObjects,早于~CBSFYApp()
virtual void OnFrameMove(); // 开始一帧,在OnDraw之前
virtual void OnDraw(); // 画一帧
virtual PGE_RESULT OnWindowProc(HWND hWnd, UINT message
, WPARAM wParam, LPARAM lParam); // 窗口消息处理函数
sPoint GetLineFirstPoint(sPoint p1, sPoint p2, int speed); // 取直线上第一点
void DrawBack(RECT& r, char* c, int cx, int cy, int nc); // 菜单
CPGESurface* m_pMainSuf; // 主表面
CPGESurface* m_pPic;
RECT m_rPlanePos;
RECT m_rPlanePic;
int m_iStat;
enum planestat
{
normal = 0,
over,
winer,
menu
};
Bullet m_Bullet[128];
CPGESurface* m_psufStarB;
CPGESurface* m_psufStarF;
void CreateStar(WORD* b);
int m_iMaxBullet;
DWORD m_dwLiveTimer;
DWORD m_dwActiveTimer;
int m_iNiceTimes;
DWORD bulletTimer;
int m_iDummyTimer;
RECT m_rDummyPos;
bool bDrawStar;
int m_iSpeed;
int m_iMenu;
char m_ExpSnd[256];
char m_NiceSnd[256];
char m_AmmSnd[256];
char m_BkSnd[256];
bool m_bSound;
int m_isroKeybord;
int m_isroMouse;
char m_caTitle[256];
int m_iMsgTimer;
bool m_bPause;
};
RECT rPicPlaneF = RECT(0,0,16,16);
RECT rPicPlaneL = RECT(16,0,32,16);
RECT rPicPlaneR = RECT(32,0,48,16);
RECT rPicBullet = RECT(48,0,52,4);
WORD StarColor[] = {
PGE_BLACK,
PGE_WHITE,
PGE_RED ,
PGE_GREEN,
PGE_BLUE,
PGE_PURPLE,
PGE_YELLOW,
PGE_LIGHTBLUE,
PGE_WHITE/2,
PGE_RED/2,
PGE_GREEN/2,
PGE_BLUE/2,
PGE_PURPLE/2,
PGE_YELLOW/2,
};
CBSFYApp g_BsfyApp; // 主应用程序对象
CBSFYApp::CBSFYApp()
{
m_pMainSuf = NULL;
}
CBSFYApp::~CBSFYApp()
{
}
void CBSFYApp::CreateStar(WORD* b)
{
int n = 0;
for (int i=0; i<320; i++)
{
n = random(1000);
b[i] = (n==10 ? StarColor[random(1, 14)] : 0);
}
}
PGE_RESULT CBSFYApp::OnInitDeviceObjects()
{
SRESULT;
#ifndef _WIN32_WCE
timer.SetFrmLim(25);
#endif
srand(GetTickCount());
m_pMainSuf = GetDevice()->GetMainSurface(); // 取得主表面
m_pMainSuf->EnableAlphaChanle();
m_pMainSuf->CreateFont(GetResFile("font\\Pgesong.font"), PGE_FONT_USEFILE);
CreateSurfaceFromFile(&m_pPic, GetResFile("pic\\pic.bmp"), GetDevice(), PGE_BLACK);
m_rPlanePos = RECT(155,175,155+16,175+16);
m_psufStarB = new CPGESurface(NULL);
m_psufStarF = new CPGESurface(NULL);
m_psufStarB->CreateSurface(240,320);
m_psufStarF->CreateSurface(240,320, PGE_NOMARL_SURFACE, PGE_BLACK);
WORD *starf, *starb;
m_psufStarF->Lock((void**)&starf);
m_psufStarB->Lock((void**)&starb);
for (int i=0; i<240; i++)
{
CreateStar(starf+i*320);
CreateStar(starb+i*320);
}
m_psufStarB->unLock();
m_psufStarF->unLock();
m_iMaxBullet = 40;
m_iStat = menu;
m_dwLiveTimer = 0;
m_dwActiveTimer = 0;
m_iNiceTimes = 0;
bulletTimer = 5000;
m_iDummyTimer = 0;
bDrawStar = false;
m_iSpeed = 2;
m_iMenu = 0;
m_bSound = true;
m_isroKeybord = 0;
m_isroMouse = 0;
m_iMsgTimer = 0;
m_bPause = false;
strcpy(m_ExpSnd, GetResFile("sound\\exploed.wav"));
strcpy(m_NiceSnd, GetResFile("sound\\ok.wav"));
strcpy(m_AmmSnd, GetResFile("sound\\magic.wav"));
strcpy(m_BkSnd, GetResFile("sound\\bk.wav"));
m_caTitle[0] = 0;
SoundPlayer.play(m_BkSnd);
OKRESULT;
RET;
}
PGE_RESULT CBSFYApp::OnDeleteDeviceObjects()
{
SRESULT;
OKRESULT;
RET;
}
PGE_RESULT CBSFYApp::OnFinalCleanup()
{
SRESULT;
OKRESULT;
RET;
}
sPoint CBSFYApp::GetLineFirstPoint(sPoint p1, sPoint p2, int speed)
{
int x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y;
int distance, t;
int x=0,y=0,delta_x,delta_y,incx,incy;
delta_x = x2-x1;
delta_y = y2-y1;
if(delta_x>0)
{
incx=1;
}
else if (delta_x==0)
{
incx=0;
}
else
{
delta_x=-delta_x;
incx=-1;
}
if (delta_y>0)
{
incy=1;
}
else if (delta_y==0)
{
incy=0;
}
else
{
delta_y=-delta_y;
incy=-1;
}
if(delta_x>delta_y)
{
distance=delta_x;
}
else
{
distance=delta_y;
}
sPoint ret;
int ispeed = 0;
for (t=0; t<distance+2; t++)
{
ret.x = x1;
ret.y = y1;
if (ret != p1)
{
ispeed++;
if (ispeed == speed) return ret;
}
x+=delta_x;
y+=delta_y;
if(x>distance)
{
x-=distance;
x1+=incx;
}
if(y>distance)
{
y-=distance;
y1+=incy;
}
}
return ret;
}
void CBSFYApp::OnFrameMove()
{
if (m_bPause) return;
if (!m_iStat)
{
m_dwLiveTimer += (timer.GetDalyTimes());
m_rPlanePic = rPicPlaneF;
bool bMove = false;
if (GetAsyncKeyState(sKeys->vkUp))
{
m_rPlanePos.top-=(m_iSpeed+1);
bMove = true;
++m_isroKeybord;
}
if (GetAsyncKeyState(sKeys->vkDown))
{
m_rPlanePos.top+=(m_iSpeed+1);
bMove = true;
++m_isroKeybord;
}
if (GetAsyncKeyState(sKeys->vkLeft))
{
m_rPlanePos.left-=(m_iSpeed+1);
m_rPlanePic = rPicPlaneL;
bMove = true;
++m_isroKeybord;
}
if (GetAsyncKeyState(sKeys->vkRight))
{
m_rPlanePos.left+=(m_iSpeed+1);
m_rPlanePic = rPicPlaneR;
bMove = true;
++m_isroKeybord;
}
if (m_MouseData.lBtn)
{
m_rPlanePos = RECT(m_MouseData.ix-7, m_MouseData.iy-7, 0, 0);
bMove = true;
++m_isroMouse;
}
if (bMove) m_dwActiveTimer += timer.GetDalyTimes();
if (m_rPlanePos.left < 0) m_rPlanePos.left = 0;
if (m_rPlanePos.left+16 > 320) m_rPlanePos.left = 320-16;
if (m_rPlanePos.top < 0) m_rPlanePos.top = 0;
if (m_rPlanePos.top+16 > 240) m_rPlanePos.top = 240-16;
m_rPlanePos.right = m_rPlanePos.left+16;
m_rPlanePos.bottom = m_rPlanePos.top+16;
int iNice = 0;
for (int i=0; i<m_iMaxBullet; i++)
{
if (PointInRect(m_Bullet[i].pos.x, m_Bullet[i].pos.y,
RECT(m_rPlanePos.left-10, m_rPlanePos.top-10,
m_rPlanePos.right+10, m_rPlanePos.bottom+10))) iNice++;
int nhit= m_rPlanePos.hit(RECT(m_Bullet[i].pos.x, m_Bullet[i].pos.y,
m_Bullet[i].pos.x+4, m_Bullet[i].pos.y+4));
if (nhit==1)
{
if (m_bSound) SoundPlayer.play(m_ExpSnd);
m_iStat = 1;
iNice = 0;
}
if (m_Bullet[i].stat == Bullet::inst)
{
m_Bullet[i].pos = GetLineFirstPoint(m_Bullet[i].pos, m_Bullet[i].des, m_Bullet[i].speed);
if (m_Bullet[i].pos.x > 0 && m_Bullet[i].pos.x < 320
&& m_Bullet[i].pos.y > 0 && m_Bullet[i].pos.y < 240)
m_Bullet[i].stat = Bullet::show;
}
else if (m_Bullet[i].stat == Bullet::show)
{
m_Bullet[i].pos = GetLineFirstPoint(m_Bullet[i].pos, m_Bullet[i].des, m_Bullet[i].speed);
if (m_Bullet[i].pos.x > 320 || m_Bullet[i].pos.y > 240
|| m_Bullet[i].pos.x < 0 || m_Bullet[i].pos.y < 0)
m_Bullet[i].stat = Bullet::idle;
}
}
if(iNice>3)
{
++m_iNiceTimes;
if (m_bSound) SoundPlayer.play(m_NiceSnd);
}
bulletTimer += timer.GetDalyTimes();
if (bulletTimer>1000)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -