Build Your First Game in Python

Kasim Ali
7 min readMar 21, 2023

Today, you will build your first game in python and find out if you survive the onslaught of enemies! By the end of this tutorial, you will be able to create a choose-your-own-adventure game.

As per usual if you prefer to follow along with the video playlist, click here.
If you want access to the course notes, click here.

Finally here are the Replits with their assignments:

What you will learn:

  • Conditional statements
  • Comparison operators.
  • Module operator.
  • Nested ‘if else’ statements.
  • ‘elif’ statement.

Let's take a look at our finished product for today:

Conditional statements.

What is a conditional statement?

Below, we will use some pseudocode to demonstrate the overall structure of a conditional statement.

Essentially, if the condition is met then we do this.

Otherwise (or else) we do that.

It is important to note, we do not always have to explicitly code the condition for the else statement but sometimes we do need more complexity which you will learn about later.

You will also have noticed that there is an indentation after the if statement and else statement. This is required and if it is not included, your code will not work.

Your basic conditional statement looks something like this:

if condition:
do this
else:
do that

To give this some more meaning, let’s say that you are deciding whether you should eat some ice cream and you decide that if the temperature is over 25 degrees you should eat some ice cream and if it is below you should not.

You could implement this solution:

temperature_c = 26

if temperature_c > 26:
print('Eat some ice cream')
else:
print('Do not eat ice cream')

Since the temperature is 26 degrees we get to eat ice cream.

Build your own decision-maker.

Here is a problem for you to solve. You are once again deciding whether to eat based on the temperature. But this time, you want it to be cold so you can drink some hot chocolate. In this situation, we want the temperature to be lower than 5 degrees.

Input different temperatures directly into the main.py file to test your code.

Use this Replit to build this before checking the answer below:

Hopefully, you were able to find the Replit and build the program without too much difficulty. Compare your answer to the one below.

temperature_c = 5
if temperature_c < 5:
print('Drink some Hot Chocolate')
else:
print('Do not drink some Hot Chocolate')

It is okay if your code looks a little bit different but it should print the correct statement to the console.

Build your own decision-maker with user input.

We have made some very static applications so far, so let's take some user input and build something more meaningful.

Use this Replit to build something that allows the user to tell us what the temperature is and for the application to tell us whether it is a good temperature to eat ice cream or not.

You have been provided with the first line which converts the user input into an integer.

Try it yourself first before looking at the answer below:

temperature_c = int(input('What is the temperature?'))

if temperature_c > 26:
print('Eat some ice cream')
else:
print('Do not eat ice cream')

Wonderful! You were able to take user input and generate different content based on their answer. I bet your mind is probably thinking about all the possibilities.

Comparison operators.

Here is something else you will need to know:

You often need to use comparison operators in your code. They can look like this.

  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to
  • == equal to
  • !+ not equal to

You might start getting confused since in math a single = is used to indicate equals to, whereas in programming we use a single = to assign something to a variable. And we use double == as a comparison operator to indicate equals to.

Modulo operator.

You will not build an application that determines whether a given number is odd or even. You will use the modulo operator which is the % sign.

This operator finds the remainder of your given number. For example, if I used this code:

print(6 % 2)

The console would print 0 because there is no remainder when dividing 6 by 2.

However, the following would print 1:

print(7 % 2)

With your newly learned skills, build an application that tells the user whether they have an odd or even number using ‘% 2'. The console should print one of the following messages:

‘This is an even number’

‘This is an odd number’

You can use the Replit here to get started.

Here is the answer:

As you can see you do not need to explicitly write the condition for the else statement.

Nested if else statements.

Sometimes, you might need to have two conditional statements. For example, if the weather is hot and the time of day since we all know we should not be having ice cream or hot chocolate past 8 pm.

Here is what your pseudocode might look like:

if condition:
if another condition:
do this
else:
do this
else:
do that

Let's build an application that checks someone's height and age for a roller coaster they want to ride. We check the height to determine if they can pass and their age to determine the price they will pay.

Take a look at this Replit to get started.

Here is the answer below:

Well done! You have now created a nested if else statement and were able to implement this in your Replit.

elif statement.

However, you now need to add one more tier. If you are 11 or younger you pay 4GBP, 12 to 17 and you pay 7GBP and if you are 18 or older you pay 9GBP.

You can implement this using elif.

It looks like this:

if condition 1:
do this
elif condition 2:
do this
else:
do that

Try to implement this in the same Replit you used before.

Here is what your code should now look like:

Note that the order is significant here. Python will print the first statement that is true so if we were to switch the if and elif statements regardless of whether the age is 12 or 99, the console would print ‘Please pay 7GBP’.

Logical operators.

There are three very useful logical operators and they are:

  • and
  • or
  • not

When using the and operator. Both conditions on either side need to be met for this line to be True.

When using the or operator. Only one of the two conditions on either side needs to be met for the line to be True.

When using not. The condition needs to have not been met to be True.

Take a look below for a more clear example:

a = 6

a > 4 and a < 7 #true
a > 4 and a < 5 #false
a > 4 or a > 100 #true
not a > 4 # false, this reverses the condition

Now, you will implement a rule for the rollercoaster than give people aged 55–65 free tickets. You can do this with logical operators and nested if-else statements. Use your previous Replit to keep on building complexity with this activity.

You can see that on line 7, all conditions need to be true for the console to print ‘It is free’.

if height >= 140:
if age >= 18 and age >= 55 and age <= 65:
print('It is free')
elif age >= 18:
print('Please pay 9GBP')
elif age >= 11:
print('Please page 7GBP')
else:
print('Please pay 4GBP')
else:
print('You cannot ride this rollercoaster')

Since Python interprets the code line by line, the first condition that is met will be executed and the rest of the if statement will be ignored.

Adventure Game.

It is time to use your skills to build your adventure game. To do this, use the Replit here and remember that you will need to use comparison operators and nested if-else statements.

You can choose to use logical operators if you choose.

Here is one possible answer:

You can play this game here.

Ascii art is available from many sources online. Here is what I used.

Conclusion.

In conclusion, you learned a lot today and covered a lot of material. Here are some things we learned together:

  • Conditional statements
  • Comparison operators.
  • Module operator.
  • Nested ‘if else’ statements.
  • ‘elif’ statement.

You should now be able to take in user information and do some interesting things with it using logical operators and comparison operators.

Test your skills by building something interesting. Maybe a tip calculator, a sassy BMI calculator or even a developed game.

Thank you for reading this tutorial. Please consider becoming a fan and subscribing to my YouTube.

--

--