I'm doing a game project with SFML and I encountered a small problem,
Here is my code:
#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;
}
Images in the code here : postimg.org/gallery/71ufmoi/da508bc3/
the problem is that I need to rotate the lineImage
when I click the spacebar but when I click space it rotate 90 then back again 90! I need to make it stable when I click spacebar it rotate 90 then when I click space again it rotate 90 to the original! Another problem I have is I'm doing dots game so, how can I make the logic of the line so I can let it between two points when I hit the enter key?