Find the Scratch code to this project below. Feel free to follow along or access the code for yourself! 

After clicking the link above, you'll be taken to the project page. Once there, click see inside to access the code.

Create a sprite for the pen

The first thing we want to do is make a new sprite for our pen. This sprite will not only be the visual representation of our painting tool but it will also hold all the associated code.

  • In the sprite info pane, delete any current sprites. 
  • Next, to create a new sprite, hover the mouse over the cat with a plus sign. Now find and click the paint brush icon.
  • This will bring us to the sprite costume pane where we can create our very own sprite!
  • Our pen sprite is simple but feel free to make it extra fun.
  • Click the circle shape in the toolbar on the left, next disable the outline, lastly we choose the color we want for the sprite in the fill drop down. Then click and drag a small circle or whatever shape we want in the canvas.
  • Now we see our new sprite in the sprite info pane, so let's go ahead and name it.
  • The last step is to change the size of the sprite by typing in the desired size in the sprite info pane.

Adding the pen extension blocks

  • In order to access the pen blocks in scratch, we need to grab the extension.
  • Click the extension icon on the bottom left of the blocks palette.
  • Next click the pen extension to add it.

Setting up the program

  • The first thing we want to do is tell the program to start after the green flag is clicked.
  • Next we create a variable called stepSize and set it to 3. This variable will determine how fast we want the pen to follow the mouse. Then create another variable called color_counter and set this to 0. This will help us switch colors more easily.
  • Next set the pen size to 5. This is the thickness of the pen.
  • Then we tell the pen to start in the middle of the screen at 0, 0.
  • Lastly we want to erase everything that's currently on the stage before the forever loop begins.

Forever loop

  • In a forever loop, we add a point towards mouse-pointer block.
  • Next, goes a pen down block.
  • Then drag a move x steps block, dragging in the variable stepSize from the variables blocks in place of the number.
  • Now we create a series of if statements that check for the color-counter value which will tell the program when to switch colors and what color to switch to.
  • Each time the character c is typed on the keyboard, the value of color-counter increases by 1 (we will get to this next). However, with the last if statement, when the value reaches above 6, it is reset back to 1.
  • The last few blocks of code define functions for pressing keys e (erase) and c (color).
  • When e is pressed, we erase the board. In the next section, when we code the CPX, we tell it to send the character e when it is shaken.
  • When c is pressed we increase the value of color-counter by 1. Later this will get translated into: when button A is pressed on the CPX, send the character c.

Test it out!

Now that the Scratch program is finished, test out the code in the stage by clicking the green flag to start. Move the mouse around to guide the pen. Press c to change colors and e to erase the board.

This guide was first published on Jan 18, 2019. It was last updated on Jan 18, 2019.

This page (Scratch Code) was last updated on Jan 16, 2019.

Text editor powered by tinymce.