Basics of Scratch Coding

by Bobthemaker in Teachers > Coding

769 Views, 6 Favorites, 0 Comments

Basics of Scratch Coding

Screenshot 2021-12-04 3.42.39 PM.png
Screenshot 2021-12-04 11.50.28 AM.png

Scratch is one of the most popular and easy to learn coding languages. You can create amazing things with it . So, I will be making a tutorial based around the basics.

It will cover all of the basic blocks *and teach you how to make a simple game!

You can use this in Hour Of Code or just as a general intro to Scratch.

*note that this overview could be very boring

Motion in Scratch

Screenshot 2021-12-04 11.53.13 AM.png
Screenshot 2021-12-04 11.55.58 AM.png

The blue blocks you see in this image are motion blocks. They will move your sprite ( character) in your program. They can move it in any direction and can randomize where it goes. To get coding, pull it out to the blank space in between your sprite and the blocks.

Looks in Scratch

Screenshot 2021-12-04 11.56.46 AM.png

The light purple blocks that you see here are what control the looks of your sprite. The can change it's costumes to another one and you can scroll down to get more of them.

Sounds in Scratch

Screenshot 2021-12-04 11.59.44 AM.png
Screenshot 2021-12-04 12.01.15 PM.png

These light pink blocks that you see here are what control the sounds that your sprite makes . They are either in the sound library or can be recorded by you to customize them. You can add up to 50 MB (megabytes) of sounds in a single project.

Events in Scratch

Screenshot 2021-12-04 12.03.14 PM.png

Sorry if the overview is so long, there are so many things to cover!

The yellow blocks in the image above are event blocks. They make things happen when you do something like press a key or click on something. They are used to start a piece of scratch code so they are very important.

Control Blocks

Screenshot 2021-12-04 12.06.23 PM.png

Next up, the orange blocks that you see are Control Blocks. They can be used to loop something and are crucial in "if then" statements which are also important pieces of code.

Sensing in Scratch

Screenshot 2021-12-04 3.39.47 PM.png

The light blue blocks that are shown in this picture are sensing blocks. These blocks can be used to make the sprite detect something. They can detect colors , motion, if you clicked, and much more.

Operations in Scratch

Screenshot 2021-12-04 3.42.06 PM.png

These light green blocks are for doing mathmatical operations in Scratch. They can be used to see if you have enough items in a game to buy something and they can randomly generate numbers. They can also be used in more complex ways such as trigenometry in very advanced games.

Variables in Scratch

Screenshot 2021-12-04 3.47.01 PM.png

These orange colored blocks are variable blocks. They can be used to create a object amount (money ) for example. To create new variables, click the "make a variable" button and you can name it to be avalible to all sprites or just that one.

You can make a list by clicking the "make a list " button and making a list of those playing the game with a Scratch account or for other things.

Extensions to Scratch

Screenshot 2021-12-04 3.53.59 PM.png

If you want to add extensions to your project, click the blocks with a plus at the bottom left corner and you can add a variety of extensions to Scratch. Motion*, Legos,* Pen tool, The sky's the limit to what you can add!

*make sure your device or you have what you need for an extension

Backdrops and Sprites

Screenshot 2021-12-04 3.57.46 PM.png

In this photo, you can see 3 different things. There is the "Sprite1 " , the cat head outline with a + , and the picture with mountains with +. The Sprite1 is telling you that that's the 1st sprite out of however many you have. The cat head outline with + , when you click on it will give you options for sprites or by hovering over it, you have options to paint a sprite, upload a sprite, or mystery sprite (randomly generated). The backdrop has the same options.

Costume Editor+sound Library

Screenshot 2021-12-04 4.04.06 PM.png

When you click the "costume" tab on the top left corner, you will be brought to the editor. You can paint a costume , delete a costume, rotate,or much more.

The sound library has many different unique and wacky sounds to choose from.

Start Designing the Game!

Screenshot 2021-12-04 4.19.53 PM.png

Congratulations if you made it this far! (I hope you're not asleep)

We will be designing a 2 player car racing game.

There will be a track, 2 cars, and a "out of bounds" that you can't get to.

See my example in this link if you need inspiration https://scratch.mit.edu/projects/568139340

Making the Track.

Screenshot 2021-12-04 4.22.56 PM.png
Screenshot 2021-12-04 4.25.10 PM.png
Screenshot 2021-12-04 4.27.37 PM.png

Start by deleting the cat sprite by clicking on the trash can button when you click the small image. Next, click the Backdrops and Paint. You should then pull out a green rectangle that covers the entire background, and shade it to whatever color green looks right to you.

Then, click the brush icon and set the brush size to 70 and set the color to a grey of your choice. Then, draw a track shape.

When you're done, it should look something like the 3rd image

The 2 Cars

Screenshot 2021-12-04 5.16.21 PM.png
Screenshot 2021-12-04 5.18.53 PM.png

Make 2 sprites and paint them both to look like a car from the top. Be as detailed as you want . When you're done, it should look something like the image above. Make 2 of them and line them up to where you want the starting line to be.

Track Details

Screenshot 2021-12-04 5.38.50 PM.png

Make 2 lines for where you want the checkpoint(s) and the starting line on the track. Make them both different colors and put them far away from each other. ( the checkpoint(s) are for anticheating).

Actual Code Part 1

Screenshot 2021-12-04 5.58.42 PM.png
Screenshot 2021-12-04 7.46.16 PM.png
Screenshot 2021-12-05 8.03.05 PM.png
Screenshot 2021-12-05 8.02.55 PM.png
Screenshot 2021-12-05 8.05.52 PM.png
Screenshot 2021-12-15 6.44.40 AM.png

First, make 2 variables. Car 1 checkpoint and Car 1 laps. These will count the checkpoints to prove you actually did a lap and how many you did. Starting with car 1, we need to control it with keys WASD. (W is forward,s is backward, a is left turn and d is right turn.) Copy down* the code in the image above to get your basic controls and checkpoint system for car 1 . Sorry about the code pics, some of them are just too long :)


* ( Note that you will want to change the colors to your color starting line and checkpoint. You can do this by clicking on the color and the dropper . Then you can select your color.)

Code Part 2

Screenshot 2021-12-04 7.58.58 PM.png
Screenshot 2021-12-04 5.58.42 PM.png
Screenshot 2021-12-04 7.46.16 PM.png
Screenshot 2021-12-05 8.02.55 PM.png
Screenshot 2021-12-05 8.03.05 PM.png

Next, make 2 more variables. Car 2 laps and Car 2 checkpoint. Repeat what you did with car 1 but change the wasd to arrow keys and replace Car 1 variables to Car 2 variables. A way to quickly copy the code is to put it in your backpack by first clicking "backpack" and dragging the code into it. Don't worry about deleting the code, it will make a copy for you to pull out on the code section .

Saving Your Project (If You Don't Have a Scratch Account)

Click file on the top left corner and then click save now. That's how to save your project!

Why the Code Works

Now you're probably wondering:"Why does this code work? " Well, first we have to know what each code block does. The code with the movement blocks, (the move 2 steps block) allows you to move your sprite by making it so that if you click any of the arrow keys or w a s d , it will move a car.

The small code with your grass color on it is so people don't cheat and go on the grass from the checkpoint to the start . Instead, they get teleported to the starting line as an additional challenge

The piece of code with the 2 if then statements in it say that you have to have proof that you got to the checkpoint otherwise you cannot complete the lap.

That's how the code works!

Once you're done, great job! You've had the patience to sit through this (very boring and lengthy) Instructable so now, eat something and take a break. You've deserved it.