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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.