This article is about the pen feature. For pen blocks, see Pen Extension.
Pen Blocks.

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

Blocks Related to Pen

Main article: Pen Extension


  • Erase All — Clears all pen marks on the screen
  • Pen Down — Turns the pen feature on inside a sprite; the sprite will pen on the screen wherever it moves until the pen is turned off
  • Pen Up — Turns the pen feature off, stopping a sprite from penning
  • Set Pen Color to () — Sets the color of the pen to a predetermined color
  • Change Pen () by () — Change a property of the pen color
  • Set Pen () to () — Set a property of the pen color
  • Change Pen Size by () — Changes the size of the pen by a chosen number
  • Set Pen Size to () — Sets the size of the pen to a chosen number
  • Stamp — Draws a copy of the Sprite on the Stage

Uses

The pen blocks are often used for:

Transparent Pen

Transparent pen is a feature in Scratch 2.0 added in v435.2[1] that allows the transparency of the pen.

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)))
Note Warning: 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 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 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

SandCastleIcon.png This section has links to websites or programs not trusted by Scratch or hosted by Wikipedia. Remember to stay safe while using the Internet, as we can't guarantee the safety of other websites.

References

  1. https://scratch.mit.edu/projects/60596326/