我正在用 SFML 做一个游戏项目,遇到了一个小问题,
这是我的代码:
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
using namespace std;
using namespace sf;
int main() {
sf::RenderWindow Window;
Window.create(sf::VideoMode(490, 485), "My First Sfml Game");
sf::Texture pTexture1;
sf::Sprite playerImage;
if (!pTexture1.loadFromFile("Data/dots.png"))
std::cout << "error could not load player image" << std::endl;
playerImage.setTexture(pTexture1);
sf::Texture pTexture2;
sf::Sprite lineImage;
if (!pTexture2.loadFromFile("Data/line.png"))
std::cout << "error could not load player image" << std::endl;
lineImage.setTexture(pTexture2);
int v;
while (Window.isOpen()) {
sf::Event event;
while (Window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
Window.close();
break;
case Event::KeyPressed:
if (event.key.code == Keyboard::Right)
lineImage.move(5, 0);
else if (event.key.code == Keyboard::Left)
lineImage.move(-5, 0);
else if (event.key.code == Keyboard::Up)
lineImage.move(0, -5);
else if (event.key.code == Keyboard::Down)
lineImage.move(0, 5);
else if (event.key.code == Keyboard::Space)
lineImage.setRotation(90);
break;
case Event::KeyReleased:
if (event.key.code == Keyboard::Right)
lineImage.move(0, 0);
else if (event.key.code == Keyboard::Left)
lineImage.move(-0, 0);
else if (event.key.code == Keyboard::Up)
lineImage.move(0, -0);
else if (event.key.code == Keyboard::Down)
lineImage.move(0, 0);
else if (event.key.code == Keyboard::Space)
lineImage.setRotation(90);
break;
}
if (event.type == Event::KeyReleased && event.key.code == Keyboard::Space)
lineImage.setRotation(180);
}
Window.draw(playerImage);
Window.draw(lineImage);
Window.display();
}
return 0;
}
此处代码中的图像:postimg.org/gallery/71ufmoi/da508bc3/
问题是当我单击空格键时我需要旋转lineImage
但是当我单击空格时它会旋转 90 然后再返回 90!当我单击空格键时,我需要使它稳定旋转 90,然后当我再次单击空格时,它会旋转 90 到原来的位置!我遇到的另一个问题是我在做点游戏,我怎样才能使线的逻辑在我按下回车键时让它在两点之间?