首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

[原创]利用LabWindows/CVI制作一个打蜜蜂游戏

[原创]利用LabWindows/CVI制作一个打蜜蜂游戏

大家一定很怀念儿时玩电视游戏机的快乐时光吧!那个时候感觉游戏很神奇,怎么按一下按钮,动画场景中的人就会动作呢?我记得我玩的第一个游戏便是小蜜蜂游戏。天上有一群小蜜蜂,在背景音乐,在不停地“嗡嗡”地叫着,我右手按住方向键,左手按下发射按钮,从小飞机中飞出子弹,射向小蜜蜂;当然,小蜜蜂也不是白给的,它们会不停地发出炸弹,想把我的飞机炸死!就在这无限的循环游戏中,我的童年消失了!
  不说那些陈年往事了!现在工作难找呀!我感觉学习LabWindows/CVI对我最大的打击就是工作难找!找不到工作,或者工作不满意,每天在郁闷之中,唯有那一段段C代码能让我心情平静!
  这个游戏很平常,只是用上下箭头来控制方向,“A”用来发射子弹!小蜜蜂(没有长翅膀)在天空中飞来飞去,还不停地扔下炸弹,不要被它所发射的炸弹碰到!
 
  游戏界面如下:
 
 
 
 
代码为:
 
#include "windows.h"
#include "mmsystem.h"
#include "toolbox.h"
#include
#include   
#include
#include "bee.h"
static int pophandle;
static int panelHandle;
static int shootflag = 0;  //子弹可见为1,不可见为0
static int boomflag = 0;
static int boomtime = 0;
static int hh = 3;
static int vv = 2;
void porperty (void);
void position (void);  
void popmessage (void);
Rect gun;
Rect bullet;
Rect bee;
Rect shootbee;
Rect boom;
Rect panelpp;
 
int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "bee.uir", PANEL)) < 0)
  return -1;
 
 porperty();
 
 DisplayPanel (panelHandle);
 RunUserInterface ();
 DiscardPanel (panelHandle);
 return 0;
}
int CVICALLBACK panelCB (int panel, int event, void *callbackData,
  int eventData1, int eventData2)
{
 int character;
 int virtualkey;
 switch (event)
 {
  case EVENT_KEYPRESS:
   virtualkey = GetKeyPressEventVirtualKey (eventData2);
   character = GetKeyPressEventCharacter (eventData2);
   
   position();
   
   switch (virtualkey)
   {
    case VAL_LEFT_ARROW_VKEY:
     SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_LEFT, gun.left-10);
     break;
    case VAL_RIGHT_ARROW_VKEY:
     SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_LEFT, gun.left+10);
     break;
    case VAL_UP_ARROW_VKEY:
     SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, gun.top-10);
     break;
    case VAL_DOWN_ARROW_VKEY:
     SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, gun.top+10);   
     break;
   }
   
   if (('a' == tolower (character)) && (!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;
  case EVENT_GOT_FOCUS:
   break;
  case EVENT_LOST_FOCUS:
   break;
  case EVENT_CLOSE:
   QuitUserInterface (0);
   break;
  case EVENT_PANEL_SIZE:
   position();
   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_TIMER, ATTR_ENABLED, 0);
    SetCtrlAttribute (panelHandle, PANEL_TIMER_BEE, ATTR_ENABLED, 0);
    
    SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_VISIBLE, 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+10); 
   }
   
   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*10); 
   SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_TOP, bee.top+vv*10);
   
   
   if ((boom.left+boom.width>=gun.left) && (boom.left<=gun.left+gun.width) && (boom.top+boom.height>=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 messageok (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   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_TRANSPARENT);
 
 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);
  
}
 
 
 
 
 
转自:小信的博客
网址:http://blog.sina.com.cn/u/1265070197
VR1Ei8z6.rar (87.41 KB)

好东西  看看

hehe ,bu cuo .不过做这个浪费了。

加油啊。 作中国最好的测试系统
返回列表