LabWindows/CVI虚拟仪器设计技术场景与游戏设计之: 打蜜蜂
CMD_QUIT | Command Button | 标题:退出回调函数:quit |
(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)
{
LabWindows 虚拟仪器 CVI 场景 游戏 相关文章:
- 用于雷达测试和验证的自动化测试设备(05-15)
- 基于LabWindows的录波分析仪的设计(03-01)
- LabWindows/CVI虚拟仪器设计技术基本控件使用之: Tab 控件(11-13)
- LabWindows/CVI虚拟仪器设计技术场景与游戏设计之:时钟制作(11-13)
- LabWindows/CVI虚拟仪器设计技术场景与游戏设计之: 下雪场景(11-13)
- LabWindows/CVI虚拟仪器设计技术基本控件使用之:回调函数(11-13)