How not to let the hero go on the window?

Hello,I am writing a small game in sfml
And faced with a question online the answer is not found
There's the location:
5a73203ec7214622841252.jpeg
How to make a character not able to go further than the end of the game?
Thank you in advance!
June 8th 19 at 16:56
1 answer
June 8th 19 at 16:58
Solution
When driving to check the player's coordinates and if they fall outside the bounds of the window - do not change these coordinates and not play the animation of walking, as if motion did not exist.
// Including libraries
#include<sfml\graphics.hpp> // SFML graphics
#include<sfml\opengl.hpp>
#include<windows.h>// Windows library
#include <cstdlib>// rand(), srand()
#include<ctime>// Time


using namespace sf;


int speed = 5;
int width = 800;
int height = 600; 
int currentFrame = 0;
HeroDemensionX int = 50;
int HeroDirection;
int HeroDementionY = height - 150;
int coinCord;
int RandW = rand() % width - 15;
int RandH = rand() % height - 15;




using namespace sf;


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { // Hide the console
 sf::RenderWindow window(sf::VideoMode(width, height), "Game, get the monet"); // Create window
 window.setFramerateLimit(60); // K-proof FPS




 sf::Image Image; // Personaj
 sf::Image coinI; // Moneta
 sf::Texture texture; // Personaj
 sf::Texture coinT; // Moneta
 sf::Sprite sprite; // Personaj
 sf::Sprite coinS; // Moneta

 sf::Clock clock; // clock

 image.loadFromFile("images/image.png");//load the picture
texture.loadFromImage(image);
sprite.setTexture(texture);
 sprite.setPosition(HeroDemensionX, HeroDementionY);
 sprite.setTextureRect(IntRect(0, 0, 64, 96));

coinI.loadFromFile("images/coin.png");
coinT.loadFromImage(coinI);
coinS.setTexture(coinT);
 coinS.setPosition(RandW, RandH);
 coinS.setTextureRect(IntRect(0, 0, 43 , 43 ));




 // Leave the window open
 while (window.isOpen())
{
 int time = clock.getElapsedTime().asMicroseconds();
clock.restart();
 time /= 800;

 sf::Event event;
 while (window.pollEvent(event))
{
 if (event.type == sf::Event::Closed)
window.close();
}

 sf::FloatRect spriteBounds = sprite.getGlobalBounds();

 if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
 HeroDirection = 0;
 currentFrame += 0.005*time;
 if (currentFrame > 4) currentFrame -= 4;
 sprite.setTextureRect(sf::IntRect(int(currentFrame)*64, 96, 64, 96));
 sprite.move(-speed, 0);

}

 if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
 HeroDirection = 1;
 currentFrame += 0.005*time;
 if (currentFrame > 4) currentFrame -= 4;
 sprite.setTextureRect(sf::IntRect(int(currentFrame) * 64, 192, 64, 96));
 sprite.move(speed, 0);

}
 if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
 HeroDirection = 2;
 currentFrame += 0.005*time;
 if (currentFrame > 4) currentFrame -= 4;
 sprite.setTextureRect(sf::IntRect(int(currentFrame) * 64, 288, 64, 96));
 sprite.move(0, -speed);
}
 if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
 HeroDirection = 3;
 currentFrame += 0.005*time;
 if (currentFrame > 4) currentFrame -= 4;
 sprite.setTextureRect(sf::IntRect(int(currentFrame) * 64, 0, 64, 96));
 sprite.move(0, speed);
}

window.clear(sf::Color::Black);
window.draw(coinS);
window.draw(sprite);

window.display();
}
}</ctime></cstdlib></windows.h></sfml\opengl.hpp></sfml\graphics.hpp>

Mmm..Can tell you where exactly to change?
Here:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down))

And cancel the animation as? Usingreturn;? - Nestor_Grady commented on June 8th 19 at 17:01
Yes, after
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
to check the coordinates. In the other 4 similar blocks, too.
And return from the block is not done, it will not come out from the condition, and all of the functions that in the end will not work all what is after (render window, in this case). - Tod commented on June 8th 19 at 17:04
Trying but not out,how would You have implemented the code,you can show
Still not very well know sfml, and C++ too - Nestor_Grady commented on June 8th 19 at 17:07
I would not be all molded into one big function, and would make the player a separate class where stored coordinates, conducted examinations and handled motion - Tod commented on June 8th 19 at 17:10

Find more questions by tags Development of gamesC++