How To Use Pen On Scratch
Pen¶
The pen extension is a drawing module. It adds ix nighttime light-green blocks.
The pencil sprite¶
We are going to use the Pencil sprite. In order to make the line appear at the point of the pencil, you must go to the sprite editor, select the whole drawing (cmd+A) and identify the tip of the pencil at the origin marking.
Depict a line¶
Now we tin can place the pencil somewhere on the phase and draw a line. To do so nosotros:
- erase all
- set the pen color (red)
- set the pen size (3 points)
- put the pen down
- movement 100 steps
This is the result, fatigued on the backdrop with xy axes.
RGB colors¶
The set pen colour to () block has a drib-downwardly carte to select the color via:
- colour
- saturation
- brightness
It is also possible to provide the color with an integer variable. Most computers represent color with the 3 components:
- red
- green
- bluish
This is besides chosen the RGB color system. The three components are expressed with a byte-sized value. The intensity of each color component can go from 0 to 255.
Each color component occupies i byte and the 3 components can be combined into a 3-byte integer. When expressed as hexadecimal digit, the RGB color number has this format:
For example:
-
0xFF0000
is crimson -
0x00FF00
is light-green -
0x0000FF
is blue
We use iii variables and express each base colour at maximum intensity.
At present we tin used these variables to draw iii line segments in these base colors. - blue is 255 - greenish is 255 shifted by 8 bits (multiplied by 256) - red is 255 shifted by 16 bits (multiplied by 256*256)
This is the result:
We can likewise add two base of operations colors to go a new color.
- crimson and light-green = yellow
- cerise and blue = magenta
- blue and gren = cyan
This is the result:
Move slowly¶
The Motion category has a glide to x, y block. We tin can create a similar block for gliding a certain distance.
In Scratch a loop executes 25 times each second. To concluding t seconds, it must echo t*25
times.
The distance increment of each iteration is 1/(t*25)
of the total altitude.
To test the gliding move we write:
Draw a square¶
Now we combine the irksome motility and the irksome turn block to draw a foursquare.
This is the outcome.
If nosotros do not set the rotation way, the pen will turn by 90 degrees at each corner. That does non requite a natural animation style. It's better to not rotate the pen. Therefore at the starting time we fix the rotation fashion to don't rotate.
Depict a polygon¶
We can turn this into a function polygon which tin draw whatsoever arbitrary regular polygon.
This is the code to test for a hexagon.
This is the result drawn to the stage.
Draw a star¶
There is a simple algorithm to draw a star. Information technology's nigh like a polygon, merely it has an actress parameter one thousand.
- n : the number of points
- m : the number of points to spring over
This is the result for a (seven, iii) star which is a seven-pointed star, jumping always to the 3rd signal.
Here is the lawmaking:
https://scratch.mit.edu/projects/397107138
Store points in a listing¶
In the following section nosotros wait at a technique to store points in a list. For this nosotros need to define 3 variables:
- 10, y the 2d coordinates
- i an integer index which points to the current point in the list
Then we create a list called Points. This list contains the coordinates of all the points of the shape.
The image below shows the Points list with the points (0, 0), (100, 0) and (100, -50). They represent a rectangular triangle. The first indicate is repeated at the end to close the shape.
We define a fonction set 10, y to add the betoken (x, y) to the Points list.
The part Triangle sets the iv points of the triangle shown in a higher place.
Load and shop¶
Operations are executed using two variables 10 and y. At whatever time, the index variable i points at a pair of coordinates in the list.
The load part loads the coordinates (x, y) from the Points list to the variables.
The store role transfers the coordinates (x, y) from the variables back to the Points list.
Describe a shape¶
The depict function does the following
- set the index i to the kickoff point
- repeat for each point
- load the current bespeak to the variables x and y
- go to position (x, y)
- put the pen downwards
- increment index i by 2
Translate a shape¶
The part add adds the vector (x, y) to the point currently pointed to by the alphabetize i.
The function translate does this for every bespeak in the Points listing. It also draws the shape.
Now nosotros tin can employ the translation to the triangle.
This is the issue.
Scale a shape¶
The office mul multiplies the indicate currently pointed to past the index i with a scalar value yard. A positive bending turns counter-clockwise.
The function scale does this for every point in the Points list. It also draws the shape.
At present we tin can apply the scaling to the triangle.
This is the outcome.
Rotate a shape¶
The function rot rotates the point currently pointed to by the index i by an angle a, with reference to the origin.
The function rotate does this for every betoken in the Points list. It also draws the shape.
Now nosotros tin utilise the rotation to the triangle.
This is the result.
Move past (ten, y)¶
At that place are ii alter ten/y by blocks but no cake which changes both at the same time. Let's create such a office.
At present let'due south define 1 to depict a line by an amount (x, y).
Draw a grid¶
At present we have everything to draw a filigree.
We place the pen to the starting position. Now draw a 7x5 grid with a line distance d=20.
And this is the result.
We place the pen to another starting position, then change the color to regal and the thickness to 3. Now draw a 3xs filigree with a line altitude d=30.
And this is the consequence.
https://scratch.mit.edu/projects/398983654
Record pen motility¶
In this example we use a list to memorize the pen movenent. Clicking anywhere on the stage, starts drawing the line with the pen sprite, and also recording of the x, y coordinates.
It uses the 4 variables:
- mouse position x, y
- mouse down state (true or faux)
- list index i
At the start, nosotros set the color to crimson and the thickness to 3.
The function add point adds the electric current mouse position to the Points list.
When the stage is clicked, the message draw is circulate. This
- moves the pen to the mouse
- records the position
- puts the pen downwardly
until the mouse is upwardly.
When the Play button is pressed the recorded points are redrawn.
- reset alphabetize i to 1
- echo until the end of the list
- read the 3 variables 10, y, downwardly from the Betoken list
- to to x, y
- if downwards is true, then pen down (otherwise up)
The Button sprite uses 2 emoticons. Pay attention to leave a space subsequently the first icon, in club to brand the frame larger. On the Android OS the icons are larger as on the iOS, and the icons are cut off on the right side. If you lot don't add the extras empty space afterwards the emoticon, the icon will exist cropped.
Make the push button hight xl points. Using ceiling we can calculate the index i of the button.
- ane : delete
- 2 : play
To use keyboard shortcuts on the reckoner, nosotros add this
Try it out
https://scratch.mit.edu/projects/404148380
How To Use Pen On Scratch,
Source: https://scratch-tutorial.readthedocs.io/fr/latest/4_pen/pen.html
Posted by: moralesknoid1942.blogspot.com
0 Response to "How To Use Pen On Scratch"
Post a Comment