🞠 Home 🞠 Tutorial 1 🞠


Basic Coding

This page contains resources and instructions for building a small text based game in the programming language Python.

Typically, IDEs (integrated development environments) are used to write code. Technically, you can write code in any program that can open text files (files that end in .txt) but IDEs also allow you to run programs and include other helpful tools.

The intended online Python IDE for this tutorial is https://www.online-python.com/

Alternatively the desktop IDE IDLE is Python's own IDE, and is installed with Python.

Contents

Examples

These example programs were made using the template provided on this page. If you cannot run the files directly on your device, open them in your IDE either by saving the files or copying and pasting the contents.

While exploring these example programs, consider what kind of game you could create using this structure:

Designing a game.

The first step in making a game is planning.

Planning allows you to decide how complex your game will be. In the examples section, you saw the type of small game you'll be making. Start small. You can always make a small game larger if time allows.

For a first project, consider limiting your initial design to three rooms or less, each with no more than two interactions. Take two minutes to draw a simple outline.

Below are the outlines for the example games.

Programming Basics

GenAI
Generative AI is not an exception to computers being stupid. Generative AI is using probability and a little bit of randomness to guess the next word based on the last few words. Chatbots outputting incorrect information or garbled nonsense are correctly following the exact same instructions as chatbots that output correct information.

Programming, at it's core, is understanding a small number of simple concepts.

Computers are stupid. They can only do exactly what they are told to do in the order they are told to do it.

Programming languages allow us to write instructions for a computer in such a way that humans can understand.

In Python, anything on a line after a # is ignored, by the computer, when the code is run. This allows us to make comments in our code — notes to remind ourselves, and inform others, about the program.

# Comments can take up an entire line
a = 1 + 3 # Or comments can be written on the same line as your code

Variables are named addresses in a computer's memory where data is stored. We can manipulate that data from anywhere in a program.

# Assigning values to variables
myNumber = 1 # An integer variable
myText = "Hello World" # A string variable
myList = ["apple", "Banana", "Orange", 5] # A list variable
myFlag = True # A boolean variable, it can be True or False

Because computers can only carry out instructions in the order they are given, how can we make computers respond differently to different circumstances?

Firstly, we can use Branching.

Branching tells a computer to skip a block of code if a condition is not met.

In most programming languages, this can be done using if statements.

# Python if statement
if myNumber == 1: # == is a comparison, it checks if the values are the same.
    print("My number is 1")

If we want code to run only when the code in the if statement does not run, we can use else.

# Python if/else statement
if myNumber == 1:
    print("My number is 1")
else:
    print("My number is not 1")

If there are multiple conditions, they can be nested to define more complex behaviour.

# Python if/else statements with nesting
if myNumber == 1:
    print("My number is 1")
else:
    if myNumber == 2:
        print("My number is 2")
    else:
        print("My number is not 1 or 2")

When there are many conditions, nested if statements can be difficult to read. Instead of nesting if you can use else if (written elif in python code) for most cases.

# if/elif/else equivalent to the nested example above
if myNumber == 1:
    print("My number is 1")
elif myNumber == 2:
    print("My number is 2")
else:
    print("My number is not 1 or 2")

else is not required when using elif.

Another way to change the flow of a program is by using loops.

If you want to run the same code multiple times in a row, one solution is to type the code multiple times.

# Repeated code in Python
myNumber = 1
myNumber = myNumber + 1 # myNumber = 2
myNumber = myNumber + 1 # myNumber = 3
myNumber = myNumber + 1 # myNumber = 4
myNumber = myNumber + 1 # myNumber = 5
myNumber = myNumber + 1 # myNumber = 6
myNumber = myNumber + 1 # myNumber = 7
myNumber = myNumber + 1 # myNumber = 8

This takes up space and, if you want to change the repeated code, you then have to modify every instance.

Loops allow the same piece of code to be repeated until a condition is met, without typing the code out multiple times.

# While loop in python
while myNumber < 8: # Will run 7 times if myNumber = 1.
    myNumber = myNumber + 1

This is more efficient than the same code without a while loop.

Functions are named blocks of code that perform specific tasks. Once they're defined (def in Python), they can be used anywhere else in the program. You can even call a function from within itself to loop through code although it's best to avoid this.

def myFunction(myPassedNumber): # This function requires myPassedNumber 
    print(myPassedNumber + 6) # Displays the answer to myPassedNumber + 6

myFunction(3) # The function is called, myPassedNumber is 3, the number printed is 9

Functions can also return values.

def mySecondFunction(myPassedNumber):
    myPassedNumber = myPassedNumber + 6 # Adds 6 to myPassedNumber
    return myPassedNumber

newNumber = mySecondFunction(7) # The function is called, myPassedNumber is 7, newNumber is 13

No one is expected to memorise an entire programming language, You learn the formatting and the most common instructions and look up anything you don't know online.

W3Schools is an often recommended reference for coding.

Template

Download Python Script

Or copy the script from this page:

# Text based game template
# Has two rooms a player can move between

# Global Variables
# Define variables here for player and game data - e.g. player health or an inventory


# Misc Functions
def playerAction():
    return input("What do you do: ").lower()


# Rooms

def startRoom():
    # Room Content
    # Players need to know what they can do in each room.
    print("This is the first room.") 

    # Player Actions
    action = playerAction()
    
    if action == "go south":
        secondRoom()
    # Insert other actions a player can take here, in lowercase

    else:
        print("You can't do that.")
    startRoom() 


def secondRoom():
    # Room Content
    print("This is the second room")


    # Player Actions
    action = playerAction()

    if action == "go north":
        startRoom()
    # Insert other actions a player can take here

    else:
        print("You can't do that.")
    secondRoom()


# Run Program by calling the start room
startRoom()

Contributed games

None yet!

Send your own game to see it listed here

Where to go Next?

If you're interested in making more games, Ren'Py is a visual novel engine that is completely free. Ren'Py is based on python so everything you learnt today can be used in Ren'Py.