我正在制作一个飞扬的小鸟游戏,只是想了解如何使用SFML 2.4和C ++进行游戏。我有一个计分系统,每当小鸟精灵与不可见的管道相交时,该系统便会将分数增加1。但是,与其将分数增加1,不如将分数提高到57和60。真正实现此想法的任何想法都值得赞赏。
int main()
{
int score = 0;
float PipeInvisi = 200.0;
while(window.isOpen())
{
if (state == State::PLAYING)
{
// Setup the Invisible Pipe for Movement
if (!PipeInvisbleActive)
{
// How fast is the Pipe
spriteInvisi.setPosition(905, 0);
PipeInvisbleActive = true;
}
else
{
spriteInvisi.setPosition(spriteInvisi.getPosition().x - (PipeInvisi * dt.asSeconds()), spriteInvisi.getPosition().y);
// Has the pipe reached the right hand edge of the screen?
if (spriteInvisi.getPosition().x < -165)
{
// Set it up ready to be a whole new cloud next frame
PipeInvisbleActive = false;
}
}
// Has the Bird hit the invisible pipe
Rect<float> Birdie = spriteBird.getGlobalBounds();
Rect<float> Paipu5 = spriteInvisi.getGlobalBounds();
if (Birdie.intersects(Paipu5))
{
// Update the score text
score++;
std::stringstream ss;
ss << "Score = " << score;
scoreText.setString(ss.str());
clock.restart();
}
}
}
}
答案 0 :(得分:1)
假设您的问题源于恒定的交点,则可以引入一个简单的标记来标出交点。
bool isBirdIntersectingPipe = false;
然后在游戏循环中,您可以像这样检测交点的开始。
if (birdRect.intersects(pipeRect)) // Intersection this frame.
{
if (!isBirdIntersectingPipe) // No intersection last frame, so this is the beginning.
{
++score;
isBirdIntersectingPipe = true;
}
// Still intersecting, so do nothing.
}
else // No intersection this frame.
{
isBirdIntersectingPipe = false;
}
理想情况下,您将拥有一个专门的碰撞系统或物理系统来跟踪场景中的所有对象,但是在这种情况下,这样的简单解决方案就足够了。