📜 ⬆️ ⬇️

Create a game on SFML

Greetings to all igrodelov and them sympathizers. In this article I want to talk about such a framework as SFML, and try to write a simple game on it (in our case it will be a clone of the legendary Pong).

v

Download and install


You can download the framework from the official site . At the moment there are two versions available for download - 1.6 and 2.0. I used version 2.0 for Visual C ++ 2010. You can choose the SFML variant that is required for your OS and compiler.

Since the purpose of the article is not to discuss the connection features of libraries in different IDEs, the installation guide can be read at the office. site . I will try to lead the story without being tied to any OS and compiler.
')

Create the base


From the libraries we will need only sfml-graphics, sfml-window, sfml-system (you can connect sfml-main for the cross-platform main function). We begin to create the skeleton of the future game.

#include <SFML/Graphics.hpp> int main() { //     sf::RenderWindow window(sf::VideoMode(800, 600), "Pong"); //    while(window.isOpen()) { //     sf::Event event; while(window.pollEvent(event)) { //           Escape if(event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)) window.close(); } //  window.clear(); //         //  window.display(); } return 0; } 


So what did we do here? The first line included the header file Graphics.hpp , and then we created the main () function and a 800X600 window with the “Pong” header.

SFML offers convenient tools for working with events. We use them to create the application's work cycle and handle window events. With the help of a cycle, we implement closing the window by pressing Escape. In general, sf :: Event provides the ability to work not only with the keyboard, but also, for example, with the mouse. In addition, you can work with some system events, and do not rewrite the code for each new platform. So, if you want to use the joystick in the game, then you will not have to rewrite half of the code - it will be enough to add a couple of lines to the processing loop.

Next we clear the window and draw the contents of the buffer. Everything, the frame of the game is ready, you can compile and check the work. A black window should appear in front of you.

Racket


In Pong there are only two types of objects - a racket and a ball. We will deal with the ball in the next article, and we will create the racket class now. But first we need to add a few lines to main () .

 //    (   ) sf::Clock clock; float time; //    (  ) window.setVerticalSyncEnabled(true); /*     window.clear().           */ time = clock.getElapsedTime().asMilliseconds(); clock.restart(); 


Now let's start creating the Paddle class. I gave here only the basic methods, everything else you can see in the source (download link at the end of the article).

 class Paddle { sf::RenderWindow* window; //   ,    sf::RectangleShape rectangle; // ,   int y; //  y  (x    ) int player; //   (1  2) int score; //     public: //  Paddle(sf::RenderWindow* window, int player) { this->score = 0; this->y = 300; this->player = player; this->window = window; //       Vector2f this->rectangle.setSize(sf::Vector2f(10, 100)); //            this->rectangle.setOrigin(5, 50); //       if(this->player == 1) { this->rectangle.setPosition(25, this->y); } else { this->rectangle.setPosition(775, this->y); } } //     void draw() { this->window->draw(this->rectangle); } /*          ,  --  W  S */ void update(float time) { if(this->player == 1) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { //         this->y -= time * 0.3; this->setY(this->y); // setY -    Y } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { this->y += time * 0.3; this->setY(this->y); } } else { if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { this->setY(this->y - time * 0.3); } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { this->setY(this->y + time * 0.3); } } } 


Racket will serve us a rectangle of size 10X100. The size we set using the Vector2f class is quite an ordinary vector, in the future it will come in handy for us to set the direction of the ball.

In addition to the event system, SFML has another way to handle input devices. That is what we use to control the rackets from the keyboard. You can also transfer player control to the mouse - for this, respectively, sf :: Mouse (or sf :: Joystick ) will be used.

In the source code I recommend to pay attention to the setY method - it checks the coordinates of the racket and does not allow it to go beyond the field.

Now you can create a pair of Paddle objects and call the draw and update methods after clearing the window. If you compile the source code, you will see two racquets on opposite sides of the window, the first is controlled with the cursor keys “up”, “down”, the second - with the keys W and S.

Conclusion


For now we’ll stop at this, and in the next article I’ll tell you how to create a ball class and a points system.
Sources

Source: https://habr.com/ru/post/149071/


All Articles