O'Reilly logo

SFML Game Development By Example by Raimondas Pupius

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The snake class

Before designing any object, one must ask oneself what it needs. In our case, the snake needs to have a direction to move towards. It also needs to have lives, keep track of the score, its speed, whether it lost or not, and whether it lost or not. Lastly, we're going to store a rectangle shape that will represent every segment of the snake. When all these are addressed, the header of the snake class would look something like the following:

class Snake{ public: Snake(int l_blockSize); ~Snake(); // Helper methods. void SetDirection(Direction l_dir); Direction GetDirection(); int GetSpeed(); sf::Vector2i GetPosition(); int GetLives(); int GetScore(); void IncreaseScore(); bool HasLost(); void Lose(); // Handle losing here. void ToggleLost(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required