Drawing with the Pen

Within Scratch applications, drawing is performed using a virtual pen. This pen works very much like a real pen. When placed in a down position, it can be used to draw on the stage. When placed in an up position, drawing ceases. In order to draw or stop drawing, you must be able to programmatically control the pen’s up and down positions, which you can do using the code blocks shown in Figure 12.2.

Figure 12.2. Using these pen blocks, you can control when the pen can be used to draw.


Using the first code block, you can easily create a simple drawing application. To create this application, start a new Scratch project and then delete the default cat sprite and replace it with a new sprite made up of a small black dot (easily created using the Paint Editor program). Once you have created your new application as described above, select its sprite and add the following script to it:

When executed, this script clears the stage and then places Scratch’s virtual pen in a down position, enabling drawing to occur (whenever the sprite to which the script belongs is moved). Next, a loop is set up that uses a motion block to make the sprite follow the pointer around the stage. As a result, whenever you move the mouse around the stage, the sprite follows, and a line is drawn. Once you create and run your own copy of this application, it should become immediately clear that you do not have enough control over the pen. Specifically, you cannot control when and when not to draw. This situation is easily remedied by modifying the script so that you can place the pen in a down or up position based on the status of the mouse-pointer’s left-mouse button, as shown next.

Figure 12.3 shows an example of a picture drawn on the stage using this modified version of the application. By being able to control when the pen is in a down position, you can produce a precise drawing.

Figure 12.3. A quick little doodle created using a small drawing application.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.79.26