C++游戏对按键没有时间响应



我正在C++用Ncurses库制作Pacman。我能够用我的代码移动 Pacman,但在不同方向之间切换需要很多时间。例如,当吃豆人向左移动并且我按向右箭头键时,它需要一些时间才能开始向右移动。

 if (ch==KEY_LEFT)
 {
    int b,row,column;
    getyx(stdscr,row,column);
    for (b=column;b>=0;b-=1)    //loop to move the pacman left until it hits the wall
    {
     mvprintw(row,b,">");  //print the ">" symbol
     refresh();
     waitf(0.2);
     attron(COLOR_PAIR(1));      //this pauses the game for 1 second
     mvprintw(row,b,">");
     attroff(COLOR_PAIR(1));
     refresh();
     waitf(0.2);
     mvprintw(row,(b),"O");  //showing the open mouth of pacman
     refresh();
     waitf(0.2);
     attron(COLOR_PAIR(1));a
     mvprintw(row,(b),"O");
      attroff(COLOR_PAIR(1));
      int h=0;
      h=getch();
      if (h!=KEY_LEFT)
      {
          break;
      }
  }
}
 right=getch();
 loop for right in an if condition
 up=getch();
 loop for up in an if condition
 down=getch();
 loop for moving down in an if condition 

我为右,向上和向下做了同样的事情。此外,我在每个 if 语句之前引入了新变量来存储 getch() 的值。

也许是waitf(0.2)导致程序在读取更多键之前等待? 如果有输入,您可以考虑中断等待...您可以改用定时选择。

不要把所有东西都放在键盘例程中。它减慢了一切。

尝试if (ch==KEY_LEFT) go_left=true然后在密钥处理 rouine 之外的函数中执行所有这些操作。

go_left==true一直保持true,直到按下ch==KEY_RIGHT,然后go_left=false.

示例游戏结构:

#include <stdio.h>
#include <conio.h>
#include <iostream> 
//#include "gamestuff.h"
using namespace std;

#define KB_ESCAPE 27
#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_F8 66

int keypress=0;
bool go_left=false;
bool go_right=false;
bool go_up=false;
bool go_down=false;
void game_menu();
void keyboard_stuff();
void graphics_stuff();
void game_loop();

int main()
{
 game_menu();
 game_loop();
 return 0;
}
void game_menu()
{
    cout<<"Pacman Game  n";
    cout<<"  n";
    cout<<"[1] Resume Game  n";
    cout<<"[2] Save Game  n";
    cout<<"[Esc] Exit Game  n";
    cout<<"  n";
}

void keyboard_stuff()
{
    if (kbhit())
      {
            keypress = getch();
            //cout<<"KB_code = "<<KB_code<<"n";
            switch (keypress)
            {
                case KB_ESCAPE:

                break;
                case KB_LEFT:
                           //go_left
                    go_left=true;
                    go_right=false;
                break;
                case KB_RIGHT:
                           //go_right   
                    go_left=false;
                    go_right= true ;
                break;
                case KB_UP:
                           //go_up  
                    go_up=true;
                    go_down=false;
                break;
                case KB_DOWN:
                           //go_down  
                    go_up=false;
                    go_down=true;
                break;
            }        
      }
}
void graphics_stuff()
{
    //put all graphics and draw stuff here
    if(go_left)
    {
        //Update  direction etc

    }
    if(go_right)
    {
        //Update   direction etc

    }
    if(go_up)
    {
        //Update   direction etc

    }
    if(go_down)
    {
        //Update   direction etc

    }
    //draw pacman stuff right here after the above
}
void game_loop()
{
   while(keypress!=KB_ESCAPE)
   {
     keyboard_stuff();
     graphics_stuff();
   }
}

相关内容

  • 没有找到相关文章