重力使用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;
}
相关文章:
- SFML纹理像播放器
- 如何使用AngelScript注册SFML Vector2运算符
- 如何修复sfml c++代码编译错误
- SFML library: http request
- 落砂模拟碰撞检测C++和SFML
- 使用 SFML 和 C++ 将 Pixel 打印到屏幕上
- SFML RenderWindow打开窗口需要很长时间
- 矢量下标超出SFML游戏中的范围
- 获取 SFML 窗口的 HWND 和高可用性?
- 将 Vulkan 与 SFML 结合使用?
- SFML 文本未绘制在窗口上
- SFML 碰撞永远不会在我的系统中注册
- 使用对象数组对 SFML 进行动画处理
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 1d.exe找不到SFML文件?
- 当我尝试通过 mingw 使用 CMake 和 SFML 库编译项目时出错
- Declaration of IntRect SFML
- 重力使用CircleShape SFML