Game Development Reference
In-Depth Information
The first thing we have to do is open a suitable window. Secondly, we need to set the
framerate limit to the standard 60 frames per second. The next thing on the list is a
sf::Vector2f variable, which we will use as a spawn point for the player. After we
initialize the square of the player, we initialize the target, a blue square a bit further to the
right in the world. The last shape is a square, which the player has to avoid. It stands
somewhere in the middle.
The updated frame, where all the game logic happens, looks like this:
We can clearly see from the preceding code that the player always moves to the right—the
player has no control over it. You can change that, so that the player has control of all four
directions—out of personal preference. Currently, the only directions in which the player
can move are up and down by using the arrow keys.
Apart from the input handling, we need to check whether the code has the logic for both
win and lose conditions. We need a method to handle collision detection between these
rectangles. Thankfully, all shapes in SFML have two functions called
Shape::getGlobalBounds() and Shape::getLocalBounds() , which return
sf::FloatRect , which represents the global or local bounds of the current shape. The
bounding rectangle of a shape (sometimes called a bounding box ) is the rectangle with
the smallest possible surface area, which contains the whole shape. Global and Local refer
to the transformation of the shape—if the shape is transformed in any way, the position,
scale, and rotation are ignored in the local bounds; whereas in the global bounds, they are
taken into consideration. Once we have the global bounds, we can use a function called
FloatRect::intersect() that takes another FloatRect and returns if the two
Search WWH ::




Custom Search