Objects: learn OOP with Scratch and Python

By Russell Barnes. Posted

Learn how objects work by building a dice game in Scratch and converting it to Python

The best way to understand how objects work in Python OOP (object oriented programming), is by starting with Scratch.

This is because the sprites you build in Scratch, work like objects in Python. Because Scratch is much more visual you get a sense of how objects handle data and functions. And it's fun.

First, we’re going to create a dice game in Scratch. Then we’ll recreate it in Python so you can see how it works in both languages.

Learn about objects by creating a dice game

Our dice game is based on Bunco. It’s a popular parlour game played in North America.

We’ve made the rules a little simpler. Each player rolls three dice and counts up the score. The player with the highest score wins.

We need two players, each with their own set of dice. Each player then rolls the dice, looks at their dice, and compares them with the dice of the other player.

If they have the same score, both call it a draw. If a player spots that their total is higher than the other person’s, they shout out “I win!”.

Local vs Global variables in Scratch and Python objects

This game introduces you to the concept of local vs global variables. Each sprite has three local variables: their own set of dice. They can also look at the variables (or dice) that are local to other sprites.

The opposite of a local variable is a global variable. This is as if both players used a single set of dice and both shared the same result. They’d always draw.

This article is based on Learn Object-Oriented Programming in issue 54 of The MagPi. Issue 53 of The MagPi has a companion piece on learning to code with a Raspberry Pi

See also:

Creating objects in Python and cloning sprites in Scratch

Scratch works slightly differently to Python. In Scratch, you create one sprite and then clone (duplicate) it to create a second sprite. In Python, you create a blueprint for your sprites (known as a ‘class’) and then stamp out two player objects. We’ll come to Python in a bit.

Let’s create the dice game in Scratch first…

Step-01: Scratch 2.0

Objects Scratch and Python

Open the web browser and visit scratch.mit.edu to open Scratch 2.0. We need the clone features from 2.0, so don’t use the Scratch 1.4 app. Log in (or create an account if you’re new to Scratch). Create a new project and you’ll see a single Scratch Cat sprite on the screen. Click the ‘i’ symbol next to the sprite in the bottom-left. Change its name to Player1.

Step-02: Create three dice variables

Objects Scratch and Python

Click on Data and then Make a Variable. Enter dice1 in the Variable name field and select ‘For this sprite only’. Click OK and dice1 appears in the blocks palette. Repeat the process to create dice2 and dice3.

Finally, create another variable called total. Remember to choose ‘For this sprite only’ for all three dice and total.

Step-03: Randomly throw the dice

Objects Scratch and Python

Click on Events and drag a when green flag clicked block to the scripts area. Below this you need to add three set diceN to pick random 1 to 6 blocks. Below these blocks, add set total to dice1 + dice2 + dice3 (you need to drag one () + () block inside another to add up three blocks.

Step-04: Speak the variable results

Objects Scratch and Python

Now drag a say for block and attach it to the end of the code. Change it to say total for 1 secs. Below that, drag an if block. Inside, add a [] > block. Drag the total variable to the left side of the greater-than block. Drag a say block inside the if block and change ‘Hello!’ to ‘I win!’.

Step-05: Compare scores

Objects Scratch and Python

We’ve only got one sprite so far. But we’re about to add another and compare one sprite’s total with the other. Choose the Sensing selection of blocks and look for one marked x-position of Player1. Change ‘x-position’ to total. Drag the block into the correct side of the greater-than block.

Step-06: Clone the Sprite to create another

Objects Scratch and Python

Our Player 1 is ready. Now we’re going to clone the sprite to create a Player 2. Right-click the sprite and choose Duplicate. The new cat sprite will be automatically called ‘Player2’. Click on Player1 in the Sprites window. Change the total of Player1 block to total of Player2 (as shown below). Now Player1 compares their total to Player2 (and Player2 is comparing theirs to Player1). Click the green flag to run the program and see which player wins.

Scope in object-oriented programming

The concept of scope is important in object-oriented programming. In Scratch, it’s so simple that you may not even notice it. But our two players each have their own dice1, dice2, and dice3 variables, plus a total variable.

These variables are local in scope. When Player1 announces total, it’s their total. If both sprites accessed the same total, it would be global in scope.

You can experiment with this by replacing the variables created in Step 2 with ones marked "For all sprites". These would be global variables, and both characters will always draw (as each rolls the same set of dice, and each checks the same pair of dice).

Now you've learned how global and local variables work, you're ready to start building objects in Python.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.