SFML Bullet 没有生成
SFML Bullet not spawning
所以我正在使用 SFML 和 C++ 创建一个简单的 space 游戏。对于无法让子弹产卵的生活。这是我的来源...我只是想学习如何在游戏中生成新精灵。
```
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <string>
int main()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");
// Load a sprite to display
sf::Texture texture_sprite;
if (!texture_sprite.loadFromFile("cb.bmp"))
return EXIT_FAILURE;
sf::Sprite sprite(texture_sprite);
sf::Texture texture_background;
if (!texture_background.loadFromFile("space.png"))
return EXIT_FAILURE;
sf::Sprite background(texture_background);
sf::Texture texture_fire;
if (!texture_background.loadFromFile("fire_1.png"))
return EXIT_FAILURE;
sf::Sprite fire_sprite(texture_fire);
fire_sprite.setScale(4.0f, 1.6f);
std::string direction = "";
bool fire = false;
// Start the game loop
while (app.isOpen())
{
// Process events
sf::Event event;
while (app.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
app.close();
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sprite.getPosition().x > -20)
{
sprite.move(-10,0);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Left";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sprite.getPosition().x < 670 )
{
sprite.move(10,0);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Right";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sprite.getPosition().y > 0)
{
sprite.move(0,-10);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Up";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && sprite.getPosition().y < 480 )
{
sprite.move(0,10);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Down";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
fire = true;
}
else if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space)
{
fire = false;
}
if(fire == true)
{
std::cout << "FIRE!!!" << std::endl;
fire_sprite.setPosition((sprite.getPosition()));
std::cout << "Fire positions is " <<fire_sprite.getPosition().x << " " << fire_sprite.getPosition().y << "The direction is " << direction <<std::endl;
}
}
app.clear();
// Draw the sprite
app.draw(background);
app.draw(sprite);
app.draw(fire_sprite);
// Update the window
app.display();
}
return EXIT_SUCCESS;
}
```
sf::Texture texture_fire;
if (!texture_background.loadFromFile("fire_1.png"))
您没有将纹理加载到 texture_fire,而是再次加载到 texture_background。应该是:
if (!texture_fire.loadFromFile("fire_1.png"))
所以我正在使用 SFML 和 C++ 创建一个简单的 space 游戏。对于无法让子弹产卵的生活。这是我的来源...我只是想学习如何在游戏中生成新精灵。
```
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <string>
int main()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");
// Load a sprite to display
sf::Texture texture_sprite;
if (!texture_sprite.loadFromFile("cb.bmp"))
return EXIT_FAILURE;
sf::Sprite sprite(texture_sprite);
sf::Texture texture_background;
if (!texture_background.loadFromFile("space.png"))
return EXIT_FAILURE;
sf::Sprite background(texture_background);
sf::Texture texture_fire;
if (!texture_background.loadFromFile("fire_1.png"))
return EXIT_FAILURE;
sf::Sprite fire_sprite(texture_fire);
fire_sprite.setScale(4.0f, 1.6f);
std::string direction = "";
bool fire = false;
// Start the game loop
while (app.isOpen())
{
// Process events
sf::Event event;
while (app.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
app.close();
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sprite.getPosition().x > -20)
{
sprite.move(-10,0);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Left";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sprite.getPosition().x < 670 )
{
sprite.move(10,0);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Right";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sprite.getPosition().y > 0)
{
sprite.move(0,-10);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Up";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && sprite.getPosition().y < 480 )
{
sprite.move(0,10);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Down";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
fire = true;
}
else if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space)
{
fire = false;
}
if(fire == true)
{
std::cout << "FIRE!!!" << std::endl;
fire_sprite.setPosition((sprite.getPosition()));
std::cout << "Fire positions is " <<fire_sprite.getPosition().x << " " << fire_sprite.getPosition().y << "The direction is " << direction <<std::endl;
}
}
app.clear();
// Draw the sprite
app.draw(background);
app.draw(sprite);
app.draw(fire_sprite);
// Update the window
app.display();
}
return EXIT_SUCCESS;
}
```
sf::Texture texture_fire;
if (!texture_background.loadFromFile("fire_1.png"))
您没有将纹理加载到 texture_fire,而是再次加载到 texture_background。应该是:
if (!texture_fire.loadFromFile("fire_1.png"))