This is part of a series of small projects and tutorials using C++ and SFML
Library version: SFML 3.0.0
C++ Standard: ISO C++20
My Base Project: https://github.com/l-paz91/SFMLProjects/blob/main/main.cpp
In this project, we'll make a simple app that generates a random string of keys the user needs to press. The user will have 5 seconds to input the keys in that order. Here's what the finished product will look like:
Step 1 - Getting User Input
So we won't add the timing aspect in just yet. We'll start by creating some code to generate keys that the user needs to input. For simplicity, there will be 8 keys the computer can choose from: W A S D, and 8426 (the arrow keys if you have a number pad, if you don't have a number pad, choose keys that make sense to you).
The reason I'm not using the actual arrow keys is because they aren't considered text, they're key events, that's why I'm using the number pad.
First though, lets add a way to display what users type:
Step 2 - Generating Keys To Press
Now, these keys for our "game" will never change (at least for this program, you may want to expand on it). So I'm going to push them back into a vector at the start of the program.
I won't be bothering with capitals in this program, but that's something you can add if you like.
Now we need to create a function that will generate our random combo and a function that will generate random numbers for us, as std::rand() isn't all that random.
Now let's display the combo to the user. I also moved the user input text object down the screen slightly, so the computer's combo is at the top.
With that done, we can move onto adding some logic to see if the combo is correct or not.
Step 3 - Checking For a Correct Combo
We can generate random combo moves and the user can give input, let's add a way to check if the input and the combo move matches and do something with that information.
Let's compare the strings when the user presses enter:
We check for the Key Release event as that is a one time event. Key Presses can happen over multiple frames depending on how fast you are. Because we check for a key release, we need to capture the 'key press' event of the enter key, otherwise it's character will be appended to our stream (which we don't want as that will mess up our combo check). Also, please excuse the magic number here. 13 is the unicode value for enter in SFML.
Step 4 - Adding A Time Limit
So to add some extra spice to our "game". Let's add in a time limit. The user has to correctly enter the combo in say 5 seconds. We'll start by adding a countdown timer to the screen. This is basically our Stopwatch but in reverse.
Now we need to use the clock and delta time to start the countdown.
Don't forget to reset the timer if enter is pressed as well:
For the most part this is a fully functioning program. However our code is starting to get a bit "unmanageable" in the amount of places we need to keep track of what to update in text boxes and reset timers. We should really have win and loss scenarios now handling that in one place so the game is easier to modify and expand.
Exercise
Add a scoring system. Refactor to check for appropriate "win" or "lose" scenarios.