SFML游戏减速射击子弹
SFML Game Slows Down When Shooting Bullets
我正在用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")
和那个调用从磁盘读取一个文件,这是一个非常慢的操作,比其他任何操作都要慢,所以您的程序在加载期间会停止。
相关文章:
- 向有射程的敌人发射子弹
- SFML 子弹未生成
- 射击时从列表中删除外星人 - 找出循环;"表达式:无法递增结束列表迭代器"
- 子弹 - 刚体初始旋转/平移
- 矢量迭代器在横向卷轴射击游戏中不可取消引用
- 在这个1D C++游戏中射出多颗子弹
- 在尝试执行触摸和保持(子弹发射)时卡住了?-椰子2d-x
- 子弹物理 "setLinearVelocity"不动
- 如何在OpenCV中由鱼眼产生的射线进行射击
- Short Int基掩码在C 库子弹中的工作方式
- 如何在子弹物理学中让刚体的欧拉旋转在 0 到 360 之间
- 太空射击游戏中的3D音效
- 虚幻引擎射击游戏示例中'class DataType* Variable' 'class'的内容
- 子弹物理刚体从表面上反弹
- 子弹物理-定向刚体
- 第 4 个射击语句在 SFML 游戏中不起作用
- 如何绘制/渲染子弹物理碰撞体/形状
- 子弹物理:刚体碰撞启用,物理禁用
- 我如何使射击多个子弹功能正确使用for循环
- SFML游戏减速射击子弹