
Overview
This is small project that I created to try modern C++ with some basic design patterns. I wanted it to be a very small and fast project, but the closer to end I was, the more features I wanted to add.
It is the simpliest snake, where we are moving by WSAD/Arrows and colleting fruits to get bigger and bigger. Collision with wall is an obvious game over. We can create room to which other clients can connect and play with us. Game is designed for up to 4 players.
Technical overview
Because this project is about code, not about game, the game itself is not very interesting, but has some technical solutions that I am praud of.
Serialization
First thing is my own serialization system based on generic classes. It consists of Serializable interface that requires overriding Serialize and Deserialize methods. Second element of this system is SerializedData class, that does the trick. It is representation (as bytes) of the serialized object and because it is generic class, we can push (or pop) practically any type of object with use of one method. In result, it looks like defining which members of the class should be serialized.
Windows
Menu in applcation is very simple and the main assumption is that we have window with title and couple of options. Option can be a clickable "button" or just text according to that it has selectable flag set or not. We can also set flag that specifies if element should be small or big. It makes that one simple class represents all possible elements in window.
System uses also simple command pattern to handle selected option. Command is invoked with passed ID of the option that has been choosen.
Input handling
I wanted to make handlng input more abstract. That means, only specific controller can know that we are using keyboard/joystick/whatever. Any other parts of code implement reactions (commands) for specific behavior. In other words, we can imagine that we have controller with only 5 buttons: 4 arrows and confirm button. That is what app sees and different game states (menu, gameplay) sets different reaction for pressing specific button.
Collisions
Handling collisions is everytime challenging for me to design it properly. In this case I used visitor pattern. Game object can inherit ICollidable interface and implement collisions with choosen types of object by overriding methods with specific type of parameter. In general, every object in game is an Actor. Because of that, when we are passing objects (actors) to collision handler, it doen't know what type of object it is (snake, wall, fruit?). That is why it is neccessary to implement general case of collision (not with specific object, but with ICollidable object) to resolve type of objects that are colliding.
