所以我正在学习c ++并决定尝试SDL,这样我就可以习惯使用类了。游戏基本上是一个太空射击游戏,所以我写了一些允许“射击子弹”的代码。代码在我的电脑上工作正常,子弹从X点到Y点没有问题。当我试图运行时在2台不同的计算机上游戏“子弹”非常多(它们会随机消失并且随机而来)。其余的游戏运行正常。我在两台计算机上测试了帧率,fps超过300
有谁知道是什么原因以及如何解决?
如果您出于某种原因需要它,可以使用以下代码(针对项目符号):
//LOAD BULLET IF FREE
if(bulletEnemyCooldown.getTicks() > bulletEnemyCooldownVar)
{
bulletEnemyCooldown.reset();
for (int i = 0; i < kShips; i++)
{
if(!evilShip[i].getIfDead())
{
for(int j=0; j<99; j++)
{
if (evilShip[i].getIfBulletFree(j))
{
evilShip[i].setKBullets(evilShip[i].getkBullets() + 1);
evilShip[i].setRectPos(evilShip[i].getPosX(),evilShip[i].getPosY(),j);
evilShip[i].setIfBulletFree(false,j);
break;
}
}
}
}
}
//CHANGE BULLET POS IF SLOT OCC AND RENDER
for (int i2 = 0; i2 <= kShips; i2++)
{
if(!evilShip[i2].getIfDead())
{
for(int j2 = 0; j2<evilShip[i2].getkBullets(); j2++)
{
if (evilShip[i2].getIfBulletFree(j2) == false)
{
evilShip[i2].setBulletRectY( evilShip[i2].getBulletRectY(j2) + bulletS,j2);
enemy_bullet_texture.render(gRenderer, evilShip[i2].getBulletRectX(j2), evilShip[i2].getBulletRectY(j2));
evilShip[i2].setBulletDis(evilShip[i2].getBulletDis(j2) + bulletS,j2);
}
}
}
}
//CHECK FOR ANY COLLISION AND FREE BULLET IF FOUND
for (int i = 0; i < kShips; i++)
{
for (int j = 0; j < evilShip[i].getkBullets(); j++)
{
if(!evilShip[i].getIfDead())
{
if (checkCollision(evilShip[i].getBulletPosRect(j), mainShip.getmCollider()))
{
evilShip[i].setIfBulletFree(true,j);
evilShip[i].setBulletDis(0,j);
evilShip[i].setKBullets( evilShip[i].getkBullets() - 1);
evilShip[i].setRectPos(0,0,j);
boolMainShipContact = true;
mainShip.curSetHP(mainShip.curGetHP() - evilShip[i].curGetATK());
kShipContact = i;
score -= rand()%(10 * currentLevel);
}
}
// FREE BULLET IF FOUND OFF SCREEN
if (evilShip[i].getBulletDis(j) > SCREEN_HEIGHT)
{
evilShip[i].setIfBulletFree(true,j);
evilShip[i].setBulletDis(0,j);
evilShip[i].setKBullets(evilShip[i].getkBullets() - 1);
}
}
}