- This article is about the feature. For the extension, see Pen Extension.
erase all stamp pen down pen up set pen color to (#FFFFFF) change pen (color v) by () set pen (color v) to () change pen size by () set pen size to ()
The pen is a feature in Scratch that allows a sprite to draw shapes, plot colored pixels, and so forth on the screen with the pen blocks. Pen was introduced in Scratch 1.0. The pen is derived from Logo's turtle graphics, as are the motion blocks with which the pen is used.
Lines, dots, rectangles, and circles are the easiest shapes to draw, but with enough scripting, any shape can be created.
Pen Blocks
- Main article: Pen Extension
erase all
— Clears all pen marks on the screenstamp
— Draws a copy of the Sprite on the stagepen down
— Turns the pen feature on inside a sprite; the sprite will leave pen marks on the screen wherever it moves until the pen is turned offpen up
— Turns the pen feature off, stopping a sprite from penningset pen color to (#ffffff)
— Sets the color of the pen to a predetermined colorchange pen ( v) by ()
— Changes a property of the pen by a given inputset pen ( v) to ()
— Sets a property of the pen to a given inputchange pen size by ()
— Changes the size of the pen by a chosen numberset pen size to ()
— Sets the size of the pen to a chosen number
Uses
The pen blocks are often used for:
- Making Snake style games
- Making a trail behind a sprite
- Animating
- Drawing objects in one sprite, one script projects
- Drawing patterns
- Creating graphs
- Program graphics editors
- Creating the platform in a Platformer game
- 3D projects
- Text Rendering
Transparent Pen
Pen Transparency can be changed using the set pen (transparency v) to ()
block. Alternatively, it is possible to use a RGBA color as an input to the Set Pen Color to () block, as described below.
Formula
The formula for ARGB is similar to that of RGB (red, green, and blue), but it has an additional value, Alpha (A) for opacity. Alpha is identified by a number 1-255, where 1 is completely transparent and 255 is opaque. The formula for RGB (without Alpha) is:
set pen color to ((((R) * (65536)) + ((G) * (256))) + (B)) // Be sure to use the pen color block with the color input (like the one below), or the colors will not function properly. set pen color to [#0000FF]
And the formula for ARGB is:
set pen color to ((((A) * (16777216)) + ((R) * (65536))) + (((G) * (256)) + (B)))
Caution: | Make sure the A, R, G, and B values are whole numbers (rounded) so they don't contribute to other colors' values. If any of these might be fractions, use the following code instead: |
set pen color to ((((round (A)) * (16777216)) + ((round (R)) * (65536))) + (((round (G)) * (256)) + (round (B))))
Use Of "Run Without Screen Refresh" Custom Block
- Main article: Render Script
Run without screen refresh is a custom block operation that is used to speed up actions by refreshing the screen only after the operation has finished. When you create a custom block, select the checkbox labelled "run without screen refresh". A script that could use the custom block to draw a picture may look like this:
define draw set [repeater v] to (0) // Our variable keeps track of which item to pick on the color list set pen size to (2) // Set the size to 2 to avoid a Flash Player glitch go to x: (-240) y: (180) repeat (360) // Repeat y repeat (480) // Repeat x change [repeater v] by (1) // Change our variable by 1 set pen (color v) to (item (repeater) of [list v]) // Set the pen color to what it's meant to be from the list pen down pen up change x by (1) end set x to (-240) // Back at the far left of the screen change y by (-1) end
or as a variable:
define draw set [repeater v] to (0) // Our variable keeps track of which item to pick on the color list set pen size to (2) // Set the size to 2 to avoid a Flash Player glitch go to x: (-240) y: (180) // Starting pos repeat (360) // Repeat y repeat (480) // Repeat x change [repeater v] by (3) // Change our variable by 3 (length of 200, the max color) set [temporary v] to [] // A temporary variable creating the color from the variable set [temporary v] to (letter ((repeater) - (2)) of (variable) set [temporary v] to (join (temporary) (letter ((repeater) - (1)) of (variable)) set [temporary v] to (join (temporary) (letter (repeater) of (variable)) set pen (color v) to (temporary) // Set the pen color to what it's meant to be from the variable pen down pen up change x by (1) end set x to (-240) // Back at the far left of the screen change y by (-1) end
Of course you are going to have to make a list or variable to contain all the colors, and it's going to have to be 172,800 colors long. However, lower resolution images can be created by tweaking the scripts, and it will require less items for the list. And a few scripts may create an image editor that can save the image you have drawn to a list.
See Also
External Links
This section has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet, as we cannot guarantee the safety of other websites. |