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.
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 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
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.