声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2276|回复: 1

[LabView] [转贴]利用LabWindows/CVI制作一个打蜜蜂游戏

[复制链接]
发表于 2007-4-3 05:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
  大家一定很怀念儿时玩电视游戏机的快乐时光吧!那个时候感觉游戏很神奇,怎么按一下按钮,动画场景中的人就会动作呢?我记得我玩的第一个游戏便是小蜜蜂游戏。天上有一群小蜜蜂,在背景音乐,在不停地“嗡嗡”地叫着,我右手按住方向键,左手按下发射按钮,从小飞机中飞出子弹,射向小蜜蜂;当然,小蜜蜂也不是白给的,它们会不停地发出炸弹,想把我的飞机炸死!就在这无限的循环游戏中,我的童年消失了!
  不说那些陈年往事了!现在工作难找呀!我感觉学习LabWindows/CVI对我最大的打击就是工作难找!找不到工作,或者工作不满意,每天在郁闷之中,唯有那一段段C代码能让我心情平静!
  这个游戏很平常,只是用上下箭头来控制方向,“A”用来发射子弹!小蜜蜂(没有长翅膀)在天空中飞来飞去,还不停地扔下炸弹,不要被它所发射的炸弹碰到!

  游戏界面如下:
4b67707502000dwq.jpg

代码为:

  1. #include "windows.h"
  2. #include "mmsystem.h"
  3. #include "toolbox.h"
  4. #include
  5. #include   
  6. #include
  7. #include "bee.h"
  8. static int pophandle;
  9. static int panelHandle;
  10. static int shootflag = 0;  //子弹可见为1,不可见为0
  11. static int boomflag = 0;
  12. static int boomtime = 0;
  13. static int hh = 3;
  14. static int vv = 2;
  15. void porperty (void);
  16. void position (void);  
  17. void popmessage (void);
  18. Rect gun;
  19. Rect bullet;
  20. Rect bee;
  21. Rect shootbee;
  22. Rect boom;
  23. Rect panelpp;

  24. int main (int argc, char *argv[])
  25. {
  26. if (InitCVIRTE (0, argv, 0) == 0)
  27.   return -1; /* out of memory */
  28. if ((panelHandle = LoadPanel (0, "bee.uir", PANEL)) < 0)
  29.   return -1;

  30. porperty();

  31. DisplayPanel (panelHandle);
  32. RunUserInterface ();
  33. DiscardPanel (panelHandle);
  34. return 0;
  35. }
  36. int CVICALLBACK panelCB (int panel, int event, void *callbackData,
  37.   int eventData1, int eventData2)
  38. {
  39. int character;
  40. int virtualkey;
  41. switch (event)
  42. {
  43.   case EVENT_KEYPRESS:
  44.    virtualkey = GetKeyPressEventVirtualKey (eventData2);
  45.    character = GetKeyPressEventCharacter (eventData2);
  46.    
  47.    position();
  48.    
  49.    switch (virtualkey)
  50.    {
  51.     case VAL_LEFT_ARROW_VKEY:
  52.      SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_LEFT, gun.left-10);
  53.      break;
  54.     case VAL_RIGHT_ARROW_VKEY:
  55.      SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_LEFT, gun.left+10);
  56.      break;
  57.     case VAL_UP_ARROW_VKEY:
  58.      SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, gun.top-10);
  59.      break;
  60.     case VAL_DOWN_ARROW_VKEY:
  61.      SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, gun.top+10);   
  62.      break;
  63.    }
  64.    
  65.    if (('a' == tolower (character)) && (!shootflag))
  66.    {
  67.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_LEFT, gun.left+gun.width/2-8);
  68.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_TOP, gun.top);
  69.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_VISIBLE, 1);
  70.     sndPlaySound("bullet.wav", SND_SYNC);
  71.     shootflag = 1;
  72.    }
  73.    
  74.    
  75.    break;
  76.   case EVENT_GOT_FOCUS:
  77.    break;
  78.   case EVENT_LOST_FOCUS:
  79.    break;
  80.   case EVENT_CLOSE:
  81.    QuitUserInterface (0);
  82.    break;
  83.   case EVENT_PANEL_SIZE:
  84.    position();
  85.    SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, panelpp.height-50);
  86.    break;
  87. }
  88. return 0;
  89. }
  90. int CVICALLBACK timer (int panel, int control, int event,
  91.   void *callbackData, int eventData1, int eventData2)
  92. {
  93. switch (event)
  94. {
  95.   case EVENT_TIMER_TICK:
  96.    position();
  97.    
  98.    SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_TOP, bullet.top-5);
  99.    
  100.    if (bullet.top < -bullet.height)
  101.    {
  102.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_VISIBLE, 0);
  103.     shootflag = 0;
  104.    }
  105.    
  106.    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)
  107.    {
  108.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_LEFT, bee.left);
  109.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_TOP, bee.top);
  110.    
  111.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_VISIBLE, 0);
  112.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_VISIBLE, 1);
  113.    
  114.     SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
  115.     SetCtrlAttribute (panelHandle, PANEL_TIMER_BEE, ATTR_ENABLED, 0);
  116.    
  117.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_VISIBLE, 0);
  118.    
  119.     sndPlaySound("shootbee.wav", SND_SYNC);
  120.    
  121.     popmessage();
  122.    }
  123.    
  124.    
  125.    
  126.    break;
  127. }
  128. return 0;
  129. }
  130. int CVICALLBACK beerun (int panel, int control, int event,
  131.   void *callbackData, int eventData1, int eventData2)
  132. {
  133. switch (event)
  134. {
  135.   case EVENT_TIMER_TICK:
  136.    
  137.    
  138.    boomtime++;
  139.    
  140.    if (boomtime>=50)
  141.    {
  142.     boomtime = 0;
  143.    }
  144.    
  145.    if (boomtime == 0 && (!boomflag))
  146.    {
  147.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_VISIBLE, 1);
  148.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_LEFT, bee.left);
  149.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_TOP, bee.top+bee.height);
  150.    
  151.     sndPlaySound("boom.wav", SND_SYNC);
  152.    
  153.     boomflag = 1;
  154.    }
  155.    else
  156.    {
  157.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_TOP, boom.top+10);
  158.    }
  159.    
  160.    position();
  161.    
  162.    if (boom.top>=panelpp.height)
  163.    {
  164.     boomflag = 0;
  165.    }
  166.    
  167.    
  168.    if (bee.left<=10 || bee.left+bee.width>=panelpp.width-10)
  169.    {
  170.     hh=-hh;  
  171.    }
  172.    
  173.    if (bee.top<=10 || gun.top-bee.top<=100)
  174.    {
  175.     vv=-vv;
  176.    }
  177.    
  178.    SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_LEFT, bee.left+hh*10);
  179.    SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_TOP, bee.top+vv*10);
  180.    
  181.    
  182.    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))
  183.    {
  184.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_VISIBLE, 0);
  185.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_VISIBLE, 0);
  186.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_VISIBLE, 1);
  187.    
  188.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_LEFT, gun.left);
  189.     SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_TOP, gun.top);
  190.    
  191.    
  192.     SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
  193.     SetCtrlAttribute (panelHandle, PANEL_TIMER_BEE, ATTR_ENABLED, 0);
  194.    
  195.     sndPlaySound("dead.wav", SND_SYNC);
  196.    
  197.     popmessage();
  198.    }
  199.    
  200.    
  201.    
  202.    
  203.    break;
  204. }
  205. return 0;
  206. }
  207. int CVICALLBACK messageok (int panel, int control, int event,
  208.   void *callbackData, int eventData1, int eventData2)
  209. {
  210. switch (event)
  211. {
  212.   case EVENT_COMMIT:
  213.    DiscardPanel (pophandle);
  214.    break;
  215. }
  216. return 0;
  217. }
  218. void porperty (void)
  219. {
  220. SetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
  221. SetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
  222. SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
  223. SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
  224. SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);
  225. SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_PICT_BGCOLOR, VAL_TRANSPARENT);

  226. SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_VISIBLE, 0);
  227. SetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_VISIBLE, 0);
  228. SetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_VISIBLE, 0);
  229. SetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTGUN, ATTR_VISIBLE, 0);
  230. }
  231. void position (void)
  232. {
  233. GetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_LEFT, &gun.left);
  234. GetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_TOP, &gun.top);
  235. GetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_HEIGHT, &gun.height);
  236. GetCtrlAttribute (panelHandle, PANEL_PICTURE_GUN, ATTR_WIDTH, &gun.width);

  237. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_LEFT, &bullet.left);
  238. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_TOP, &bullet.top);
  239. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_HEIGHT, &bullet.height);
  240. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BULLET, ATTR_WIDTH, &bullet.width);

  241. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_LEFT, &bee.left);
  242. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_TOP, &bee.top);
  243. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_HEIGHT, &bee.height);
  244. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BEE, ATTR_WIDTH, &bee.width);

  245. GetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_LEFT, &shootbee.left);
  246. GetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_TOP, &shootbee.top);
  247. GetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_HEIGHT, &shootbee.height);
  248. GetCtrlAttribute (panelHandle, PANEL_PICTURE_SHOOTBEE, ATTR_WIDTH, &shootbee.width);

  249. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_LEFT, &boom.left);
  250. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_TOP, &boom.top);
  251. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_HEIGHT, &boom.height);
  252. GetCtrlAttribute (panelHandle, PANEL_PICTURE_BOOM, ATTR_WIDTH, &boom.width);

  253. GetPanelAttribute (panelHandle, ATTR_HEIGHT, &panelpp.height);
  254. GetPanelAttribute (panelHandle, ATTR_WIDTH, &panelpp.width);
  255. }
  256. void popmessage (void)
  257. {
  258. pophandle = LoadPanel (0, "bee.uir", PANEL_POP);
  259. InstallPopup (pophandle);
  260.   
  261. }
复制代码


转自:小信的BLOG  http://blog.sina.com.cn/u/4b677075010006j4

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2007-4-3 09:34 | 显示全部楼层

噢!很好吧!

试试看了!
很不错吧!
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-28 21:39 , Processed in 0.066298 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表