Academind Logo

Python Tutorial: Learn Python by Building a Game

Python is THE most trending programming language in 2019! Get started now and learn it from scratch by building a game - no prior Python knowledge is required, so what are you waiting for?

Created by Manuel Lorenz
#

Learn Python from Scratch by Building a Game - Project Setup

Python is an awesome programming language, but getting started can be difficult. In this article I'll guide you through the Python basics in a practical and fun way by building a game from scratch.

You are only two steps away from getting started, just download & install Python 3 from python.org and install Visual Studio Code to make writing Python code a breeze.

After creating a new project (please follow this guideline in case you never worked with Visual Studio Code before) we can already dive into Python!

#

What are we Going to Build?

The Python game structure

This image also shows the core components we need to build:

  • Two parties with certain attributes - the player and the monster

  • A user interface to allow the user to input data to access these attributes

  • Logic to react to user input (e.g. after an attack, the health level of the opponent must decrease)

  • The game logic to start a new game as soon as either the player or the monster ran out of health

Besides that, we'll also add optional features like displaying a highscore list, but that's something we'll focus on later. Although the game might appear rather simple at this stage, we need a lot of programming and Python basics to make this game work smoothly.

#

Variables

variables are required whenever we need to store and access information in our code. An example would be to store the name of a person, which could be created as follows in Python:

person_name = 'Manu'

Note the underscore after person and the single quotation marks around Manu. The first is a naming convention for variables, no capital letters should be used and connecting multiple words should be done with an underscore (this notation is also named snake_case), the latter indicates that the variable contains a string. But what is a String?

#

Data Types

A String is one of 4 core data types in Python, besides Strings we have:

# Integers
person_age = 30
# Floating Point Numbers or Floats
player_score = 1.3
# Booleans
player_alive = True

We'll dive into Booleans a bit later in this article, so let's focus on Strings and Integers at this point. We use Strings whenever we want to add text to our code, so the name in our case. Writing numbers is done via Integers or Floats, quotation marks are not required here though.

On our slide we saw that we need certain attributes for the player, with variables we are able create these easily:

player_name = 'Manuel'
player_attack = 10
player_heal = 16
player_health = 100

We can now store information in variables, but no output is displayed in our terminal (which will be our user interface in this project) so far.

#

print()

Printing content to the terminal requires a, perfectly named, method or function. We'll have a look at functions later in this article, for the moment let's assume that functions allow us to perform specific actions without writing any additional code.

Using the print() function and adding the variable name between the parentheses will "magically" display the information stored in the variable in our terminal:

print(person_name)

That's cool, right? However, instead of always entering a different variable to print another type of information (e.g. player_heal or player_health), we should look for a better solution to store information in Python.

#

Lists

With Lists we can add multiple items to a variable. In our case a List could look like this:

player = ['Manuel', 10, 16, 100]

That's a big improvement already, we now saved all player related information in this variable. Besides saving the information, we can also access the different List items:

print(player[1])

This command will print the second item in our List. Why item 2 and not item 1? Because accessing List items works via indexes - and the indexes happen to start at 0, not 1. This means that item 1 (Manuel) has an index of 0, item 2 (10) has an index of 1 and so on.

Updating information is also possible, let's say we want to change the name from Manuel to Max:

player[1] = 12
print(player[1])

We now changed item 2 (with an index of 1) from 10 to 12, so we can also update data stored in a List, pretty cool isn't it? Still, working with indexes works fine, but accessing the items in our List not by an index but by an actual value would be a lot more explicit here. For example, our recently changed List item with an index of 1 could be accessed with the 'attack' keyword.

#

Dictionaries

In contrast to Lists, Dictionaries store information in so-called "key-value-pairs". How does this work?

person = {'name': 'Manuel', 'attack': 10, 'heal': 16, 'health': 100}

The notation is quite different from the List, isn't it? Each value (so 'Manuel', 10, 16 and 100) is assigned to a "key". Keys must be wrapped in quotation marks and followed by a colon, the naming of the key should describe the corresponding value it contains.

Accessing Dictionary keys is also possible of course:

print(person['name'])

This will print the value stored under the 'name' key, 'Manuel' in this case. Changing the value is also possible:

person['name'] = 'Max'
print(person['name'])

This will print 'Max' as the newly assigned value for the 'name' key.

Dictionaries seem to be the right approach to create our player and moster, the monster could look like this:

monster = {'name': 'Max', 'attack': 12, 'health': 100}

With these first steps done, we're ready to dive deeper into our game. So far we only display information, but the user should actually be able to input some information too. This will be the next step in our Python journey...

Recommended Courses