1. Make a sprite that draws random polygons

  • Delete the default Cat sprite and add the Pencil sprite by clicking “Choose Sprite from Library” in the Sprite List.
  • Click on the “Data” section of the Palette and then click the “Make a Variable” button.
    1. Name your variable “SideLength”.
    2. Click OK to continue.
    3. Make one more variable and name it “NumberOfSides”.
  • Now, add the code blocks below to make the Pencil draw random polygons on the screen each time you click on the Stage:

2. Make your own custom block that displays the polygon’s name

  • Click on the “More Blocks” Palette and then click the “Make A Block” button.
  • Type “sayShapeName” in the field to name the block.
  • Click the “Options” text dropdown.
    1. Click “Add a Number Input”.
    2. Type the word “sides” to replace “number1”.
    3. Click “OK” to continue.

You’ve made a block, but . . . what does it do? Nothing yet! You’ve got to define what it does before it will be usable.

  • Add code blocks to your new, purple “define: sayShapeName” block so that it says the name of the shape. For example: if sides = 3 then say “Triangle” for 1 second. Make a series of if/then statements for each of the different polygon names:
    • 3 sides: triangle
    • 4 sides: square
    • 5 sides: pentagon
    • 6 sides: hexagon
    • 7 sides: heptagon
    • 8 sides: octagon
    • 9 sides: nonagon
  • Now add your new “sayShapeName” code block to the end of your first code sequence — after your “Point In Direction” block as shown below.
  • Plug the “NumberOfSides” variable into its field.
  • Test your program.

3. Fractals

Fractals are patterns that repeat themselves at smaller scales over and over to infinity and often simulate patterns that occur in nature.

  1. Start a new Scratch project. Replace the default cat sprite with your choice of sprite from the library that will do the drawing and set it to a suitably small scale.
  2. Enter the following code:
  3. Notice that the tree function (aka. more block) calls itself. A function that calls itself is called a recursive function. The tree function uses itself to make more and more smaller branches. It repeats the pattern over and over at smaller scales.
  4. When you run your script it should draw something like this:
  5. Play around with the code and values used to see how it effects the outcome.
  6. You may notice that it takes some time to draw because it has to repeat the pattern so many times. Try the ‘run without screen refresh’ option and notice the difference:

4. More Fractals – Sierpinski Triangles


Sierpinski Triangles are fractal patterns formed with triangles. They are named after a Polish mathematician Wacław Sierpiński, but appeared as a decorative pattern many centuries prior to the work of Sierpiński.

Basic setup and variable creation

  1. Start a new Scratch project. Replace the default cat sprite with your choice of sprite from the library that will do the drawing.
  2. Click on the “Data” section of the Palette and then click the “Make A Variable” button and name your variable “Size”.
  3. Click OK to continue.
  4. Make three more variables and name them “Levels”, “CenteredX”, and “CenteredY”.

Make three custom blocks for drawing fractal art.

The “Make A Block” feature allows you create custom procedures that you can reuse in your Scratch projects. We’ll need three custom blocks for this project. Here’s how to make a custom “triangle” block:

  • Click on the “More Blocks” palette and then click the “Make A Block” button.
  • Type “triangle” in the field to name the block.
  • Click the “Options” text drop-down arrow.
  • Click “Add A Number Input”.
  • Type “sideLength” to replace “number1”. This number will set the side length of each triangle drawn. Programmers call these input definitions “parameters”.
  • Click OK to continue.
  • Add the code blocks below to your new “triangle” procedure:
  • Make a second block named “Sierpinski”.
  • Click the “Options” text drop-down.
  • This time, make two number inputs.
  • Name one “sideLength” and the other “levelOfRecursions”.
  • Click OK to continue.
  • Add the code blocks below to your new “Sierpiński” procedure.

How It Works

We’ve just defined a new procedure named Sierpiński, and it takes two inputs called “sideLength” and “levelOfRecursions”. (We’ll prompt the user for these two numbers when the program is run.) Basically, this function uses an algorithm to draw an intricate pattern of self-similar triangles named after Wacław Sierpiński, the Polish mathematician who first described it in 1915. You’ll also notice something unusual: there is a Sierpiński block inside the definition of the Sierpiński procedure itself. Just as in the previous fractals project, this technique is called recursion.

  • We typically need to control recursive procedures with a conditional statement, in this case an if-then/else block.
  • Here we use the conditional test to determine whether or not the recursive call should be made. If it evaluates to “true”, then we will execute the “triangle” block and draw a triangle.
  • However, if the test evaluates to “false”, we instead execute the code inside the else part of the conditional. There, a recursive call to Sierpiński is made and the “levelOfRecursions” parameter is decremented by one.
  • The “Move” and “Turn” blocks direct the Sprite to the next appropriate position before drawing another triangle.
  • Eventually, “levelOfRecursions” will reach zero, the recursion will stop, and the smallest instance of the triangle will be drawn on the Stage.
  • The resulting final output is one large triangle comprised of smaller, self-similar triangles.

We can follow the flowchart below to demonstrate how Sierpiński works when it is called with a “levelOfRecursions” argument of 1:

The procedure starts with user input of 1 level of recursion and a largest triangle side length of 300 pixels. The procedure first checks if “levelOfRecursions” is equal to 0. Since 1 is not equal to 0, the procedure subtracts 1 from “levelOfRecursions” and calls itself with an argument of 0. The “Move” and “Turn” blocks are not yet reached and nothing is drawn on the Stage.

In the second call, “levelOfRecursions” does equal 0, executing a call to the “triangle” procedure, which draws a triangle with a side length of 150 pixels. The procedure returns to the “Move” and “Turn” blocks from the first call, moves 300 pixels, and turns 120 degrees. This process is repeated three times total as required by the “Repeat” loop inside the “Else” statement, drawing two more triangles of 150 pixels at different positions on the Stage.

Make a “centerTheDrawing” block.

  • Make a third block named “centerTheDrawing”.
  • Click the “Options” text drop-down.
  • Click “Add a Number Input”.
  • Type “sideLength” to replace “number1”.
  • Add the code blocks below to your new “centerTheDrawing” procedure:

How It Works

  • The next custom procedure centers the entire drawing on the Stage by calculating the appropriate starting position for the Sprite based on the user’s desired size input. The Sierpiński procedure draws the fractal in a spiraling, counter-clockwise pattern from the bottom left corner of the largest triangle.
  • The Stage is a four-quadrant coordinate grid with the origin (0, 0) in the center. The X limits are 240 and -240, and the Y limits are 180 and -180.
  • So if we know the Stage has a center point of X = 0, then to center the triangle horizontally drawing should start at centeredX = “0 – sideLength / 2”. We’re considering the base of this triangle to be its width.
  • To find the vertical center, we first need to find the height of the equilateral triangle because it’s not the same as the length of its sides. That formula is height = (√ 3  / 2) sideLength. Now that we know its height, we can center the triangle drawing vertically by calculating centeredY = “0 – height / 2”

Write the main code sequence below to make the bug draw fractal triangles based on user input.


  • The first six blocks in our main sequence size, position, and orient our Sprite then clear the Stage of any previous drawings.
  • The “Ask () And Wait” block, found in the “Sensing” Palette, prompts the user to input a number up to 300. This number will be the size of the largest triangle in pixels. The user’s input will be stored in the “Answer” block.
  • We use a conditional statement to test if the user input a valid size number. With any bigger than 300, the drawing won’t fit neatly on the Stage.
    • If the user inputs a number greater than 300, then we set the “Size” variable to 300 anyway, and use the “Think” block to give the user some feedback about the limit.
    • Else the user has input a size up to 300, and we set the “Size” variable to the user’s input.
  • Next we “Ask” the user how many levels of recursion they would like to see in their fractal drawing.
  • We use a conditional statement to test if the user input a number of 6 or less. Due to the resolution of the Stage, we wouldn’t be able to see more than six recursions in our Sierpiński Triangle drawing anyway.
    • If the user inputs a number greater than 300, then we set the “Level” variable to 6 anyway, and use the “Think” block to give the user some feedback about the limit.
    • Else the user has input a valid number, and we set the “Level” variable to the user’s input.
  • Now that “Size” and “Level” variables are set, we pass those values to our “centerTheDrawing” and Sierpiński procedures and execute them.
  • Inserting the “Pen Down” block in between our custom procedure calls ensures that we start drawing at the right time.

Sit back and enjoy some eye-catching, animated mathematical art!

Extra Challenge: Can you modify your project to make the Sierpinski triangles redraw over and over again at different variations of size, colour, etc? Maybe even set it to music! For example, something like this:

Remember to add your projects to the Shapes & Fractals Scratch Studio