Shapes, Lines, and Text
From the simplest things grow the most magnificent creations
The rendering module can draw more than rectangles. In this lesson we meet the rest of the drawing functions, and we learn how colors work.
Rectangles
We already know DRAW_RECT. Here is its shape, written out. This block is only a
reference, so it cannot be run.
The x and y are the top-left corner. The rectangle is always filled in. There is no
outline option.
Circles
DRAW_CIRCLE takes the x and y of the circle’s center, then its radius, and then
its color.
This draws a circle with its center in the middle of the canvas. The radius is the distance from the center to the edge, so this circle is 100 pixels across.
A rectangle is positioned by its top-left corner, but a circle is positioned by its center. This is an easy thing to mix up.
The radius cannot be negative. A radius of 0 is allowed, and it draws nothing.
Lines
DRAW_LINE draws a line from one point to another. It takes the x and y of the
start, the x and y of the end, and then a color.
This draws an X across the canvas. Lines are always one pixel thick. When we want something thicker, we can draw a rectangle instead.
Text
DRAW_TEXT writes text onto the canvas. It takes the text, the x and y of the
text’s top-left corner, the size of the text in pixels, and a color.
The text can be a string or a number. This is handy for showing something like a score.
DRAW_TEXTis different fromDISPLAY.DISPLAYwrites to the output of the program.DRAW_TEXTwrites onto the canvas. Booleans and lists cannot be drawn as text.
Colors
Every drawing function ends with a color. A color is a string made of a # followed by
exactly six characters, like "#453465".
Those six characters are three pairs. The first pair is how much red is in the color,
the second is how much green, and the third is how much blue. Each pair is a
number from 00 (none) to ff (the most). This kind of color code is called a hex color.
Hex numbers count with sixteen digits instead of ten. After 0 to 9 they continue with
a, b, c, d, e, and f. That is why ff is the biggest two-digit value.
| color | code |
|---|---|
| red | "#ff0000" |
| green | "#00ff00" |
| blue | "#0000ff" |
| black | "#000000" |
| white | "#ffffff" |
| purple | "#453465" |
| yellow | "#ffff00" |
Mixing the three gives other colors. Full red plus full green makes yellow. Equal amounts
of all three make a shade of gray, so "#808080" is a medium gray.
The color must be exactly
#plus six hex digits. Short codes like"#fff", names like"red", and numbers are all errors.
Because a color is just a string, we can store it in a variable and reuse it.
Putting It Together
Here is a small scene that uses everything from this lesson. Remember that later shapes are drawn on top of earlier ones, so we draw the background first.
Practice
- Add a second house, or a tree made from a rectangle and a circle.
- Draw a smiley face using circles for the head and eyes and a line for the mouth.
- Change the sky to a night sky, and add stars made from small circles.
Next: Animation