How to code your first simple game using Python
All you need is a .py file and the command line!
If you’re new to programming, the idea of writing a script that performs a specific task can seem daunting. In this post, I’m going to show you how to write a very simple Python script containing only a few functions that allows you to play a simple math skills game.
Open a .py file
Using any text editor (I’ll use Sublime Text), open a new file by navigating to File-> New File within the program. Then, save it as a .py file somewhere you’ll be able to find it later (as a Windows user, I’ll use my User folder in my C: drive). Now you’re ready to start writing your game!
Math Game
Let’s say I want to write a program that helps me practice my addition, subtraction, and multiplication using only the integers 0 through 9. While this is obviously a very simple use case, I think it’s a great jumping-off point for more complex things.
How to get the initial numbers?
In this game, I’d like some way to generate random integers (0 through 9) for each math question. The Python random module can accomplish this through the randint() function. I can import this module and function into my script by writing the following into the .py file:
Beginning the game
Next, I’ll define a function in the script that begins the game when called. This function will have a few different tasks to perform:
- Introduce the game to the user
- Ask the user if they’d like to do addition, subtraction, multiplication, or exit the game
- Take the user’s input and proceed based on the user’s choice
My start_game() function below accomplishes each of these tasks.
Additionally, the final part of the if-else statement above simply returns the user to the beginning of the game if they make an invalid input. Otherwise, the script would return an error message if the user entered something other than A, B, C, or D.
Playing the Game
Once the game begins, our user can choose from addition, subtraction, or multiplication. So, I’ll set up the script so that each of the input options in start_game() lead to a different function allowing the user to practice a different math operation.
First, I’ll start with addition:
My addition function generates two random integers using the randint() function. Then, the user is prompted to input the sum of these numbers. If the input matches the actual sum of these numbers, the user is praised and returned to the beginning of the game to play again (Note that the user’s choice must be converted to an integer before it is compared to the actual sum, because the initial input will be stored as a string). If the user gives an incorrect answer, they are given feedback and also returned to the start of the game. This general structure can be followed for subtraction and multiplication with minor changes:
Initiating the Game
To actually use and interact with the functions I’ve written, I’ll call the start_game function at the end of the script.
Running the Script (Playing the Game!)
Now I’m ready to play and test my game. First, I’ll need to open Command Prompt. You can also use other terminal applications like Git Bash, Windows Powershell, Mac Terminal — any of these will allow you to run Python scripts from the command line.
With Command Prompt open, I see that I’m conveniently already working in the directory where I saved my script: C:\Users\dougl.
Now that I’m in the correct directory, I can simply run my Python game script using the following command: python math_game.py.
This takes me immediately into the game, with the following output:
The game introduces itself, gives me my options, and has an input prompt for me to make my selection. Let’s see what happens if I choose ‘A’.
My game asks me to add 6 and 1. If you remember, each of these numbers were selected using the randint() function.
Entering the correct answer, 7, results in positive feedback from the game. And, due to the way that the addition() function is set up, we go right back into a new round of the game. This will happen for every question until I decide that I’m finished, and select ‘D’ to exit the game.
Conclusion
If you’re new to programming and Python, I hope you found this a useful (though overly simplified) example of how to code your own simple game using just a script, a few functions, and the command line. Now that you’ve got some basics, get out there and create a more complex, more interesting game than mine! Think of an entertaining game concept, and figure out how to bring it to life with code. I’m sure you’ll learn a lot along the way.
References:
This post was inspired by several of the exercises in Learn Python 3 the Hard Way— a book that I would strongly recommend for anyone new to Python.
https://www.amazon.com/Learn-Python-Hard-Way-Introduction/dp/0134692888