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(); ...

Get SFML Game Development By Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.