重力使用CircleShape SFML

Gravity using CircleShape SFML

本文关键字:CircleShape SFML      更新时间:2023-10-16

我正在尝试为一款分屏游戏设置重力。我的游戏的问题在于,当按下W或Up键时,圆圈会上升,但重力似乎并不适用于它们。此外,当我在空中向左或向右移动它们时,它们会消失,直到我再次按W或向上键。

sf::Vector2f position(screenDimensions.x /2, screenDimensions.y / 2);
sf::Vector2f position2(position);
sf::Clock clock;
float moveSpeed = 0.5f , jumpSpeed = 0.3f;
   while(Game.isOpen())
{
    clock.restart();
    sf::Event Event;
    while(Game.pollEvent(Event))
    {
        switch(Event.type)
        {
        case sf::Event::Closed:
            Game.close();
            break;
        case sf::Event::KeyPressed:
            if(Event.key.code == sf::Keyboard::Escape)
                Game.close();
            break;
        }
    }

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
        velocity.x = moveSpeed;
        circ1.move(velocity.x, velocity.y);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            velocity.x = -moveSpeed;
            circ1.move(velocity.x, velocity.y);
        }
    else
        velocity.x = 0;
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            velocity.x = moveSpeed;
            circ2.move(velocity.x, velocity.y);
        }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            velocity.x = -moveSpeed;
            circ2.move(velocity.x, velocity.y);
        }
    else
        velocity.x = 0;
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            velocity.y = -jumpSpeed;
            circ1.move(velocity.x, velocity.y);
        }
    else
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
       {
           velocity.y = -jumpSpeed;
           circ2.move(velocity.x, velocity.y);
       }
    if (circ1.getPosition().x + 10 >= view1.getSize().x / 2)
        position.x = circ1.getPosition().x + 10;
    else
       position.x = view1.getSize().x / 2;
    if (circ2.getPosition().x + 10 >= view2.getSize().x / 2)
        position2.x = circ2.getPosition().x + 10;
    else
       position2.x = view2.getSize().x / 2;
    if (circ1.getPosition().y + circ1.getRadius() < groundHeight || velocity.y > 0)
        {
            velocity.y += Gravity;
        }
    else
        {
            circ1.setPosition(circ1.getPosition().x, groundHeight - circ1.getRadius());
            velocity.y = 0;
        }

    view1.setCenter(position);
    view2.setCenter(position2);
    Game.setView(view1);
    Game.draw(bImage);
    Game.draw(circ1);
    Game.draw(circ2);
    Game.setView(view2);
    Game.draw(bImage);
    Game.draw(circ2);
    Game.draw(circ1);
    Game.display();
    Game.clear();
}

}

感谢您的帮助

如果你想要"跳跃"本身,那么你需要阻止用户在已经在空中时跳跃。这可以通过在开始时创建bool jumping=false;,然后在初始化跳转时将jumping设置为true来实现。然后,每次程序循环时,if (jumping && floor.getGlobalBounds().intersects(circ1) || circ.getPosition().y<0){jumping=false;velocity.y=0;}

否则,您可以尝试更改

 if (circ1.getPosition().y + circ1.getRadius() < groundHeight || velocity.y > 0)
        {
            velocity.y += Gravity;
        }

 if (circ1.getPosition().y + circ1.getRadius() > groundHeight || velocity.y > 0)
        {
            velocity.y += Gravity;
        }