您的位置:首页 > 职场人生

如果不测试,作为程序员的你绝对不知道自己有多么粗心

2015-03-22 14:17 357 查看
          历时将近两个月, 我们的Kinect体感游戏项目进展顺利,相应的测试也在进行当中。

          周五早上,我和队友们在实验室测试了Kinect的功能。整个测试过程真是让人惭愧,我简直是太粗心了,代码中的错误层出不穷。

          由于进行的是双人模式,因此写完程序难以立即测试,只能等队友们都有时间的时候一起测试。 

                                


          我们的项目包括八个不同的动作对应不同的操作。最开始的测试很不顺利,腿部的动作非常奇怪,为了更快的发现出错的位置,我们注释掉了腿部的动作,只看手部动作。

           

SkeletonPoint playerA_head = two[0].Joints[JointType.Head].Position;
SkeletonPoint playerB_head = two[1].Joints[JointType.Head].Position;

SkeletonPoint playerA_leftshoulder = two[0].Joints[JointType.ShoulderLeft].Position;
SkeletonPoint playerB_leftshoulder = two[1].Joints[JointType.ShoulderLeft].Position;

SkeletonPoint playerA_rightshoulder = two[0].Joints[JointType.ShoulderRight].Position;
SkeletonPoint playerB_rightshoulder = two[1].Joints[JointType.ShoulderRight].Position;

SkeletonPoint playerA_leftHand = two[0].Joints[JointType.HandLeft].Position;
SkeletonPoint playerB_leftHand = two[1].Joints[JointType.HandLeft].Position;

SkeletonPoint playerA_rightHand = two[0].Joints[JointType.HandRight].Position;
SkeletonPoint playerB_rightHand = two[1].Joints[JointType.HandRight].Position;
         

             程序的逻辑是根据手部与肩部的距离判断手部的动作,因为存在大量的代码复制,导致在应该写left 的地方粘贴过来没有修改,还是right。这是测试过程中发现的第一个问题。

             手部功能正常进行后,我们打开了腿部代码的注释,开始对腿部的功能进行测试。程序的逻辑是左腿抬起触发事件一,右腿抬起触发事件二。可是两位队友无论将腿抬起多高都无法触发事件。检查代码之后我们发现设置的腿部抬起阈值是一米,

private const double LegXStretchedThreshold = 0.2; //腿部X轴方向伸展的阀值,单位米
private const double LegYStretchedThreshold = 1.0; //腿部Y轴方向伸展的阀值,单位米
      

              终于找到了出现问题的原因,阈值设置了这么大,队友们得跳起来才能达到要求的阈值。我们以为改正了这个问题以后,程序就可以正常运行了,可是再次运行我们发现还是无法触发事件。仔细阅读源代码后,我们发现,程序的逻辑是判断头部与脚部的距离,这也就是一米的阈值出现的原因。

                        

bool playerA_isLeftFootStretched = ((playerA_head.Y - playerA_rightFoot.Y) < LegYStretchedThreshold);
if (playerA_isLeftFootStretched)
{
KeyboardToolkit.Keyboard.Type(Key.K);
PlayerA_leftlegraised = true;
}


                 在实际的应用中,这种判断方法其实是不合理的,因为我们的用户有身高一米五的萌妹子,也有身高一米八的女汉子,这种情况下再判断头部与脚部的距离就是造成问题。于是,我们将逻辑改为判断左脚与右脚的距离。代码如下:

bool playerA_isLeftFootStretched = ((playerA_leftFoot.Y - playerA_rightFoot.Y) > LegYStretchedThreshold);
if (playerA_isLeftFootStretched)
{
KeyboardToolkit.Keyboard.Type(Key.K);
PlayerA_leftlegraised = true;
}


                   终于能够顺利触发腿部事件后,我们发现,抬腿时同一事件会被触发好多次,这感觉有点像按下键盘按键不放开了,我们想要的是按一下就好。这个问题的解决方法比较大众化,就是加一个bool变量记录是否已经按下,代码如下: 

bool playerA_isLeftFootStretched = ((playerA_leftFoot.Y - playerA_rightFoot.Y) > LegYStretchedThreshold);
if (playerA_isLeftFootStretched)
{
if (!PlayerA_leftlegraised)
{
KeyboardToolkit.Keyboard.Type(Key.K);
PlayerA_leftlegraised = true;
}
}


   

                经过了测试,我们的程序终于能够正常的运行了。没有进行过测试的程序难以让人放心。

                  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# kinect 测试 源代码
相关文章推荐