• 易迪拓培训,专注于微波、射频、天线设计工程师的培养
首页 > 测试测量 > 技术文章 > LabWindows/CVI虚拟仪器设计技术场景与游戏设计之: 打字练习

LabWindows/CVI虚拟仪器设计技术场景与游戏设计之: 打字练习

录入:edatop.com    点击:

(2)程序源代码

//头文件声明
#include <formatio.h>
#include "toolbox.h"

#include <cvirte.h>
#include <userint.h>
#include "打字练习.h"
static int panelHandle;
//声明得分为全局静态变量
static int score;
//声明自定义初始化函数
int InitChar (int choice);
//主函数
int main (int argc, char *argv[])

if (InitCVIRTE (0, argv, 0) == 0)
return –1;
if ((panelHandle = LoadPanel (0, " 打字练习.uir", PANEL)) < 0)
return –1;
//初始化要显示的下落字符
InitChar (0);
InitChar (1);
//设置下落字符背景透明
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG, ATTR_TEXT_BGCOLOR, VAL_TRANSPARENT);
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG_1, ATTR_TEXT_BGCOLOR, VAL_TRANSPARENT);
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;

//面板回调函数
int CVICALLBACK PanleCB (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
char str[2];
switch (event)
{
//响应键盘事件
case EVENT_KEYPRESS:
str[0] =  '';
GetCtrlVal (panelHandle, PANEL_TEXTMSG, str);
// 判断按键是否与下落字符一致
if (eventData1 == str[0])
{
InitChar (0);
// 加分
score ++;
SetCtrlVal (panelHandle, PANEL_NUMERIC_SCORE, score);
break;

}
str[0] =  '';
GetCtrlVal (panelHandle, PANEL_TEXTMSG_1, str);
if (eventData1 == str[0])
{
InitChar (1);
score ++;
SetCtrlVal (panelHandle, PANEL_NUMERIC_SCORE, score);
break;
}
break;
case EVENT_CLOSE:
  QuitUserInterface (0);
  break;

return 0; 
}  
//定时器1——产生字符下落效果
int CVICALLBACK timer1 (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int Cheight;
int speed;
int top;
int top1;
switch (event)
{
case EVENT_TIMER_TICK:
// 获得下载字符Top 属性
GetCtrlAttribute (panelHandle, PANEL_TEXTMSG, ATTR_TOP, &top);
GetCtrlAttribute (panelHandle, PANEL_TEXTMSG_1, ATTR_TOP, &top1);
// 获得速度值
GetCtrlVal (panelHandle, PANEL_NUMERIC_SPEED, &speed);
// 设置字符下落速度
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG, ATTR_TOP, top + speed);
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG_1, ATTR_TOP, top1 + speed);
GetCtrlAttribute (panelHandle, PANEL_BACKGROUND, ATTR_HEIGHT, &Cheight);
// 当下落字符淡出屏幕,重新从顶端下落
if (top + speed> Cheight)
{
InitChar (0);
}
if (top1 + speed> Cheight)
{
InitChar (1);
}

  break;

return 0; 
}  
//定时器2——计时
int CVICALLBACK timer2 (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
static int i = 0;
int sel;
char str[100];
char strscore[10];
switch (event)
{
case EVENT_TIMER_TICK:
// 设置显示时间
SetCtrlVal (panelHandle, PANEL_NUMERICTHERM_TIME, i);
// 当时间大于等于60s 时,重新开始计时
if (i >= 60)
{
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
SetCtrlAttribute (panelHandle, PANEL_TIMER_2, ATTR_ENABLED, 0);
Fmt (strscore, "%s<%i", score);
// 计算每分钟打字速度
str[0] =  '';
strcat (str, " 您一分钟的打字速度是:");
strcat (str, strscore);
strcat (str, " 个字!");
// 弹出提示对话框
sel = GenericMessagePopup (" 提示", str, " 继续", "退出", 0, 0, 0, 0, VAL_GENERIC_
POPUP_BTN1, VAL_GENERIC_POPUP_BTN1, VAL_GENERIC_POPUP_BTN2);
if (sel == 1)
{
// 选择继续按钮
start (panelHandle, PANEL_CMD_START, EVENT_COMMIT, 0, 0, 0);
SetCtrlVal (panelHandle, PANEL_NUMERIC_SCORE, 0);
score = 0;
}
else
{
// 选择退出按钮
PanleCB (panelHandle, EVENT_CLOSE, 0,  0, 0);
}
i = 0;

  i ++;
  break;

return 0; 
}  
//开始按钮
int CVICALLBACK start (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
// 设置下落字符可见
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG, ATTR_VISIBLE, 1);
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG_1, ATTR_VISIBLE, 1);
// 启动定时器
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 1);
SetCtrlAttribute (panelHandle, PANEL_TIMER_2, ATTR_ENABLED, 1);
break;
}
return 0;
}
//暂停按钮
int CVICALLBACK pause (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
SetCtrlAttribute (panelHandle, PANEL_TIMER_2, ATTR_ENABLED, 0);
break;
}
return 0;
}
//随机产生字符
int InitChar (int choice)
{
int CTop;
int CWidth;
char str[2] = {0x00, 0x00};
GetCtrlAttribute (panelHandle, PANEL_BACKGROUND, ATTR_WIDTH, &CWidth);
GetCtrlAttribute (panelHandle, PANEL_BACKGROUND, ATTR_TOP, &CTop);
//产生随机字符
SetRandomSeed (0);
str[0] = Random (97, 123);

//设置两个下落字符的Left 属性
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG + choice, ATTR_LEFT, CWidth * (int)(Random (97, 123)
– 97) / 27); 
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG + choice, ATTR_TOP, CTop);
//choice 为0 时,设置控件PANEL_TEXTMSG ,为1 时,设置控件PANEL_TEXTMSG_1
SetCtrlVal (panelHandle, PANEL_TEXTMSG + choice, str);
return 0;

 

点击浏览:矢量网络分析仪、频谱仪、示波器,使用操作培训教程

上一篇:基于模块化设计的R&S BTC可优化执行各种测试任务
下一篇:怎样采用多种单端信号驱动低功率、1Msps、±2.5V 差分输入、16 位 ADC

微波射频测量操作培训课程详情>>
射频和天线工程师培训课程详情>>

  网站地图