text stringlengths 0 897 |
|---|
The solution is as shown in the example above: when you don't care about alpha, just truncate the color you get back to 7 characters. |
```terminal |
]gfx.pixel(200,200)[:7] |
#FF0000 |
``` |
Now you can compare this to other 7-character color strings with no problem. |
## Drawing Lines, Rectangles, and Ellipses |
Next up, let's look at some of the figure drawing routines. All of these have an optional `color` parameter, which defaults to whatever color you set on `gfx.color`; and `penSize`, which defaults to one. Try this at the prompt: |
```terminal |
]gfx.color = color.green |
]gfx.line 200,100, 500,540 |
``` |
This sets the default drawing color to green, then draws a line from X=200, Y=0 (i.e. a point at the bottom of the screen, 200 pixels from the left edge) to X=500, Y=640 (a point at the top of the screen, 500 pixels from the left). Now use the up-arrow to recall that last command, but specify a different color: |
```terminal |
]gfx.line 200,100, 500,540, color.yellow |
``` |
The line turns yellow. Now let's try one more variation, specifying a pen size of 20: |
```terminal |
]gfx.line 200,100, 500,540, color.yellow, 20 |
``` |
The first two commands produced a very thin line, but this produces a fat, chunky line with square ends. Let's use this to make a "screen saver" that endlessly fills the screen with random lines! |
{caption:"Random-lines screen saver."} |
```miniscript |
while true |
c = color.rgb(255*rnd, 255*rnd, 255*rnd) |
pen = 1 + rnd*20 |
gfx.line 960*rnd,640*rnd, 960*rnd,640*rnd, c, pen |
end while |
``` |
Drawing the outline of a rectangle or ellipse is very similar, but the third and fourth parameters are the width and height of the figure, rather than the other end of a line. The first two parameters are the position of the bottom-left corner (since Mini Micro always measures things from the bottom left). Just keep ... |
```terminal |
]gfx.drawRect 200,100, 300,80 |
]gfx.drawRect 200,100, 300,80, color.lime |
]gfx.drawEllipse 200,100, 300,80, color.orange, 20 |
``` |
{width:"75%"} |
 |
But a rectangle or ellipse can also be filled rather than outlined. In this case there is no `penSize` parameter. Type the next one in on the REPL, line by line, to see a surprise. |
{caption:"Sometimes, when you stare at the computer long enough... the computer stares back."} |
```terminal |
]gfx.clear |
]gfx.fillRect 200,100, 400,500, color.brown |
]gfx.fillEllipse 250,400, 150,50, color.white |
]gfx.fillEllipse 400,400, 150,50, color.white |
]gfx.fillEllipse 455,410, 40,40, color.blue |
]gfx.fillEllipse 305,410, 40,40, color.blue |
]gfx.fillRect 300,250, 200,20, color.red |
]text.clear |
``` |
If your eyes glaze over when you see so many numbers, don't worry. It's actually not very common to write code like this; usually if you need a picture, you would just load a picture from disk (as we'll see later in this chapter). More often when you're using drawing commands like these, the coordinates are computed ... |
## Drawing Polygons |
{i:"polygons"} |
Mini Micro can also draw polygons of three or more sides, and by "draw" we mean either outline or fill, just like rectangles and ellipses. But polygons are a little trickier to specify, since they can include any number of points. Mini Micro solves this by using a list of points. Each element of the list can be itse... |
```terminal |
]gfx.clear |
]gfx.drawPoly [[100,100], [200,300], [300,100]] |
]gfx.drawPoly [[100,100], [200,300], [300,100]], color.red |
]gfx.drawPoly [[100,100], [200,300], [300,100]], color.red, 10 |
]gfx.fillPoly [[100,100], [200,300], [300,100]], color.blue |
``` |
As an alternative, each point can be a little map containing "x" and "y" values. This is mostly useful when working with other things that already have coordinates in this form, like the mouse pointer or sprites (all things we'll get to later). For now, let's just prove that it works: |
```terminal |
]clear |
]gfx.drawPoly [{"x":100,"y":100}, {"x":200,"y":300}, {"x":300,"y":100}] |
``` |
D> Note that this line of code does not quite fit on one line on the screen. That's OK; just keep typing, and don't let it worry you when it wraps to the next line! |
## Drawing Text |
Sure, you already know how to print text to the text display. But it turns out you can also draw text to the graphics display, and it's more flexible in two ways. First, you can draw the text anywhere you like, rather than only in predefined rows and columns. Second, you can draw it in a smaller or larger font, in a... |
```terminal |
]@gfx.print |
FUNCTION(self, str, x=0, y=0, color, fontName="normal") |
]gfx.print "Hello world!", 600, 400 |
]gfx.print "Hello world!", 600, 350, color.aqua |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.