- 易迪拓培训,专注于微波、射频、天线设计工程师的培养
LabWindows/CVI虚拟仪器设计技术场景与游戏设计之: 打蜜蜂
(2)程序源代码
//头文件声明
#include "windows.h"
#include "asynctmr.h"
#include "toolbox.h"
#include "mmsystem.h"
#include "bee.h"
static int panelHandle;
//声明异步定时器句柄
static int timerkey;
//声明游戏结束面板句柄
static int pophandle;
//定义发射子弹可见为1,不可见为0
static int shootflag = 0;
//定义蜜蜂炸弹可见为1,不可见为0
static int boomflag = 0;
//定义蜜蜂炸弹发射间隔
static int boomtime = 0;
//定义蜜蜂水平移动单位
static int hh = 3;
//定义蜜蜂垂直移动单位
static int vv = 2;
//定义枪(飞机)、子弹、蜜蜂、炸弹等位置信息
Rect gun;
Rect bullet;
Rect bee;
Rect boom;
Rect shootbee;
Rect panelpp;
//自定义函数
void porperty (void);
void position (void);
void popmessage (void);
int CVICALLBACK keystroke(int reserved, int timerId, int event, void *callbackData, int eventData1, int
eventData2);
//主函数
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return –1;
if ((panelHandle = LoadPanel (0, "bee.uir", PANEL)) < 0)
return –1;
//设置枪(飞机)、子弹、蜜蜂、炸弹等控件背景透明
porperty ();
//装载异步定时器
timerkey = NewAsyncTimer (0.001, –1, 1, keystroke, 0);
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
//释放异步定时器资源
DiscardAsyncTimer (timerkey);
return 0;
}
//面板回调函数
int CVICALLBACK panelCB (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
int character;
int vk;
switch (event)
{
case EVENT_CLOSE:
QuitUserInterface (0);
break;
//面板尺寸改变时响应事件
case EVENT_PANEL_SIZE:
// 获得枪(飞机)、子弹、蜜蜂、炸弹等控件位置
position();
// 重新定位枪(飞机)控件Top 属性
SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, panelpp.height–50);
break;
}
return 0;
}
//底端枪(飞机)子弹发射路径以及击中蜜蜂效果
int CVICALLBACK timer (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
case EVENT_TIMER_TICK:
// 获得枪(飞机)、子弹、蜜蜂、炸弹等控件位置
position();
// 子弹向上发射路径
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_TOP, bullet.top–5);
// 当子弹超过屏幕顶端后消失
if (bullet.top < -bullet.height)
{
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_VISIBLE, 0);
shootflag = 0;
}
// 子弹击中蜜蜂的效果
if ((bullet.left+bullet.width>=bee.left) && (bullet.left<=bee.left+bee.width) && (bullet.top<=bee.
top+bee.height) && (bullet.top+bullet.height>=bee.top) && shootflag)
{
// 用击中后的图片替换原有图片
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_LEFT, bee.left);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_TOP, bee.top);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_VISIBLE, 0);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_VISIBLE, 1);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_VISIBLE, 0);
// 游戏停止
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
SetCtrlAttribute (panelHandle, PANEL_TIMER_BEE, ATTR_ENABLED, 0);
SetAsyncTimerAttribute (timerkey, ASYNC_ATTR_ENABLED, 0);
// 播放击中音乐
sndPlaySound("shootbee.wav", SND_SYNC);
// 弹出游戏结束面板
popmessage();
}
break;
}
return 0;
}
//顶端蜜蜂运动路径以及蜜蜂炸弹击中枪(飞机)效果
int CVICALLBACK beerun (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_TIMER_TICK:
// 间隔一定时间,蜜蜂发射炸弹,炸弹垂直下落
boomtime++;
if (boomtime>=50)
{
boomtime = 0;
}
if (boomtime == 0 && (!boomflag))
{
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_VISIBLE, 1);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_LEFT, bee.left);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_TOP, bee.top+bee.height);
sndPlaySound("boom.wav", SND_SYNC);
boomflag = 1;
}
else
{
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_TOP, boom.top+20);
}
// 重新获得炸弹、蜜蜂等位置信息
position();
if (boom.top>=panelpp.height)
{
boomflag = 0;
}
// 蜜蜂到达边界自动反弹,并重新计算位置
if (bee.left <= 10 || bee.left + bee.width >= panelpp.width – 10)
{
hh = -hh;
}
if (bee.top <= 10 || gun.top - bee.top <= 100)
{
vv = -vv;
}
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_LEFT, bee.left+hh*(int)Random
(0, 10));
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_TOP, bee.top+vv*(int)Random
(0, 10));
// 蜜蜂炸弹击中枪(飞机)效果
if ((boom.left+boom.width>=gun.left) && (boom.left<=gun.left+gun.width) && (boom.top+
boom.height–30>=gun.top) && (boom.top+boom.height<=gun.top+gun.height) && (boomflag))
{
// 用击中后的图片替换原有图片
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_VISIBLE, 0);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_VISIBLE, 0);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_VISIBLE, 1);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_LEFT, gun.left);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_TOP, gun.top);
// 游戏停止
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
SetCtrlAttribute (panelHandle, PANEL_TIMER_BEE, ATTR_ENABLED, 0);
sndPlaySound("dead.wav", SND_SYNC);
popmessage();
}
break;
}
return 0;
}
//退出按钮
int CVICALLBACK quit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
DiscardPanel (pophandle);
QuitUserInterface (0);
break;
}
return 0;
}
//重新开始按钮
int CVICALLBACK restart (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
// 参数重置
shootflag = 0;
boomflag = 0;
boomtime = 0;
porperty();
// 开始游戏
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 1);
SetCtrlAttribute (panelHandle, PANEL_TIMER_BEE, ATTR_ENABLED, 1);
SetAsyncTimerAttribute (timerkey, ASYNC_ATTR_ENABLED, 1);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_VISIBLE, 1);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_VISIBLE, 1);
DiscardPanel (pophandle);
break;
}
return 0;
}
//设置枪(飞机)、子弹、蜜蜂、炸弹等控件背景透明
void porperty (void)
{
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_PICT_BGCOLOR, VAL_TRANS-
PARENT);
//一些控件初始态不可见
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_VISIBLE, 0);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_VISIBLE, 0);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_VISIBLE, 0);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_VISIBLE, 0);
}
//获得枪(飞机)、子弹、蜜蜂、炸弹等控件位置
void position (void)
{
//枪(飞机)
GetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_LEFT, &gun.left);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, &gun.top);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_HEIGHT, &gun.height);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_WIDTH, &gun.width);
//子弹
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_LEFT, &bullet.left);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_TOP, &bullet.top);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_HEIGHT, &bullet.height);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_WIDTH, &bullet.width);
//蜜蜂
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_LEFT, &bee.left);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_TOP, &bee.top);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_HEIGHT, &bee.height);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_WIDTH, &bee.width);
//击中蜜蜂
GetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_LEFT, &shootbee.left);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_TOP, &shootbee.top);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_HEIGHT, &shootbee.height);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_WIDTH, &shootbee.width);
//炸弹
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_LEFT, &boom.left);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_TOP, &boom.top);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_HEIGHT, &boom.height);
GetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_WIDTH, &boom.width);
//面板
GetPanelAttribute (panelHandle, ATTR_HEIGHT, &panelpp.height);
GetPanelAttribute (panelHandle, ATTR_WIDTH, &panelpp.width);
}
//游戏结束面板
void popmessage (void)
{
pophandle = LoadPanel (0, "bee.uir", PANEL_POP);
//以模态对话框形式显示游戏结束面板
InstallPopup (pophandle);
}
//异步定时器回调函数
int CVICALLBACK keystroke(int reserved, int timerId, int event, void *callbackData, int eventData1, int
eventData2)
{
int left;
int right;
int up;
int down;
int a;
switch (event)
{
case EVENT_TIMER_TICK:
// 获得虚拟键↑↓←→的按键状态,以及是否按下a 键
right = GetAsyncKeyState(VK_RIGHT);
left = GetAsyncKeyState(VK_LEFT);
up = GetAsyncKeyState(VK_UP);
down = GetAsyncKeyState(VK_DOWN);
a = GetAsyncKeyState('A');
// 向左移动
if (left != 0)
{
SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_LEFT, gun.left–2);
}
// 向右移动
if (right != 0)
{
SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_LEFT, gun.left+2);
}
// 向上移动
if (up != 0)
{
SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, gun.top–2);
}
// 向下移动
if (down != 0)
{
SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, gun.top+2);
}
// 按下a 键,发射子弹
if (a && (!shootflag))
{
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_LEFT, gun.left+gun.
width/2–8);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_TOP, gun.top);
SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_VISIBLE, 1);
sndPlaySound("bullet.wav", SND_SYNC);
shootflag = 1;
}
break;
}
return 0;
}
上一篇:怎样采用多种单端信号驱动低功率、1Msps、±2.5V
差分输入、16
位
ADC
下一篇:LabWindows/CVI虚拟仪器设计技术场景与游戏设计之:
下雪场景