FriconiX
Free collection of beautiful vector icons for your web pages.

SFML - Part 4 - Moving a sprite with the keyboard

This article is part of a tutorial dedicated to SFML. The reader may consult the other parts of the tutorial by following this link : SFML tutorial.

Demo of SFML (Simple and Fast Multimedia Library) programs with sound, shaders, sprite and text. ## Introduction This post presents how to move a sprite with the keyboard in fullscreen mode with SFML. Example presented on this page has been prepared with the following software versions: * XUbuntu 16.04 LTS * Qt Creator 3.5.1 * SFML 2.3.2 The following video is a preview of the result:
## Example and source code The following program create a fullscreen mode window and display the sprite on the screen. The sprite can be moved with the arrows keys of the keyboard. ``` cpp /*! * \file main.cpp * \brief move a sprite with the keyboard (https://www.lucidarme.me/?p=6127) * \author Philippe Lucidarme * \version 1.0 * \date 12/18/2016 */ // SFML libraries #include #include #include // Sprite speed (high values = high speed) #define SPRITE_SPEED 5 int main() { // _____________________ // ::: Create window ::: // Create a window with the same pixel depth as the desktop sf::VideoMode desktopMode = sf::VideoMode::getDesktopMode(); sf::RenderWindow window(sf::VideoMode( desktopMode.width, desktopMode.height, desktopMode.bitsPerPixel), "SFML part 3", sf::Style::Fullscreen); // Enable vertical sync. (vsync) window.setVerticalSyncEnabled (true); // When a key is pressed, sf::Event::KeyPressed will be true only once window.setKeyRepeatEnabled(false); // ____________________ // ::: Load texture ::: // Create texture from PNG file sf::Texture texture; if (!texture.loadFromFile("../../img/sfml-icon-small.png")) { std::cerr << "Error while loading texture" << std::endl; return -1; } // Enable the smooth filter. The texture appears smoother so that pixels are less noticeable. texture.setSmooth(true); // _______________________________________ // ::: Create sprite and apply texture ::: // Create the sprite and apply the texture sf::Sprite sprite; sprite.setTexture(texture); sf::FloatRect spriteSize=sprite.getGlobalBounds(); sprite.setOrigin(spriteSize.width/2.,spriteSize.height/2.); // _________________ // ::: Main loop ::: // Sprite coordinates int x=window.getSize().x/2.; int y=window.getSize().y/2.; // Flags for key pressed bool upFlag=false; bool downFlag=false; bool leftFlag=false; bool rightFlag=false; sf::Clock timer; while (window.isOpen()) { // Process events sf::Event event; while (window.pollEvent(event)) { // Close the window if a key is pressed or if requested if (event.type == sf::Event::Closed) window.close(); // If a key is pressed if (event.type == sf::Event::KeyPressed) { switch (event.key.code) { // If escape is pressed, close the application case sf::Keyboard::Escape : window.close(); break; // Process the up, down, left and right keys case sf::Keyboard::Up : upFlag=true; break; case sf::Keyboard::Down: downFlag=true; break; case sf::Keyboard::Left: leftFlag=true; break; case sf::Keyboard::Right: rightFlag=true; break; default : break; } } // If a key is released if (event.type == sf::Event::KeyReleased) { switch (event.key.code) { // Process the up, down, left and right keys case sf::Keyboard::Up : upFlag=false; break; case sf::Keyboard::Down: downFlag=false; break; case sf::Keyboard::Left: leftFlag=false; break; case sf::Keyboard::Right: rightFlag=false; break; default : break; } } } // Update coordinates if (leftFlag) x-=SPRITE_SPEED; if (rightFlag) x+=SPRITE_SPEED; if (upFlag) y-=SPRITE_SPEED; if (downFlag) y+=SPRITE_SPEED; // Check screen boundaries if (x<0) x=0; if (x>(int)window.getSize().x) x=window.getSize().x; if (y<0) y=0; if (y>(int)window.getSize().y) y=window.getSize().y; // Clear the window and apply grey background window.clear( sf::Color(127,127,127)); // Rotate and draw the sprite sprite.setPosition(x,y); sprite.setRotation( timer.getElapsedTime().asSeconds() / M_PI * 90.f ); window.draw(sprite); // Update display and wait for vsync window.display(); } return 0; } ``` ## Download Qt project for this example can be downloaded here: [ Source code on GitLab](https://gitlab.com/philippe.lucidarme/sfml_tutorial/tree/master/part_004) ## See also * [ SFML - Part 1 - Installation and first program with Qt Creator ](/en/sfml/sfml-part-1-installation-and-first-program) * [ SFML - Part 10 - Text and font ](/en/sfml/sfml-part-10-text-and-font) * [ SFML - Part 2 - Get available screen resolutions ](/en/sfml/sfml-part-2-get-available-resolution) * [ SFML - Part 3 - Display a sprite ](/en/sfml/sfml-part-3-display-a-sprite) * [ SFML - Part 5 - Shaders ](/en/sfml/sfml-part-5-shaders) * [ SFML - Part 6 - Sprites and shaders ](/en/sfml/sfml-part-6-sprites-and-shaders) * [ SFML - Part 7 - Heat effect shader ](/en/sfml/sfml-part-7-heat-effect-shader) * [ SFML - Part 8 - Water effect shader ](/en/sfml/sfml-part-8-water-effect-shader) * [ SFML - Part 9 - Sound and music ](/en/sfml/sfml-part-9-sound-and-music) * [ SFML tutorial ](/en/sfml/sfml-tutorial) ___
Last update : 03/15/2021