Part 1

Creating a Window

To start our project let’s start a new file and enter the following lines:

TITLE = "Burgers"
WIDTH = 1000
HEIGHT = 714

When it’s a good time to test your code you will see this icon:

_images/play_icon.png

When you see this icon you can hit the Play button in Mu to start your game. If you can’t see the button you might need to press stop first.

Go ahead and press play now. You should see an empty black window. Now let’s add a background.

Add these lines to the end of your file:

def draw():
    screen.blit("burgers/background",(0,0))
_images/play_icon.png

There’s the play icon again. Hit play and you should now see the Burgers background. If this worked you can jump ahead to Ingredients List. If you got an error read on for tips on fixing it:

Errors (Skip if you don’t have an error)

The first thing to check is that you downloaded the required images for this project. See Installing Burger Images for details on this. You can check your images are in the right place by clicking on the Images button in Mu. This will open the images folder, you should have put the burgers folder inside this folder.

The second problem you might have is if you saved your code file in a custom location. Unfortunately Mu only likes code files that are saved in its mu_code directory. This is the directory that is one level above the images directory that opens up when you click Images.

Ingredients List

In this project we’re going to create our own class called GameData. We’ve already used classes in the Flappy Bird tutorial. When you typed barry_the_bird = Actor... you were using the Actor class.

A class is kind of like a blueprint for an object. Each class makes its own type of object. The Actor class makes objects that we can draw and move around on the screen, like a Sprite in Scratch.

Type the following at the end of your code to create a new class called GameData.

class GameData:
    pass

game = GameData()

Remember that indendation (number of spaces at the beginning of the line) matters in Python!

The first two lines make the simplest class you can possibly have, with nothing in it! The game = GameData() line creates an object from this class. Classes are normally more complicated than this, but to keep things simple we’ll start with an empty class and just add stuff to it as we go.

Next add the following lines to the end of your file:

def start_game():
    game.items = []
    clock.schedule(spawn_item, 1)

def spawn_item():
    new_item = Actor("burgers/bun_top", (500, 100))
    game.items.append(new_item)

start_game()

What did we just add?

We added function called start_game, and a function called spawn_item, these are functions we’re adding so we could have called them anything we want. The start_game function does two interesting things:

game.items = []

The [] symbols make a list, and there’s nothing inside the [] so it’s an empty list! We’ll add some items to it soon. This whole line means we’re creating a list variable called items in our game object. The game object is where we’re going to keep all of our game variables.

clock.schedule(spawn_item, 1)

This line uses the clock.schedule function which is part of Pygame Zero. It says that we would like the spawn_item function to be called 1 second later. We use this make a delay.

The next function we just added is the spawn_item function. This is the function we just told clock.schedule to call! Let’s look at what it does:

new_item = Actor("burgers/bun_top", (500, 100))

This line creates a new Actor, just like we did in Flappy Bird. We use a variable called new_item to hold it.

game.items.append(new_item)

This adds the new Actor we just created to the list we created in the start_game function.

start_game()

Finally this is the line that will actually start the game!

Make sure start_game() is always the last line of the file

We won’t see anything different yet, but go ahead and check that your code still runs ok:

_images/play_icon.png

To give us something to see we need to add these highlighted lines to the draw function:

def draw():
    screen.blit("burgers/background",(0,0))
    for item in game.items:
        item.draw()

The for keyword is a way to make a loop. In this loop were going take every item in our game.items variable and then draw the item.

_images/play_icon.png

Now you should see that after one second a part of a burger appears on the screen!

Let’s add an update function to make this sprite fall down the screen:

def update():
    for item in list(game.items):
        item.y += 5

We’re using for again to loop over all the items in our list. There’s only one item in the list so far, but we can change that now. Add this line to end of your spawn_item function:

def spawn_item():
    new_item = Actor("burgers/bun_top", (500, 100))
    game.items.append(new_item)
    clock.schedule(spawn_item, 0.5)

This means that everytime an item spawns we schedule another to be spawned again in 0.5 seconds.

_images/play_icon.png

Now you should see a trail of burger tops streaming down the screen.

Mix It Up!

Next we’re going to randomize which ingredients fall down and the screen and we’ll make it so they’re not all in the center.

Add these highlighted lines to the top of your file:

import random

TITLE = "Burgers"
WIDTH = 1000
HEIGHT = 714

SPAWN_ITEM_INTERVAL = 0.5
ITEM_X_MIN = 250
ITEM_X_MAX = 750
FALL_SPEED = 5

Can you guess why what these lines are for?

First let’s change our start_game and spawn_item functions to use the new SPAWN_ITEM_INTERVAL variable. We can also change the update function to use FALL_SPEED. The highlighted lines are the lines you need to change:

def start_game():
    game.items = []
    clock.schedule(spawn_item, SPAWN_ITEM_INTERVAL)

def update():
    for item in list(game.items):
        item.y += FALL_SPEED

def spawn_item():
    new_item = Actor("burgers/bun_top", (500, 100))
    game.items.append(new_item)
    clock.schedule(spawn_item, SPAWN_ITEM_INTERVAL)

This means we have one convenient place at the top of the file to tweak the game.

_images/play_icon.png
  • What happens if you set FALL_SPEED to 50?

  • What happens if you set SPAWN_ITEM_INTERVAL to 0.001?

  • When you’re done experimenting set FALL_SPEED back to 5 and SPAWN_ITEM_INTERVAL back to 0.5

Next let’s create a list of image filenames so that we can pick at random which to use. Add these highlighted lines after your constants at the top of your file:

ITEM_X_MAX = 750
FALL_SPEED = 5

NUM_ITEM_TYPES = 5
item_images = ["burgers/bun_bottom",
               "burgers/bun_top",
               "burgers/meat",
               "burgers/cheese",
               "burgers/tomato"
               ]

Remember the [] from before? Well here they are again, but this time there are 5 items in the list, each one is the file path to an image we want to use.

Next, change our spawn_item function so it picks one of the random images (you need to change the highlighted lines):

def spawn_item():
    item_type = random.randint(0, NUM_ITEM_TYPES-1)
    new_item = Actor(item_images[item_type], (500,100))
    game.items.append(new_item)
    clock.schedule(spawn_item, SPAWN_ITEM_INTERVAL)

Now when we create the Actor we’re using an image from our item_images list. The code that selects an image is item_images[item_type], this time the [] are not creating a new list, they’re accessing part of a list we already have.

In Python the [] symbols do two different things:

my_list = ["Cat", "Dog", "Parrot"]

print(my_list[1])

On the first line they create a new list, and on the second line they’re used to get item number one in the list. Item one is Dog! Lists start at zero in python.

We use random.randint to randomly choose a number between 0 (the first image in the list), and NUM_ITEM_TYPES-1, the last item in the list.

  • Can you figure out why we do NUM_ITEM_TYPES - 1 instead of just NUM_ITEM_TYPES?

_images/play_icon.png

You should now have some random burger pieces falling down the screen.

Finally let’s spread them out randomly from left to right. Change this highlighted line in your spawn_item function:

def spawn_item():
    item_type = random.randint(0, NUM_ITEM_TYPES-1)
    new_item = Actor(item_images[item_type], (random.randint(ITEM_X_MIN, ITEM_X_MAX),100))
    game.items.append(new_item)
    clock.schedule(spawn_item, SPAWN_ITEM_INTERVAL)

Now we’re using random.randint again to pick a random starting x value for each item. The second parameter to the Actor function is an (x,y) pair of numbers. We’re using our new call to random.randint for the x value, and 100 for the y value.

_images/play_icon.png

You should now game that looks something like this:

_images/screenshot_falling_items.png

Served Up Next

In the next part we’ll add a plate so we can catch the pieces and build some tasty burgers!