Computer Programming and Game Design With: Scratch
Computer Programming and Game Design With: Scratch
Programming
and Game
Design with
Scratch
What is Computer
Programming?
Computer programming is the process of writing instructions to be
carried out by a computer.
Your computer and all of the websites you visit, your phone and
all the apps you use, and any video games you play are all made
possible by computer programming.
Computer programming is also the driving force behind many of
the world’s current technological advancements, including artificial
intelligence, autonomous vehicles, and virtual reality.
Reflect Brainstorm
Iterate
Prototype
Playtest
2
What is Scratch?
There are many different programming languages. Some of the
most popular programming languages include Python, JavaScript,
and C++. We are going to use a language called Scratch to learn the
basics of programming and game design.
3
The Scratch Project Editor
The Scratch project editor is where all of our coding,
designing, and creating will take place.
Stage - Here you will see the Tool Select Tabs - Choose View Options - Here you can
output of your project as you’re between Code, Costumes, switch between different view
building it and Sounds tools. Switching modes for the project editor.
between these tabs will change
Sprite Menu - Sprites are all what tools are available in the Backpack - The backpack allows
characters and objects in our sections below. you to transfer code, sprites
projects. Here you can control and backdrops between Scratch
what sprites are in your project, Block Palette - Here you projects.
and the name, position, visibility, can see all of the available
and size of those sprites. Add code blocks. Code blocks are Project Options - Here you can
more sprites by clicking on the separated into color-coded save your project, add a title,
blue circle with the cat face in categories, like Motion, Events, share your project (which makes
the bottom-right of the menu. Sound etc... it public), change the language,
and other options.
Backdrop Menu - Backdrops are Coding workspace - Here
backgrounds for our projects. you can write your programs Account Options - Here you can
Add and manage backgrounds by connecting blocks from access your other projects and
here. Add a new backdrop by the block palette. Note that other account settings.
clicking on the blue circle with each sprite will have its own
the mountains at the bottom of programs.
the menu.
4
Code Blocks
In Scratch, programs are written by connecting the colored code
blocks. To start building programs, drag blocks from the block
palette into the coding workspace in the center of the project editor..
There are currently 119 different blocks in Scratch, divided into 8 categories: Motion, Looks, Sound,
Events, Control, Sensing, Operators, and Variables. We will introduce blocks as they come up in our
code, but it is very helpful to spend some time looking through the categories and familiarizing yourself
with the different available blocks. Understanding the different tools at your disposal is crucial to being
an effective programmer! Visit the Scratch Wiki blocks guide for detailed information about all of the
different types of blocks and how they work together.
Here are some general tips for working with Scratch’s code blocks:
Block shapes - Pay attention to the shapes of blocks and different blocks connect to one another. A
block’s shape will tell you about how it is to be used in a program and how it will work with other blocks.
5
Code Blocks (CONTINUED)
Numbers and Text - In any blocks containing numbers or text inside a white space, these numbers
or text can be edited to any value you want! Just click on the text inside the block, delete it, and write
whatever you want in its place.
Dropdown Options - Some blocks contain a small white dropdown arrow. Additional options can be
selected for these blocks . Just click on the arrow to expose the menu, and select your option.
6
Remixing
Scratch, in addition to being a programming language and toolkit, is
a community of programmers and creators. You can view and play
with projects from other users by going to the Scratch homepage and
clicking “Explore”. One key feature in Scratch is the ability to Remix
others’ projects. Remixing allows you to add to or change another
user’s project. When you remix another user’s project, you save a
copy of the project to your own account, so changes you make will
not change the original project.
7
Remixing (CONTINUED)
8
Remixing (CONTINUED)
Changing Sprites: If you want to change a sprite without changing its code, do this by adding
a new costume to the sprite. To do this, click on the sprite in the sprite menu, then click on
the “Costumes” tab, and on the “Choose a Costume” button in the bottom left. Choose the
sprite you want to replace your current sprite.
After you
pick a new
costume, you
can delete
the original
costume by
selecting the
costume and
clicking on the
trash can in
the top-right
You can also re-name your
corner of the
sprite in the sprite menu.
costume.
9
Project #1: Dungeon Maze
For our first
project, we will
build a dungeon
maze game!
You can see
(and remix) my
version here.
From events
Triggers any attached blocks when the
green flag above the stage is clicked
From events
Triggers any attached blocks when the
specified key is pressed.
From events
Triggers any attached blocks when the
backdrop is switched to the specified one
From motion
Moves the sprite the specified number of
steps (pixels)
10
Project #1: Dungeon Maze (CONTINUED)
From motion
Points the sprite in the specified direction,
where 90 degrees points to the right
From motion
Sets the rotation style of the sprite, which
determines how a sprite will rotate and flip
From motion
Send the sprite to a specific X, Y position
on the stage
From motion
Make a sprite glide to a specific X, Y
position in the specified amount of time
From control
Checks a condition from the top of the
block. If the condition is true, the blocks
inside it will activate.
From control
Repeats any inside blocks forever
From control
Stops all or some pieces of code from
running
From sensing
Checks if sprite is touching a specific color
11
Project #1: Dungeon Maze (CONTINUED)
From sensing
Checks if sprite is touching the mouse
pointer or another sprite
From operators
Checks if either condition is true
From looks
Switches to the next backdrop
From looks
Switches to the next costume
From looks
Makes a sprite invisible
From looks
Makes a sprite visible
12
Project #1: Dungeon Maze (CONTINUED)
For this project we will use the Once you choose your sprite, you can
mouse sprite as our player. delete the original cat sprite by clicking
You can change this to a on the trash can in the top-right corner
different sprite later if you like. of the sprite thumbnail. This should leave
you with only your new sprite.
Note: In Scratch, our programs will almost always start with a block from the events category.
Notice how their curved tops will not allow any blocks to be connected above them.
13
Project #1: Dungeon Maze (CONTINUED)
4. All four directions: Repeat the above block of code for the other three directions.
Note: You can copy a chunk of code by right-clicking (control+click) on the top block in
the chunk, and selecting “duplicate”.
For each direction (right, left, up, down), change the “when space key pressed” and
“point in direction 90” blocks to the correct direction. Make sure your numbers/
directions match the combinations below:
14
Project #1: Dungeon Maze (CONTINUED)
6. Title and save: On the top of the screen, add a title to your project. Then look for the
words “Save Now” next to the picture of a folder near the top-right corner of the screen.
If you do not see these words, that means your project has already been saved!
15
Project #1: Dungeon Maze (CONTINUED)
4. Save your project. In the top-left of your screen, click File > Save Now just to be sure.
• The player starts at the beginning of the maze and must walk to the end of the maze using
their arrow keys.
• If the play touches a wal, they must go back to the beginning of the maze.
• When a player reaches the end of the maze, they may continue on to the next level.
16
Project #1: Dungeon Maze (CONTINUED)
2. The Green Flag: The “when green flag clicked” block from events is often used as a start
button for Scratch games. The green flag in this block refers to the green flag just above the
stage. When the green flag above the stage is clicked, any blocks connected to the “when
green flag clicked” block will be triggered. The red stop sign next to the green flag can be
used to stop your program at any time.
17
Project #1: Dungeon Maze (CONTINUED)
4. Set starting position: We will now Press the green flag and
set the starting position of our see if your sprite goes to the
player sprite. First, drag your player beginning of the maze. If
sprite to the beginning of your not, place your player sprite
maze. This piece of code will start at the sprite of the maze,
with a “when green flag clicked” and copy the coordinates
block from events. Then, connect a from the X and Y fields on
“go to x_ y_” block and a “point in the sprite menu.
direction 90” block from motion.
18
Project #1: Dungeon Maze (CONTINUED)
To set the color we are checking This will darken the entire Now, place another “go to X_
for in the “touching color” screen except for the stage. Now Y_” and “point in direction”
block, first click the colored bring your cursor to the stage block from motion inside the
circle inside the block. When the and click on the color that you “then” portion of the “if_
color menu pops up, click the want to sense. In our case, this then_” block. Make sure the
dropper icon at the bottom. is whatever color your walls are. X and Y coordinates match the
coordinates of your starting
position. You can now add this
code to the code that sets the
starting position.
Unfortunately, the above code will not fully work yet. As written,
the above code will only check to see if we’re touching the wall
one time, at the very instant that the green flag is clicked. In order
to repeatedly check this, we need to add a loop around our “if_
then_” block. For this, we can use a “forever” block from control.
6. Try it out: Try running your mouse into a wall. Does it send your sprite back to
the beginning like it’s supposed to? Save your project here before moving on.
19
Project #1: Dungeon Maze (CONTINUED)
1. Painting more mazes: Create two more mazes using the same process that you used to
create the original one. Keep your mazes’ entrance and exit in the same place. Try to make
these ones more challenging, but not impossible!
20
Project #1: Dungeon Maze (CONTINUED)
3. Set the goal: Now we will write code that We need two things to happen when the
allows our player to pass to the next level mouse reaches the cheesy puffs: We need
when the mouse reaches the cheesy puffs. to advance to the next level, and we need
First, add another “if_ then_” block to our to send the mouse back to the beginning
“forever” loop. This time we will use the of the maze. We will reset the mouse with
“touching _” block from sensing as the the same blocks we used earlier, and add
condition. Use the drop down arrow inside the an “next backdrop” block from looks to
“touching_” block to select the cheesy puffs. advance to the next backdrop.
21
Project #1: Dungeon Maze (CONTINUED)
22
Project #1: Dungeon Maze (CONTINUED)
2. Program behavior: Program behaviors for the enemy sprite. Use the “when backdrop
switches to_ ” block from events to program different behaviors for different backdrops.
Here I am using “show” and “hide” blocks from looks to make the sprite only appear on
some levels. On levels where the cat appears, I am using “glide _ secs to x_ y_” blocks
inside of a “forever” loop to program automatic movement for the sprite. You will need to
decide where your enemy sprite moves depending on the layout of your maze!
23
Project #1: Dungeon Maze (CONTINUED)
Playtest
Now that your project is in a playable state, spend some time
playtesting it and letting others play it. Here are some questions
to guide you while you playtest:
• Does the game work the way you want it to?
• Are there any unexpected behaviors in the game?
• Is the game fun to play?
• Can you actually reach/beat every level?
• Does the game feel too easy, too hard, or just right?
• What could be added to make the game more challenging?
• What could be added to make the game more fun?
• Is there anything confusing or unnecessary that should be removed?
24
Project #2:
Don’t Drop the Donuts
For our second
project, we will
make a game
where the
player must
catch falling
objects.
You can see
(and remix) my
version here.
From motion
Changes the Y position of a sprite by the
specified amount
From motion
Sets the X position of a sprite
From motion
Sets the Y position of a sprite
25
Project #2: Don’t Drop the Donuts (CONTINUED)
From motion
Rotates a sprite by the specified amount
From motion
A sprite’s current Y position
From operators
Compares two numbers. Condition is met
if the first number is less than the second.
From operators
Picks a random number between the two
specified values.
From sensing
The current X position of the mouse
From control
Waits for the specified amount of time
From variables
Sets the value of a variable
From variables
Changes the value of a variable
26
Project #2: Don’t Drop the Donuts (CONTINUED)
1. New project: Open a new Scratch project by going to scratch.mit.edu, signing into your account,
and clicking “Create” in the top left corner. Add a player sprite and a backdrop to your project.
• For the player sprite, any animal, person, or character will work well.
• For the backdrop, something simple and not too busy will work best.
27
Project #2: Don’t Drop the Donuts (CONTINUED)
28
Project #2: Don’t Drop the Donuts (CONTINUED)
29
Project #2: Don’t Drop the Donuts (CONTINUED)
2. Reset the score: We need to make sure 3. Set the rules: Now it’s time to program the
that when the game starts, our score rules for this game. Here are the two main
is set to 0. First, return to the falling rules:
object’s code. Take a “set my variable
• The player will earn points by catching
to 0” block from variables and use the
falling objects
drop down arrow on the block to change
• The player’s score will go back to 0 when
“my variable” to “Score”. Add this block
an object is missed
to the top of the falling object’s code,
directly under the “when green flag To make the score reset to 0, we can simply
clicked” block. add another “set score to 0” block inside the
existing “if_ then_” statement.
30
Project #2: Don’t Drop the Donuts (CONTINUED)
31
Project #2: Don’t Drop the Donuts (CONTINUED)
2. Add a top score: Lastly, we are going to add a way to keep track of the top score.
Go to variables and make another new variable, this one called “Top Score”.
32
Project #2: Don’t Drop the Donuts (CONTINUED)
33
Project #3: Boss Battle
For our third
project, we will
create a version of
a video game boss
battle. My battle
will be one between
a wizard, controlled
by the player, and
an enemy fire-
breathing dragon.
You can see (and
remix) my version
here.
From motion
Rotates the sprite to point towards the
mouse pointer or a sprite
From motion
Places the sprite at a random position,
another sprite’s position, or the mouse
pointer
From looks
Sets a graphic effect for a sprite. Click the
drop down arrow to see different available
effects
34
Project #3: Boss Battle (CONTINUED)
From looks
Clears all graphic effects
From control
Triggers one of two outcomes depending on
if the given condition is met
From control
Repeats code inside loop until a condition
is met
From events
Receives broadcasted messages from
other sprites and triggers attached blocks
From events
Broadcasts a message to be received by
other sprites
From sensing
Checks if specified key is pressed
From sensing
Allows sprite to access many types of
information about other sprites or stage
35
Project #3: Boss Battle (CONTINUED)
1. Add your sprites: Open a new Scratch project by going to scratch.mit.edu, signing into
your account, and clicking “Create” in the top left corner.Add a player sprite and an enemy
sprite to your project. I am using the wizard for my player, and the dragon for my enemy.
Adjust your sprites to the size you want.
36
Project #3: Boss Battle (CONTINUED)
1. If/then Movement: In our first project, we 3. Two directions: Add another “if_ then_”
used “when key _ pressed” event blocks to loop to program movement to the left.
trigger our movement blocks. This works,
but creates movement that is not very
responsive or smooth. In this project, we
will use “if_ then_” blocks with the “key
_ pressed?” block from sensing to create
smoother movement. We will use the same
“point in direction _” blocks and “move
10 steps” blocks that we’ve used for our
movement in the past.
2. Loop it: For this code to work properly, we 4. Two directions: Add another “if_ then_”
need to put it inside a “forever” loop to loop to program movement to the left.
constantly check if the key is being pressed.
We will start with a “when green flag
clicked.”
Test your code by clicking on the green flag and using your arrow keys to move!
Make sure your project is saved.
37
Project #3: Boss Battle (CONTINUED)
This code will send our sprite to a random X,Y position near the top
of the stage every second.
2. Loop it: We will place this block in a “forever” loop to repeat it.
We will again start with a “when green flag clicked”
3. Turn and face: Add a “point towards _” block to make the enemy face our player.
Try it out! Click the green flag and watch your enemy start to fly around!
38
Project #3: Boss Battle (CONTINUED)
39
Project #3: Boss Battle (CONTINUED)
4. Adjust the position: We need to move our This extremely useful block can be used
fireball to the front of our enemy sprite. to get information about other sprites
Where we place it will be determined by or the stage. Select your enemy sprite
which way our enemy sprite is facing, which using the second dropdown, and then
we can determine using the following block “direction” using the first.
from sensing:
Now we will need to use a “_>_” block inside an “if _ then _ else _”
block to position our fireball depending on which direction the enemy
sprite is facing. Use “change x by _” and “change y by _” blocks to
place the fireball sprite where you want to. Note the use of positive
and negative values in the “change x by _” blocks.
Now the fireball stays at the front of the enemy sprite where-ever it goes.
40
Project #3: Boss Battle (CONTINUED)
41
Project #3: Boss Battle (CONTINUED)
2. Make it fly: We will start with code that 3. Next we’ll add a “wait until _” block and
is similar to what we just wrote for the some “hide”/“show” blocks to make the
lightning fireball. The lightning bolt will lighting bolts only shoot and show when
start at the player sprite, and fly upward the spacebar is pressed.
until it nears the top of the stage.
Phase 6: Health
Now we will create variables for player health
and enemy health, and program these to
interact with our attacks.
42
Project #3: Boss Battle (CONTINUED)
2. Set starting health: Go to the player 4. Program Player Health: Go to the fireball
sprite and add a “set Player Health to sprite’s code. Add an “if_ then_” block to
10” to the start of the code, underneath decrease the player’s health and then “hide”
the “when green flag clicked”. the fireball if the fireball touches the player.
Make sure you add this inside the “repeat
until” loop. Also add a “show” block to make
the fireball reappears when it starts moving.
43
Project #3: Boss Battle (CONTINUED)
5. Enemy Health: Do the same for 6. Program Enemy Health: Go to the lightning
the enemy sprite, this time setting bolt sprite’s code. Add an “if_ then_” block to
“Enemy Health” to 10. decrease the enemy’s health and then “hide”
the lighting if the bolt touches the enemy.
Make sure you add this inside the “repeat
until” loop.
44
Project #3: Boss Battle (CONTINUED)
1. Create banner sprite: We are going to create a new sprite to display either “you win”
or “game over” when the game ends. To do this, paint a new sprite, and create two
costumes for it- one for each outcome. Title the costumes “win” and “lose”.
45
Project #3: Boss Battle (CONTINUED)
2. Receive the broadcast: In both the player and enemy sprites, use a “when i receive _”
to receive the proper message and apply graphic effects, costume changes, or anything
else! Use a “wait _ secs” block and a “clear graphic effects” block from looks set your
sprite back to normal at the end of the animation.
46
Project #3: Boss Battle (CONTINUED)
Playtest
Now that your project is in a playable state, spend some time playtesting it
and letting others play it. Here are some questions to guide you while you
playtest:
47
Final Project: Build
your own Impact Game!
Now that you’ve built three games in Scratch (and from scratch!), it’s time to use your
new skills to build a game for impact.
Impact games are games that deal with real issues and encourage players to learn and
take action on important issues in the real world! These can be issues big and small.
Steps:
1. Choose your issue: What is the real world issue or challenge that you will tackle
with your impact game? It can be something relating to your local community, or
something that affects the whole world.
2. Dive into your issue: Who is impacted by your issue? What causes it to occur?
Where and when does it happen? What are possible solutions to the issue? What
information should the public know about this issue and its possible solutions?
3. Brainstorm your game: Can you think of a connection between your chosen issue
and any games you’ve played or made? Decide what type of impact game you’re
going to make. Your game can be built off of one of the games we’ve already made,
something you create from scratch, or a remix of an existing project.
4. Plan your game: What sprites are you going to need? What will each sprite do?
What backdrops will you need? What will the player’s experience be like? Make a
plan for making your project before you start programming.
5. Make your game: Get into Scratch and build your game!
6. Playtesting: Let someone else play your game and get them to give you feedback.
Look for bugs or unexpected behaviors in the game.
7. Improve: Put your playtesting feedback into action by fixing bugs and improving
your game.
48