c++ - 游戏循环不工作 ncurses

标签 c++ c loops

我是 ncurses 的新手,我们有一个项目可以创建我们选择的游戏。 我的游戏的想法是拥有一艘宇宙飞船并让敌人从顶部攻击。 我刚开始编写代码就遇到了一个问题,当我使用空格键发射子弹时,子弹会移动,但是我无法在子弹移动的同时移动我的飞船。

#include<ncurses.h>

typedef struct
{/*define a structure for player information*/
    int row;
    int col;
}playerinfo;

int changeRow(int x,int m, int c)
{
    if(x+m==22 || x+m==0  )
    {
        beep();
        return x;
    }
    return x+m;
}
int changeColumn(int y,int n, int r)
{
    if(y+n==72 || y+n==0  )
    {   
        beep();
        return y;
    }
    return y+n;
}

int main(){
    initscr();              

    start_color();  
    assume_default_colors(COLOR_GREEN, COLOR_BLACK); 

    noecho();
    cbreak();                       

    curs_set(0); /* turn cursor display off */

    timeout(0); 

    keypad(stdscr,TRUE);  /* allow keypad keys to be used */            
    playerinfo playership;

    playership.row= 10;
    playership.col= 15;

    char player[] ="     X     ";
    char player2[]="    |o|    ";
    char player3[]="  xX| |Xx  ";
    char player4[]="  X | | X  ";
    char player5[]=" X__-|-__X ";

    char bullet = '.';

    int key = 0;
    int i=0;
    bool moving= false;
    mvprintw(playership.row,playership.col,player);
    mvprintw(playership.row+1,playership.col,player2);
    mvprintw(playership.row+2,playership.col,player3);
    mvprintw(playership.row+3,playership.col,player4);
    mvprintw(playership.row+4,playership.col,player5);
    timeout(0);

    while(key!='q'){ 
        usleep(17000);

    mvprintw(playership.row,playership.col,"         ");
    mvprintw(playership.row+5,playership.col,"          ");

    key = getch (); 

    switch(key){ 
        case KEY_UP: playership.row=changeRow(playership.row,-1,playership.col); /* move up */ 
            break; 
        case KEY_DOWN: playership.row=changeRow(playership.row,+1,playership.col); /* move down */ 
            break; 
        case KEY_LEFT:playership.col=changeColumn(playership.col,-1,playership.row); /* move left */ 
            break; 
        case KEY_RIGHT:playership.col=changeColumn(playership.col,+1,playership.row); /* move right */ 
            break; 
        case ' ': moving=true; break;
            default: break; /* do nothing if other keys */ 
    }

    mvprintw(playership.row,playership.col,player);
    mvprintw(playership.row+1,playership.col,player2);
    mvprintw(playership.row+2,playership.col,player3);
    mvprintw(playership.row+3,playership.col,player4);
    mvprintw(playership.row+4,playership.col,player5);

    if (moving==true){
        for( i=0; i <24; i++){ 
            refresh; mvprintw(playership.row-i-2,playership.col+5,"%c", bullet); mvprintw(playership.row,playership.col,player); 
            refresh();usleep(12000); mvprintw(playership.row-i-1,playership.col+5," ");} moving=false; }
            refresh(); 
        }


    echo();   /* turn echo back on */

    endwin(); /* End curses mode */

    return 0;
} 

最佳答案

那是因为您在游戏循环中嵌套了“开火”代码,所以在您执行该动画时所有输入都停止了。您需要将您的逻辑更改为一种状态机:

    switch(key)
    {
    // ...
    case ' ':
        if( !bullet_active )
        {
            bullet_active = true;
            bullet_pos = 0;
        }
        break;
    }

    if( bullet_active ) {
        // TODO: Draw bullet at bullet_pos

        if( ++bullet_pos == 24 ) bullet_active = false;
    }

关于c++ - 游戏循环不工作 ncurses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19781360/

相关文章:

c++ - 如何以与 VS 解决方案(VS 自动构建依赖项)类似的方式管理自定义库?

c++ - GSL 使用勒让德多项式的问题

c++ - 如何通过 C++ 从 GroupName 中检索组 SID

c - 递归反转一个句子代码。为什么常量指针指向常量字符?

java - 为什么这个 do/while 循环卡在 inf 中?环形?

C++ 如何 std::accumulate future vector ?

c - 如何在 C 中创建一个新的文本文件?

c - 如何选择缓冲区大小?

Excel宏循环通过列来分配单元格名称框

javascript - 如何循环遍历数组并使用JS选择没有索引值的元素