gpt4 book ai didi

c++ - 运动不工作 ncurses 游戏

转载 作者:行者123 更新时间:2023-11-30 17:44:52 26 4
gpt4 key购买 nike

我正在制作一个 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();
}

我尝试实现 this answer to my earlier question 中评论中的建议,但我认为我做得不对:

If you can have 5 bullets at once, you need to store their positions. If you have int bullet_pos[5] that would be fine. You could use -1 in each position to say that no bullets are active. Then when you want to fire one you search the array to find the first position that is -1 and change it to 0. When you draw the bullets, you go through the array and draw a bullet for any position that is not -1, and update its position.

最佳答案

如果您还没有,请尝试在您的项目符号结构中添加一个标志。类似于活着

当您想要开火时,您可以检查数组并找到未使用的子弹位置(如果有):

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com