1

我正在使用 SFML 用 C++ 制作小行星游戏。我似乎有射击子弹的问题。尽管每次射击子弹时课程似乎都有效,但游戏速度明显减慢。这是宇宙飞船和子弹的代码。我似乎无法找到它有什么问题!感谢您的时间。

这是船的代码:

#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);
}

编辑:更改了代码,使其在 Spaceship 类中加载图像,并在创建后将其传递给 Bullet 的资源。问题仍然是一样的。每次射击子弹时,游戏都会变得越来越慢,并且会一直保持缓慢,直到被清除为止。

4

2 回答 2

3

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
}
于 2013-04-24T13:08:56.587 回答
1

每次你调用一个新的 Bullet

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

您还调用LoadResources(x,y,v,r)which 调用

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

并且该调用从磁盘读取文件,这是一个非常慢的操作,比其他任何操作都慢一个数量级,因此您的程序在加载期间停止。

于 2013-04-24T13:11:16.830 回答