SFML游戏减速射击子弹

SFML Game Slows Down When Shooting Bullets

本文关键字:射击 子弹 游戏 SFML      更新时间:2023-10-16

我正在用c++使用SFML制作一个小行星游戏。我似乎对射击有问题。虽然这个类似乎在每次射击时都有效,但游戏速度明显减慢。这是飞船和子弹的密码。我就是找不出它有什么毛病!谢谢你的宝贵时间。

这是船舶规则:

#include "Spaceship.h"
Spaceship::Spaceship(void){}
Spaceship::~Spaceship(void){}
void Spaceship::LoadResources()
{
    if(!shipAnimImg.LoadFromFile("Resources/Images/SpaceshipAnimation.png"))
        std::cout <<"Could not locate the ship animation image" <<std::endl;
    if(!shipIdleImg.LoadFromFile("Resources/Images/SpaceshipIdle.png"))
        std::cout <<"Could not locate the ship idle image" <<std::endl;
    if(!bulletImg.LoadFromFile("Resources/Images/Bullet.png"))
        std::cout <<"Could not locate the bullet image" <<std::endl;
    shipSpr.SetImage(shipIdleImg);
    shipSpr.SetScale(0.5,0.5);
    shipSpr.SetCenter(shipIdleImg.GetWidth() / 2,shipIdleImg.GetHeight() / 2);
    x = DEFAULT_SCREENWIDTH / 2; 
    y = DEFAULT_SCREENHEIGHT / 2;
    shipSpr.SetPosition(x,y);
    shipSpr.SetRotation(90);
    std::cout<<shipSpr.GetCenter().x<<std::endl;
    std::cout<<shipSpr.GetCenter().y<<std::endl;
    vx = 0.2;
    vy = 0.2;
    isBulletOnScreen = false;
    isPressed = false;
}
void Spaceship::UnloadResources(){}
void Spaceship::Update(sf::RenderWindow &Window,sf::Event event)
{
    if (Window.GetInput().IsKeyDown(sf::Key::A))
    {
        shipSpr.Rotate(0.08);
    }
    if (Window.GetInput().IsKeyDown(sf::Key::D))
    {
        shipSpr.Rotate(-0.08);
    }
    if (Window.GetInput().IsKeyDown(sf::Key::W))
    {
        x += (cos(shipSpr.GetRotation() * (3.14159265/180.0)) *0.2);
        y -= (sin(shipSpr.GetRotation() * (3.14159265/180.0)) *0.2);
        shipSpr.SetPosition(x,y);
    }

    if (Window.GetInput().IsKeyDown(sf::Key::Space) && !isPressed)
    {   
    isBulletOnScreen = true;
    isPressed  = true;
    bullets.push_back(new Bullet(shipSpr.GetPosition().x,shipSpr.GetPosition().y,0.3,shipSpr.GetRotation(),bulletImg));
    }
    if (event.Type == sf::Event::KeyReleased)
    {
        isPressed  = false;
    }
    if(bullets.size() != 0)
    {
        for (int i=0; i<bullets.size(); i++)
        {
            bullets[i]->Update(Window,event);
            if ((bullets[i]->GetX() > DEFAULT_SCREENWIDTH + 40) || (bullets[i]->GetX() < 0 - 40) ||
                (bullets[i]->GetY() > DEFAULT_SCREENWIDTH + 40) || (bullets[i]->GetY() < 0 - 40))
                {
                    bullets.erase(bullets.begin() +i);
                }
        }
        std::cout<<bullets.size()<<std::endl;
    }
    std::cout<<bullets.size()<<std::endl;
}
void Spaceship::Draw(sf::RenderWindow &Window)
{
    if(isBulletOnScreen)
    for (int i=0; i<bullets.size(); i++)
    {
        Bullet *cur = bullets[i];
        bullets[i]->Draw(Window);
        std::cout<<bullets.size()<<std::endl;
    }
    Window.Draw(shipSpr);
}

这是为子弹设置的:

#include "Bullet.h"
Bullet::Bullet(void){}
Bullet::Bullet(float x,float y,float v,float r,sf::Image image)
{
    LoadResources(x,y,v,r,image);
}
Bullet::~Bullet(void){}
void Bullet::LoadResources(float x,float y,float v,float r , sf::Image image)
{
    this->x = x;
    this->y = y;
    this->v = v;
    bulletImg = image;
    bulletSpr.SetImage(bulletImg);
    bulletSpr.SetScale(0.5,0.5);
    bulletSpr.SetCenter(bulletImg.GetWidth() / 2,bulletImg.GetHeight() / 2);
    bulletSpr.SetPosition(x,y);
    bulletSpr.SetRotation(r);
}
void Bullet::UnloadResources(){}
void Bullet::Update(sf::RenderWindow &Window,sf::Event event)
{
    x += (cos(bulletSpr.GetRotation() * (3.14159265/180.0)) *v);
    y -= (sin(bulletSpr.GetRotation() * (3.14159265/180.0)) *v);
    bulletSpr.SetPosition(x,y);
}
void Bullet::SetX(float x)
{
    this->x = x;
}
void Bullet::SetY(float y)
{
    this->y = y;
}
void Bullet::SetRotation(float r)
{
    rotation = r;
}
float Bullet::GetX()
{
    return x;
}
float Bullet::GetY()
{
    return y;
}
void Bullet::Draw(sf::RenderWindow &Window)
{
    Window.Draw(bulletSpr);
}

编辑:修改了代码,使其在太空船类中加载图像,并在创建后将其传递给子弹的资源。但问题依然存在。游戏变得越来越慢,每次子弹被击中,它一直很慢,直到它被擦除。

1.每次创建一个新对象(通常,如果您喜欢拍摄)时,都要从磁盘加载Bullet PNG图像。从磁盘加载可能会非常慢。尝试多次重复使用相同的图像!

你可以将LoadFromFile功能从LoadResources中抽出,并将其放在游戏持续时间内的某个地方。然后只要让LoadResources在需要创建新子弹时指向那个地方。这同样适用于任何其他图像或资源,可以在你的游戏中重用。

2。我还看到你的代码中有std::cout。尝试删除所有这些在渲染循环中,因为打印很慢。

for (int i=0; i<bullets.size(); i++)
{
    Bullet *cur = bullets[i];
    bullets[i]->Draw(Window);
    std::cout<<bullets.size()<<std::endl; // This is going to be slow
}

3。您可能还想看看bullets向量。当向它添加子弹时,push_back会改变向量的大小,这每次都需要一些内存的分配和释放。更好的方法是为子弹的最大数量留出空间(例如使用resize函数),以便矢量在创建子弹时不会改变大小。

if (Window.GetInput().IsKeyDown(sf::Key::Space) && !isPressed)
{   
    isBulletOnScreen = true;
    isPressed  = true;
    bullets.push_back(new Bullet(...); // avoid this
}

每次调用new Bullet

Bullet::Bullet(float x,float y,float v,float r)
{
    LoadResources(x,y,v,r);
}

还调用LoadResources(x,y,v,r),它调用

bulletImg.LoadFromFile("Resources/Images/Bullet.png")

和那个调用从磁盘读取一个文件,这是一个非常慢的操作,比其他任何操作都要慢,所以您的程序在加载期间会停止。