2 – State of Play


🎯 Learning Objectives

After this lesson you should be able to:

  • Explain what makes games work
  • Think about games in the context of states and moving between them
  • Use some basic ideas about objects and why they’re important
💬 Key Vocabulary

  • Interactive
  • State
  • Player
  • AI
  • Object
  • Class
  • Parent
  • Inheritance
  • User Interface

Introduction

In this lesson, you will:

  • Explore what states are
    • You will use some examples, and then try and come up with answers of your own
    • You will learn how states contribute to the way games are played
  • Learn about Object Oriented Programming and why it’s so useful in games
  • Start thinking about the importance of how games feel to play – the User Experience
Last lesson, you…

  • Analysed an existing game
  • Brushed up on your Python skills and built a simple demo
  • Completed a case study on Agile
Today, you will…

  • Write some more programs
  • Explore the concepts of states and objects
  • Learn about user experience

📝 Starter Activity

  • Let’s see what you know already about states.
A light switch – simple, huh?
  1. What do switches do?
  2. How many different ways are there to set these switches?
  3. How might you be able to use these switches to communicate something?

Think about these questions, then have a look at the answer below.

Click here for the answer

📖 Learn it – States

Imagine you had those two switches on your desk, now labelled “I need more pens” and “I need more paper”. Using these two switches, you can send four messages:

  1. I need pens
  2. I need paper
  3. I need pens and paper
  4. I don’t need either.

We can see each of these four situations as a state. By changing the switches, we can change between them – our switches enable us to transition between different states. Each state is simply recording and remembering what we have done to the switches.

This is exactly how almost every interactive device works, it uses states to keep track of the user input controlling it.

Here are some more examples from everyday life:

  • Televisions – they remember the current channel/programme and the current volume. When you press the volume control or a channel button, the TV responds by doing something. The state here is the current value of everything, so which channel you are on, the current volume, any other settings, are all part of the current state.
  • Vending machines – they keep track of how much money you have put in, and whether you have selected a product yet. States mean you can select a product first or put money in first and it can keep track of both.

📝 Activity 1 – Game State

Think of a game you have played recently. Write a list of what you think its states might be, and then explain what the player does to move between them. In your answer, explain how using these states helps the game keep track of what is happening.

📖 Learn It – Timmy

This is Timmy. Timmy is a cat.
Timmy is also an object, because all things are objects. What makes Timmy himself?

Click here for the answer

📖 Learn It – Everybody wants to be a cat

Because a cat’s the only cat who knows where it’s at…
Timmy isn’t the only cat – there are lots of them, and they have different attributes describing themselves. But, they’re still all cats…
This property they all share defines them as the same kind of object. We call this concept a class.

📝 Activity 2 – Top of the Class

  • A class is the idea of a type of object, not an object itself.
  • It’s like a template, design, or blueprint.
  • It tells us what that object is made up of, and what it can do, but not necessarily how to do it.
  • We can use the class to make similar objects really quickly.
  • In games, this is really useful – it’s how we make e.g. players, enemies, items without having to describe them in code each time.
  • In Python, it looks something like this:

The advantage is hopefully quite clear – we don’t have to describe a player each time we want a new one, we can just tell our program what a player is, and then ask it to make more of them!

Try it for yourself: make an extra player, and print out their details.

  • Classes can also be used to make other classes as well as objects that are instances of the class.
  • A class made from a class is called a child and it does something cool – anything the parent class has, the child class automatically inherits, but the child can also add new properties or even change the existing ones.
  • We can use this property to make objects that are similar, but not the same.
  • Maybe our game contains both human players and computer enemies – they are probably very similar characters, but our human players need to have control, where the computer enemies should be controlled automatically.
  • This means we can make a parent class that describes any character, and then have player and enemy child classes that take that basic design and modify it for the individual uses. This way, we don’t need to write the code they share twice.

Try it for yourself: make a new Enemy class, and make Player 2 into an enemy.

📝 Activity 3 – An Experience

The programs above use the console for text output. Last time, we used pygame to draw a window instead, and filled it with shapes. Things like images, colour, and graphics make a program much nicer to use than just a bunch of text on a screen.

How a program looks and feels controls what’s called the User Experience, or UX for short. Essentially, having a good UX means your program is nice to use! It incorporates what you see and click on (the User Interface or UI) but also things like the order of steps in a process, the speed of actions, how obvious features are, the overall ease of use, and so on.

Come up with three key ideas that you think are most important for the UX of a game, and for each one, justify your decision with reasoning. Submit your ideas for the Platinum badge task.

💬 Summary

In this lesson, you…

  • Explored the concepts of states and objects
  • Built some more simple games
  • Introduced the idea of user experience

In the next lesson, you will…

  • Learn more about user experience
  • Use it to design an interface for a game

🏅 Badge it

🥈 Silver Badge

  • Upload your notes on states to the Silver badge task.
🥇 Gold Badge

  • Upload a screenshot of your trinket code to the Gold badge task.
🥉 Platinum Badge

  • Upload your ideas about User Experience to the Platinum badge task.