How I Built My First Game With Python

Ikwuje Peace
9 min readNov 23, 2022

--

This is a step to step guide on how I built my first “word game” that selects a random word from a dictionary, shuffles the word, and gives room for a user to try to guess the word. All these were done on a single Python file with less than 30 lines of code and a “txt” file.

What is Python?

Python is a programming language that is very efficient for building large-scale projects with less code compared to other programming languages. It is one of the top most used programming languages today. It is easy to understand and also very easy to use. Python had to be installed on my computer or PC for its code to run on my PC. Python can be installed here.

Installing an IDE

After installing Python, I also needed to have an IDE installed on my computer too. IDE stands for Integrated Development Environment, and it is a software application that allows developers to code efficiently. While there are many efficient IDEs, I preferred PyCharm. This was because PyCharm seemed user-friendly for the beginner that I was when I created the game.

PyCharm automatically creates a virtual environment for you. A virtual environment can be likened to a box, in it, all the libraries and scripts are dropped. All the things dropped in it are secluded from those dropped in other boxes. When a virtual environment is active, only the items dropped in the box will be used, even if the project needs other items, if they aren’t in the box, it will throw an error.

According to Python’s official documentation: “A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python i.e., one which is installed as part of your operating system”.

If you use a different IDE, you might have to create the virtual environment and activate it every time you run your code. For a beginner, this might be frustrating at some point because the different commands needed to both create and activate the virtual environment differs from one Operating system to another. You might run into errors or bugs. For more information on how to create a virtual environment, see.

First, I created a folder in the PyCharm IDE, this folder housed the two files I used for the project. I created a Python file in the folder. A Python file is identified by its extension name “.py. Then I created a “txt” file and named it “words”. I also went online and copied a list of words to test my project with.

In the “word_game.py file” I wrote my code. I started by using the “import random”. This function is to automate the random selection of words, integers, or objects. I used this function to randomly select words that I inputted in the words.txt file. On the next line, I declared a list and named it “dictionary”, it was an empty list as nothing was yet in it. A Python list is denoted by square brackets, items can be added or removed from it too.

In the next line, I started using a Python file operation. There are different Python file operations some of which are open(), close(), read(), writelines(), remove(), flush(), detach(), tell(), next(), etc. Each has a different function but is related to opening, closing, reading, writing lines, removing, flushing, detaching, telling, or moving to the next file as the case may be and how it is used in the context of the code. I used the open() operation to open the “word.txt” file and used ‘r’ to indicate that it is not to write into the file, but only to read from the file. It follows the syntax on line 3 below.

The third line is to open words.txt and read from the file only

In the fourth line, After indenting carefully with four spaces, I declared a variable called “words” and assigned it to the split set of words in the “words.txt” file. This is possible because, in line three, I declared that I would be using the contents of the “words.txt” file as “word”. The set() function is used to create a set object. The items in the setlist are unordered, this makes the items appear randomly. The split() function is used to split given strings or lines into their bits or components by specifying the basis for the split, usually a sub-string as a delimiter. The string before and after the sub-string specified as a delimiter is returned as the output.

The Importance of line four is for reading, breaking down, and sorting the words in the file.

In lines five and six, using a “for loop”, I iterated through the words contained in the variable on line five and used the append function to add items to the empty dictionary list created in line two. The Append function in Python is a pre-defined method used to add a single item to certain collection types. Without the append method, developers would have to alter the entire collection’s code for adding a single value or item.

To print the items in the dictionary, the following lines of code were written. I wrote a while loop to make sure that as long as the code runs perfectly and all is True the next lines of code could run. In the next line, I created another variable named “computerword”. The variable was named with two words without spacing because it will throw an error if there was space between the two words. The program will decode the variable as two different words, which is not the objective in this context.

A Python variable containing two or more words can be either written together or separated by an underscore. It cannot be separated by a hyphen “-”. It is case sensitive too. For example, the variable “Age” and “age” are two different variables in Python because of the different cases of their first letters. The first one is started with an uppercase while the second is lowercase.

I assigned the variable “computerword” to the random choice of the program and the randomly selected choices are gotten from the dictionary list declared in line 2. I printed out the selected word in the next line, this is because it would easier to test if the program works if it prints the randomly selected word first before the next lines of code run. Then in the next line, I created another variable names “shuffle” and assigned it to the sorted randomly selected “computerword”.

The sort() function in Python is used to sort a list of numbers or alphabets in an ascending order or a descending order. By default, the letters will be sorted in alphabetical order, while numbers will be sorted in ascending order. It will throw an error if you try to sort a list that has both string values and numerical values together.

After specifying those variables under the while loop, still under the while loop block of code, I wrote a for loop to iterate all letters of the words saved under the shuffle variable. The sort function I used in the shuffle variable already turned my words into a sorted list of letters. A for loop works with lists because a list is iterable. The newly added lines of code below function to iterate through each letter of the randomly selected word, make it uppercase, and move to the next line.

The “upper()” function in Python is to convert letters to uppercase. It doesn’t work for numerical values. If it is used for an entire sentence, the entire sentence becomes uppercase letters. There are a lot of similar functions like this.

Out of the for loop but still, inside the while loop, I wrote a print function to urge the user to rearrange the letters printed in line fourteen to rhyme with the randomly selected word in line ten. I used ‘\n’ in the print function to indicate the end of a line before and after the text written in the print function.

In line 19 below, I declared a variable “guess” and assigned it to the input function with the guide words for the user to enter the words, and then it will be converted to lowercase irrespective of which case the user used to input the letter. This is because Python is case-sensitive and a word written in uppercase is not the same as the exact word written in lowercase. I had to convert it to lowercase by default to be able to easily assess if the inputted word is the same as the random word selected by the program from the dictionary list.

On line 21, still inside the while loop, I wrote code to ascertain if the word inputted by the user is the same as the word selected by the program. The double equal to sign “==” in Python is used to check for equality, while the single equal to sign “=” is used to assign values to variables. Line 21 means that if the word guessed by the user is the same as the word selected by the computerword, the next line of code could run. In other words, if line 21 is True, the next line which is a Hooray message should be printed. Else, the user should guess again after seeing the “wrong guess” message. If the user guesses a wrong word, the loop breaks and that is the function of “break” I wrote under the else statement.

This code can be modified and improved in a whole lot of ways, but those other concepts might seem overwhelming for a beginner. For example, we could add a life variable in the code, assign it to a particular default number, and count the number of times the user inputs a wrong word. The life variable will reduce by a value of 1 every time the user inputs a wrong word, and when the life number is equal to zero the loop breaks.

To do this, I created a life variable just before the line of the “while loop”, I assigned the variable to the integer “3”. The number of times the user can guess a wrong word and still be allowed to guess another would be 3. I also reassigned the value of the life right under the else statement. The operation “ -= ” is a subtraction assignment. It subtracts a value from the variable and assigns the result to that variable.

In this context, the “-=” sign will subtract 1 from the original value of 1 and assign the result of the subtraction to the same life variable. Every time the user inputs a wrong word, the life value will reduce by 1. The next if statement I wrote was to check for when the final value of life will be equal to zero. The loop breaks when this if statement is True.

An “if statement” in Python is a conditional statement. It outputs True or False. If the conditions written in front of the if statement is met, the if statement outputs True, and the block of code underneath it runs. But if it outputs false, an else statement if found will run. A Python if statement can be housed by another if statement and even another.

The complete game code.

This is the step to step guide on how I created the word game. Following these steps, you too can create a more complex game that is artificially intelligent. Python games are a good way for beginners to strengthen their foundation and get familiar with the Programming language and its interface. Thanks for reading.

--

--

No responses yet