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

Implementing the snake structure

Let's now create the two files we'll be working with: Snake.h and Snake.cpp. Prior to actually developing the snake class, a definition of some data types and structures is in order. We can begin by actually defining the structure that our apple eating serpent will be made out of, right in the snake header file:

struct SnakeSegment{
    SnakeSegment(int x, int y) : position(x,y){}
    sf::Vector2i position;
};

As you can tell, it's a very simple structure that contains a single member, which is an integer vector representing the position of the segment on the grid. The constructor here is utilized to set the position of the segment through an initializer list.

Tip

Before moving past this point, make sure you're competent with ...

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