运动不起作用的诅咒游戏

movement not working ncurses game

本文关键字:游戏 诅咒 不起作用 运动      更新时间:2023-10-16

我正在制作一个ncurses游戏,其中有一艘宇宙飞船向其他敌人发射子弹。我让船发射子弹,当我发射多颗子弹时,只有最新的子弹会移动,其余的会保持静止。

int i=0 , j=-1;
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 ' ': {j++; bullets[0].col=playership.col+5; bullets[j].row=playership.row-2 ;break;}
    default: break; /* do nothing if other keys */ 
    }
 if (j!=-1){
     attrset(COLOR_PAIR(2));
     mvprintw(bullets[j].row,bullets[0].col,"%c",bullet);
     mvprintw(bullets[j].row+1,bullets[0].col," ");
     bullets[j].row=bullets[j].row-1;
     refresh();
 }

试图实现我之前问题的回答中的评论中的建议,但我认为我做得不对:

如果一次可以有 5 颗子弹,则需要存储它们的位置。 如果你有int bullet_pos[5],那就好了。您可以在 -1 中使用 每个位置都说没有子弹处于活动状态。然后当你想 开火一个,你搜索数组找到第一个位置是 -1 并将其更改为 0。当你画出子弹时,你会经历 数组并为任何非 -1 的位置绘制项目符号,然后更新 它的位置。

如果还没有,请尝试向项目符号结构添加标志。 像alive.

当您想要射击时,请检查阵列并找到未使用的子弹位置(如果有):

for( int i = 0; i < MAX_BULLETS; i++ ) {
    if( !bullets[i].alive ) {
        bullets[i].alive = true;
        bullets[i].row = playership.row;
        bullets[i].col = playership.col+5;
        break;
    }
}

然后,当您更新或绘制时:

for( int i = 0; i < MAX_BULLETS; i++ ) {
    if( bullets[i].alive ) {
        attrset(COLOR_PAIR(2));
        mvprintw(bullets[i].row, bullets[i].col, "%c", bullet);
        mvprintw(bullets[i].row+1, bullets[i].col, " " );
        bullets[i].col++;
        // TODO check for bullet death.  If bullet is done, set `alive` to false.
    }
}
refresh();