iwan_T 2020-06-23 17:07 采纳率: 0%
浏览 645

请问一下C语言怎么实现长按按键执行某样操作啊,比如贪吃蛇长按方向键实现加速。

while(1)

{

        LoadKeyboardLayout("0x409", KLF_ACTIVATE | KLF_SETFORPROCESS);  //切换为英文输入法

        char s1[10], s2[10];        //保存成绩

            settextcolor(GREEN);
        sprintf(s1, "玩家1分数:%d", score);   //将成绩格式化输出到字符串s1中 
        outtextxy(0, 0, s1);
        sprintf(s2, "玩家2分数:%d", score2);  //将成绩格式化输出到字符串s2中 
        outtextxy(0, 20, s2);

            outtextxy(1050,200,_T("按空格键暂停游戏,按p退出游戏!"));
     // 获取按键

        t2 = GetTickCount();

        if (kbhit())
        {
            c = getch();

            t2 = GetTickCount();
            t1=t2;

            if(c==' ') //按空格键游戏暂停
            {    
                outtextxy(520, 320, "游戏暂停,按回车键继续游戏");

                getchar();
                settextcolor(WHITE);
                outtextxy(520, 320, "游戏暂停,按回车键继续游戏");
            }
            if(c=='p') //按p退出游戏
            {  
                exit(0);
            }

            // 根据按键设置蛇的运动方向
            switch(c)
            {
                case 'a':   //向左,原方向为上下才可以变
                    if(snake->headDirection == 'w' || snake->headDirection == 's' )snake->headDirection = c ; break;
                case 'd':   //向右,原方向为上下才可以变
                    if(snake->headDirection == 'w' || snake->headDirection == 's' )snake->headDirection = c ; break;
                case 'w':   //向上,原来为左右才变化
                    if(snake->headDirection == 'a' || snake->headDirection == 'd' )snake->headDirection = c ; break;
                case 's':   //向下,原来为左右才变化
                    if(snake->headDirection == 'a' || snake->headDirection == 'd' )snake->headDirection = c ; break;
                case 75:   //向左,原方向为上下才可以变
                    if(snake2->headDirection == 'w' || snake2->headDirection == 's' )snake2->headDirection = 'a' ; break;
                case 77:   //向右,原方向为上下才可以变
                    if(snake2->headDirection == 'w' || snake2->headDirection == 's' )snake2->headDirection = 'd' ; break;
                case 72:   //向上,原来为左右才变化
                    if(snake2->headDirection == 'a' || snake2->headDirection == 'd' )snake2->headDirection = 'w' ; break;
                case 80:   //向下,原来为左右才变化
                    if(snake2->headDirection == 'a' || snake2->headDirection == 'd' )snake2->headDirection = 's' ; break;
            }
        }

        judge=moveSnake(snake,1,snake2);   //按照蛇的运动方向移动
        if(judge==5)
        {
            settextcolor(GREEN);
            outtextxy(455, 320, "玩家1撞到了自己的身体,恭喜玩家2获得了胜利,游戏结束!");
            outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
            break;
        }
        if(judge==6)
        {
            settextcolor(GREEN);
            outtextxy(455, 320, "玩家2撞到了自己的身体,恭喜玩家1获得了胜利,游戏结束!");
            outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
            break;
        }


        if(judge==0)
        {
            settextcolor(GREEN);
            outtextxy(455, 320, "玩家1撞墙了,恭喜玩家2获得了胜利,游戏结束!");
            outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
            score=0;
            score2=0;
            break;
        }
        else if(judge==2)
        {
            settextcolor(GREEN);
            outtextxy(455, 320, "玩家1撞到了玩家2的身体,恭喜玩家2获得了胜利,游戏结束!");
            outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
            score=0;
            score2=0;
            break;
        }
        judge=moveSnake(snake2,2,snake); 
        if(judge==0)
        {
            settextcolor(GREEN);
            outtextxy(455, 320, "玩家2撞墙了,恭喜玩家1获得了胜利,游戏结束!");
            outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
            score=0;
            score2=0;
            break;

        }
        else if(judge==2)
        {
            settextcolor(GREEN);
            outtextxy(455, 320, "玩家2撞到了玩家1的身体,恭喜玩家1获得了胜利,游戏结束!");
            outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
            score=0;
            score2=0;
            break;
         }

        // 延时
        Sleep(v-70);

        if(t2 - t1 > 50)
        {
            v=50;
            t1=t2;
        }
    } 

  • 写回答

1条回答 默认 最新

  • qq_20677505 2020-06-26 16:07
    关注

    长按一般就是加上时间的判断,长按时间内都为同一个键值即判断为长按。注意要添加消抖。释放和按下都要做消抖。消抖时间一般几十毫秒。

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记