F.L.A.M.E.S. is an interesting game that takes two names as inputs from the user and outputs their relationship. The relationship could be one of the following: friends, lovers, affectionate, married, enemies, or siblings. These relationship possibilities lead to the acronym F.L.A.M.E.S. The F.L.A.M.E.S. game works on a very simple letter-elimination logic. The unique letters from the names of the participants are used to iteratively eliminate letters from the word FLAMES. After each elimination, the process continues until only one letter is left in FLAMES, which is then outputted to the user as the final relationship.
Python Tkinter Tutorial: Create a F.L.A.M.E.S. game from scratch
This article is part of a Python Tkinter series written by Pratik Shukla. And stay tuned for more Tkinker articles from Pratik!
This series aims to further your knowledge of Tkinter, Python’s standard interface to the Tk GUI toolkit by building interactive games you can add to your resume. To get up to speed on Tkinter, go read our first article in this series Python Tkinter Tutorial: build a number guessing game. Once you get down the basics, come back here to build a F.L.A.M.E.S. from scratch!
Today, we will cover:
- F.L.A.M.E.S. game walkthrough
- Game implementation in Python
- Putting it all together
- Wrapping up and resources
F.L.A.M.E.S. game walkthrough#
In this article, we will develop the famous childhood game called F.L.A.M.E.S. In this relationship calculator, the user will enter two names, and as an output our program will tell us that whether they are:
- Friends
- Lovers
- Affectionate
- Married
- Enemies
- Siblings
Let’s develop the same game using Python 3 and Tkinter!
Rules and how to play#
- Run the program.
- Enter the names.
- Press the button.
- See the result.
- Play again.
Walkthrough of the program#
When the user runs the program, the user will enter two names for which they wants to find the relationship. After the user enters the details, our program will calculate the relationship status based on an algorithm. Let’s try to understand it with an example. First of all, our algorithm will remove all the common characters between the entered names.
For example, take the inputs:
CHANDLER
MONICA
The output will be:
HDLERMOI
Now our program will calculate the total number of letters in that final output list to determine how to iterate through the word FLAME for a result. In our above example, we have 8 letters in our final list, so we will go through the letters in FLAMES and cross out every 8th character iteratively. Let’s check it visually.
Here notice that the final result is “A” (Affectionate). Our program will also play different types of sound files on various events.
Final result#
Game implementation in Python (13 steps)#
Now that we have a sense of what the F.L.A.M.E.S game will entail, let’s walk step-by-step through the process in Python. If you don’t yet know how to set up Tkinter, check out our first article in this series for a brief tutorial.
1. Import required libraries#
tkinter: To add widgets in our application.simpleaudio: To play the sound files.
2. Main tkinter window#
root = Tk( ): This is used to initialize our tkinter module.root.title( ): We use this to set the title for our application.root.geometry( ): We use this to specify at which location our application window will open. In our case, it will open at 100 points to the left and 0 points to the down from the top-left corner.root.configure( ): We use this to specify the background color for our application. In our case, the background color will be black.root.resizable( ): We use this prevent the user from resizing our main window.root.iconphoto( ): We use this to set the icon in the title bar for our application window.
3. Load the sound files#
We will use some sound files that will play at various events. When our program starts, it will play start file. We’re going to play different files when we have different results for our game. One thing to notice is that Tkinter only accepts .wav files. First, we need to load the sound file in an object. Then, we can play it using .play( ) method when required.
4. Take input names#
Here we are taking input names from user. The user has to enter 2 names. After the user enters the name, our program will transform the names into upper case. Our program will also replace space with empty string. We have to do this if the user enters their first name and last name separated by space. Then, we’re going to convert it into list so that we can apply remove_comman_character( ) function on it.
5. Remove common characters#
Here we are going to remove the common characters from our two names like we did in the walkthrough. We will use an interactive method for this.
Notice that when there are common characters in our string, it will return the both the name strings separated by # with a True flag. When there are no common characters in our strings, it will return both the name strings separated by # with a False flag. We’ll use this flag value later in our code to calculate the relationship.
6. Calling the function#
First, we’re setting the proceed flag as True so that our remove_common_char function can go work. After that we’re going to remove common characters until the proceed flag isn’t set to False. When there are no common characters our program will set the flag as false.
Then we are finding the index of # by which we joined our two names after removing common characters. name1_list contains name1 characters, and name2_list stores name2 characters. Finally, we are storing the total length of both names after removing common characters in the variable called count.
From the example of the walkthrough, we get:
HDLER
MOI
- Name 1:
Length of name_1 = 5 - Name 2:
Length of name_2 = 3 - Total length: 5 + 3 = 8
7. Calculating the relationship#
Let’s understand this with example. Our program will run until there is only one element left in result. Click through to see the iterations.
8. Load game images#
Here we are loading a few images which we are going to use in our application. We are going to display the result label in form of images.
9. Generate list of names and load images for letters in names#
Here we are generating lists of names that we’ll use to fetch the images for each letters in name. We are also finding the length of name that’ll be useful while running the for loop.
Then, we want to display each letters in name, we first need to load it in our application. Notice that our image files for alphabets are stored like:
A.pngB.pngC.png
Since we already have a list of letters we are going to need, we can easily run a for loop to fetch the letters.
10. Display the names#
Here we are displaying the names in form of image labels. Our first name will be displayed in the 0th row and then there will be a blank space on the 1st row. Our second name will be displayed on the 2nd row. After that, we’re adding a blank space on the 3rd row.
11. Add button and result label#
Since we have displayed the names, we are creating a button and a label. When the user will press the button it will trigger the Reveal( ) function. Our reveal function will display the relationship between two entered names in form of image.
12. Main function to display the result#
This is the main function, that will change the image of our label based on the relationship result. Our result variable is storing the value of the relationship result calculated. The rest of the code is just if-else statements which is self explanatory.
13. Enter the mainloop#
We have to enter the main loop to run the program. If our program doesn’t have this line then it will not work. Our program will remain in the main loop until we press the close button.
Wrapping up and resources#
Congrats! You’ve reached the end and built a successful Python Tkinter game. Great work applying your Python knowledge. I hope you enjoyed this article and learned something new about algorithms from it.
Python is such an intuitive programming language that the easiest way to master it is through hands-on practice like this. Keep up the work and keep building!
This is the final article in my Tkinter series. I hope you enjoyed the series. Continue using Python and build your resume with fun games like these!
If you have any doubts, questions, or thoughts regarding this article, feel free to contact me at shuklapratik22@gmail.com
Continue learning#
Frequently Asked Questions
What is the F.L.A.M.E.S. game in Python?
What is the F.L.A.M.E.S. game in Python?