top of page

Goblin Hunt >>> Lesson 2: Setting up the game



So far in Lesson 1 we haven't done much except for preparing a welcoming text and asking the player for their name. Let's start setting up the game now.

The goblin will be hiding in our kitchen cupboards. In this very simple example we will not worry about pretty graphics, so we can depict the kitchen cupboards using a string of characters as follows. Add these two lines of code to the three you wrote in Lesson 1 (it's best to leave a blank line before them for clarity):

cupboard_door = "|_||_||_||_||_|"

print(cupboard_door)

On the right hand side of the = is a string of characters, or simply a string, enclosed in quotation marks "" as we saw in Lesson 1. We are using the vertical bar, or pipe symbol | which is Shift and backslash (\) on most keyboards for the sides of the cupboard doors, and the underscore _ which is Shift and dash (-) for the door handle. In this example we have five cupboard doors. We are assigning this string to a variable which we have called cupboard_door. Note that variable names cannot have spaces so we use an underscore _ to separate words. Variable names should always be chosen such that they clearly represent the data they contain. This might not seem important in a short program such as this one but it becomes essential when writing much longer programs.

What we have done here is, however, considered bad practice in programming since we are forcing our game to always have five doors. What if we want to change the number of doors to eight, say?. Let us make the change we need in two stages. First, change the two lines above as follows:

cupboard_door = "|_|"

print(cupboard_door * 5)

The variable cupboard_door now represents just one door but we are still showing five doors by adding *5 in the print() function. Asterisk * represents multiplication in computing. You can run your program ('run' button) to check that it works. This is still bad practice, however, since any number, such as the five in this case, should be assigned its own variable name, as follows:

nu_of_doors = 5

cupboard_door = "|_|"

print(cupboard_door * nu_of_doors)

The reason we want to do this is as follows. If we need to use the number of doors several times in the code, and then we decide to change the number of doors, we only need to change this number once when we assign it to the variable nu_of_doors and not everywhere we have used the number of doors in the program. The data contained in the variable nu_of_doors is not a string of characters this time but a number (which is why we don't put quotation marks "" around the number 5). Programming languages distinguish between whole numbers, called integers, and decimal point numbers, often referred to as floating point numbers or floats in computing. But we don't need to worry about that here.

In our next lesson, we will choose where to hide the goblin and ask the player to guess where it is hiding. In doing so we will introduce one of the most important commands in programming that will allow us to make decisions inside our program. Lesson 3 is waiting for you here.

Any issues, problems or questions? Email me at info@codetoday.co.uk

7 views

Recent Posts

See All

Python Coding for Young People

CT-Unlimited-FinalRAW-transparent.png

Codetoday Unlimited is for the curious teenager or preteen keen to learn proper Python coding. Stephen's courses start from the basics and carry on to intermediate and advanced levels.

Python Coding for Adults

The Python Coding Place is Stephen's platform full of courses and other resources for beginners and intermediate learners. The focus is on clarity and Stephen's unique communication style.

bottom of page