This article describes step by step how to create a PUZZLE GAME. This type of game involves an interesting logic to train our knowledge in C++ and Game Development.
We are going to use SFML to create this game, but you can adapt the logic to any other library or game engine.
01. START THE PROJECT
For this game we will use this image and also a Makefile to compile our project.
02. CREATING CONTENT FOR ALL CREATED FILES
Rendering the image as a single sprite just to start the project.
Building and run for first test:
03. SPLIT THE IMAGE INTO 16 SPRITES
sf::Sprite member in the Puzzle class with the
array of sprites in
And now in the
puzzle.cpp file instead of just defining the texture we will create multiple sprites according to the region:
And in the member function
void Puzzle::draw() let’s also replace the line that draws the sprite with the corresponding nested loop below:
Build and run a second time to see if everything is ok:
04. LISTEN TO THE MOUSE CLICK AND MOVING CHANGING POSITIONS
See the output in the terminal.
puzzle.hpp add this member function as protected
puzzle.cpp add the execution,
Build and run a third time to see if everything is ok:
05. SHUFFLEING THE NUMBERS AT THE START OF THE GAME
For this we will use std::experimental::randint, if on Windows use
<random> or any other corresponding.
Change the constructor signature in
Add argument to constructor execution in
Still in the constructor, change the
grid line to the content below:
main.cpp file, add the header for the randint and add the code below to generate 16 non-repeating random numbers and pass the parameter to the object class:
Compile, run and enjoy!
If you want to see all the steps mentioned here on video, watch the video below.
Remembering that the video is in Portuguese, however, you can use Youtube’s automatic translation or watch without translation, as the code is universal!
Code on GitHub
Don’t forget to leave a ⭐ there!