2012-09-24 49 views
0

我正在使用Ncurses庫在C++中製作Pacman。我能夠將Pacman與我的代碼一起移動,但在不同的方向上切換需要很長時間。例如,當Pacman向左移動並按下右箭頭鍵時,開始向右移動需要一些時間。C++遊戲沒有時間響應鍵

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()的值。

+7

「了大量的時間來切換」 ......什麼是「大量」?你自己的代碼有評論說「這會暫停1秒」。如果你的「很多」是「1秒」,那麼你的答案是。 –

+0

需要那段時間時屏幕上會發生什麼?它還在畫什麼,即動畫? – user1201210

+1

你不是依靠重複使pacman繼續移動,是嗎?這會違反pacman的精神,也可能是導致他「遲緩」的原因。 – paddy

回答

1

也許是waitf(0.2)導致程序在讀取更多密鑰之前等待?如果有輸入,你可以考慮中斷等待......你可以使用定時選擇。

+0

你的意思是按時間選擇? – User14229754

+0

看到這個例子:[stdin select](http://stackoverflow.com/questions/5360660/using-select-system-call-for-listening-on-stdin-and-the-server)也查找API參考選擇,你可以看到你讓它等待一段特定的時間而不是永遠。 – mark

0

不要把所有東西放在鍵盤例程中。它減慢了一切。

嘗試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(); 

    } 

}