#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <Windows.h>
usingnamespace std;
int main()
{
sf::RectangleShape rectangle(sf::Vector2f(150, 80));
sf::RenderWindow window(sf::VideoMode(1000, 1000), "Moving a shape");
window.setVerticalSyncEnabled(true);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
return 0;
}
}
window.clear(sf::Color::White);
rectangle.setFillColor(sf::Color(0, 250, 0));
window.clear(sf::Color::Black);
if (event.type == sf::Event::KeyPressed){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
rectangle.move(0, -5);
Sleep(1);
}
}
if (event.type == sf::Event::KeyPressed){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
rectangle.move(0, 5);
Sleep(1);
}
}
if (event.type == sf::Event::KeyPressed){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
rectangle.move(-5, 0);
Sleep(1);
}
}
if (event.type == sf::Event::KeyPressed){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
rectangle.move(5, 0);
Sleep(1);
}
}
window.draw(rectangle);
window.display();
}
}
I've tried inserting it anywhere I can think of. Can this be done without using threads? And can someone help guide me to the correct way to set the background image please?