How to draw a Snowflake using Code-Blocks in Scratch
By: Azfar Bakht
Tags: Scratch, Kids Programming, Christmas, Learning To Code, Scratch Programming

Scratch is a block-based visual programming language and website targeted primarily at children 8–16 to help learn code.

As Christmas draws closer and we indulge ourselves in the wonderful holiday spirits, now might be an excellent time to learn how to code with a fun and simple exercise of drawing a snowflake using code!

Just by making small adjustments to our code, we can draw one or many snowflakes of varying size. In fact it can automatically resize itself based on your choice!

This article assumes no prior knowledge of Scratch, however if you know your way around it and wish to get straight to the snowflake part, feel free to skip ahead!

A link to the complete project has been added at the end of this article.

What is Scratch and how to get started?

Scratch is a block-based visual programming language and website targeted primarily at children 8–16 to help learn code. It was developed by MIT and is free to use for both students and educators.

To create a new scratch project, go to their website here and click on “Create” at the top left.

Once you’ve clicked on create, you will see a screen such as the one I have added below. I am encircling and labeling various parts of the screen and will explain them to you in order.

A new project in Scratch

Let’s take a quick look at the individual components

The Navigation bar allows you to save your project. You may also load projects from your computer. You can name your project and share it from here as well. This portion of the screen allows you to select different blocks of code. They have been split up into different colors and shapes on the basis of their functionality. I could explain all of the different types here but the best way to learn about them is by experimenting through code! This portion of the screen is the central playground where you will drag and drop the blocks of code for them to be functional. This portion of the screen is what I like to call the Sprite Selector. All of the different sprites that you add to your project will be displayed here along with options to set their size, location and visibility etc. This is the main game screen. All the visible sprites and all the changes you make to your code will be displayed over here. The last of our screen divisions. The left button with a cat drawn on it will allow you to choose from the many available sprites that Scratch has to offer. The button on the right does the same for Backdrops. In addition selecting Scratch assets, you can even upload or draw them on your own!

Now that formalities are out of the way, let's get to the fun part!

Let’s begin our Code!

Take a look the snowflake we’re about to create.

Snowflake drawn using Scratch

In order understand how the snowflake is drawn, we must divide our problem into smaller parts. For that lets learn to create one out of the eight lines. Once we have understood how that works we can just recreate the code and create a perfectly symmetrical snowflake!

Creating a single side

Lets start by adding the pen extension. You can do that by clicking on the blue button at the bottom left of the screen and then selecting the pen extension. Once the pen is added, add the following blocks to draw a single side

The set pen size block is used to set the thickness of our drawing lines. Setting it to 5 means that it will have a width of 5 pixels. When we use the pen down block, we allow our sprite to begin drawing alongside its movements.

The sprite moves by 70 pixels and draws the backbone of our side, the 45 degree anti-clockwise turn is for our first branch. Followed by two blocks with positive 30 and negative 30 movements. This just means the sprite will go forward and then come back to its original position. We the turn clockwise block with a value of 90 degrees to finish up the opposite side of our branching out. We repeat the same steps but this time the backbone is kept at 30 instead of 70 and the diagonal movements are by 20 pixels back and forth, instead of 30.

Earlier in this article, we talked about how the code would be generic, it would draw a snowflake for any given size. Well in order to incorporate that, we must first learn about blocks.

In order to get started with Custom Blocks, go to the block selection area at the left side of your screen. Click on “My Blocks” and select “Make a Block”

When you click on make a block, you will see something similar to the above diagram on your screen. You can write in the rectangle field to name your block and click on “Add an input” to make your block accept external values. Once you’re ready, check the “Run without screen refresh” block and click “OK.” Now if you head on over to the “My Blocks” part on your screen, you will notice something different.

The code for single side after turning it into a custom block

Creating the complete Snowflake!

Now that we have successfully created a block for drawing a single side, we can use it to complete our snow flake. Once again we will create a custom block by heading on over to the my blocks section and then attempting the following code.

The code for drawing a snowflake using the Draw Side custom block we defined earlier

Now that we have both our custom blocks ready to use, let’s set the backdrop color to dark blue finalize our code.

1. Click on the area highlighted

2. Click on Backdrops

3. Click on Fill and select the following values by dragging the sliders.

4. Use the rectangle tool and draw a large rectangle over screen.

5. Click on the Code tab again

Once you have done the above steps, click on sprite1 in the sprite selector section of your screen and construct the following code.

Final code to draw the snowflake

Instead of 10, try any other number that you like and you’ll see some results. But beware, if the size of the snowflake exceeds the boundaries of the screen, our drawing might get distorted.