text stringlengths 0 897 |
|---|
A> **Chapter Objectives** |
A> - Learn about the graphics display and the `gfx` module. |
A> - Read and write individual pixels on the display. |
A> - Learn to draw lines, rectangles, polygons, strings, and images. |
Today is a very special day. For the first 17 chapters of this book — and perhaps your entire programming career up till now — your programs have been built around strings of text. Strings come in, strings go out. In the last chapter we got a little fancy about how and where the strings go out, but still, it was all... |
Today that changes forever. |
As of today, you are no longer limited to creating text. Today you learn to create *graphics*: pictures composed of tiny pixels! Of course if you ran the demos in `/sys/demo`, or have played any Mini Micro games on the web, then you already know what that looks like. So let's jump right in with a short program. ... |
{caption:"Our first graphics program."} |
```miniscript |
clear |
gfx.clear color.white |
gfx.color = color.gray |
for x in range(0, 960, 64) |
gfx.line x, 0, x, 640 |
gfx.print x, x,0 |
end for |
for y in range(0, 640, 64) |
gfx.line 0, y, 960, y |
gfx.print y, 0,y |
end for |
key.get // wait for a keypress |
``` |
The first line clears the screen, and the last line waits for a keypress; you already know about these. The rest of the program uses the built-in `gfx` global, which is new. This is pronounced "graphics," and it is a reference to Mini Micro's pixel display, in the same way that `text` is a reference to the text displ... |
{width:"75%"} |
 |
When you run that program, it should draw a grid with the X (horizontal) and Y (vertical) values listed every 64 pixels. |
As you can see, the graphics display is 960 pixels across horizontally, and 640 pixels vertically. Just as with the text display, 0,0 is the lower-left corner of the screen; X values increase to the right, and Y values increase towards the top. |
In this program you used one graphics property, `gfx.color`, as well as three methods: `gfx.clear`, `gfx.line`, and `gfx.print`. There are a few more graphics properties: |
{i:"Mini Micro, `gfx` module;`gfx` module;graphics properties"} |
{caption:"Properties in the `gfx` module. `width` and `height` are read-only; the others can be assigned new values."} |
| `gfx.color` | default color for subsequent drawing | |
| `gfx.width` | width of the graphics display, in pixels | |
| `gfx.height` | height of the graphics display, in pixels | |
| `gfx.scrollX` | horizontal scroll position, in pixels | |
| `gfx.scrollY` | vertical scroll position, in pixels | |
| `gfx.scale` | scale factor, or [hScale, vScale] factors | |
EBOOK>Functions in the `gfx` module are listed below. You've already used `gfx.clear`, which has three optional parameters: color, width, and height. The color parameter (listed as `c` in the table) defaults in this case to `color.black`, while `width` and `height` default to 960 and 640, respectively. We'll get int... |
{caption:"Functions in the `gfx` module. Parameters: `points` is a list; `c` is a color; `s` and `font` are strings; `img` is an Image; all other parameters are numbers measured in pixels. All parameters have default values; in particular, color, pen size, and font parameters are usually omitted.", colWidths:"200,*"} |
| `gfx.clear c, width, height` | clear and resize the graphics display | |
| `gfx.pixel(x, y)` | get the color of pixel at *x*, row *y* | |
| `gfx.setPixel x, y, c` | set the color of pixel at *x*, row *y* to *c* | |
| `gfx.line x1, y1, x2, y2, c, penSize` | draw a line | |
| `gfx.drawRect left, bottom, width, height, c, penSize` | outline a rectangle | |
| `gfx.fillRect left, bottom, width, height, c` | fill a rectangle | |
| `gfx.drawEllipse left, bottom, width, height, c, penSize` | outline an ellipse | |
| `gfx.fillEllipse left, bottom, width, height, c` | fill an ellipse | |
| `gfx.drawPoly points, c, penSize` | outline a polygon | |
| `gfx.fillPoly points, c` | fill a polygon | |
| `gfx.print s, x, y, c, font` | draw string `s` in the given font | |
| `gfx.drawImage img, left, bottom, width, height, srcLeft, srcBottom, srcWidth, srcHeight` | draw an Image (or a rectangular portion of an image) | |
| `gfx.getImage(left, bottom, width, height)` | get a portion of the display as an Image | |
PRINT>Functions in the `gfx` module are listed on the next page. You've already used `gfx.clear`, which has three optional parameters: color, width, and height. The color parameter (listed as `c` in the table) defaults in this case to `color.black`, while `width` and `height` default to 960 and 640, respectively. We... |
## Getting and Setting Pixels |
Just as you can read or change individual cells of the text display with methods like `text.cell`, you can get and set individual pixels of the graphics display with the `gfx.pixel` and `gfx.setPixel` methods. The program listed on the next page is a quick demo that randomly changes pixels from black to gray, and from... |
There is one important trick to note on line 5 of this program. Recall that colors are represented as a string containing a hash symbol, plus two digits each for red, green, blue, and *optionally* alpha. The predefined color strings like `color.black` and `color.aqua` do not include the alpha characters (with the exc... |
{caption:"Pixel fog."} |
```miniscript |
gfx.clear |
while true |
x = 960 * rnd |
y = 640 * rnd |
c = gfx.pixel(x, y)[:7] // (ignore alpha) |
if c == color.black then |
gfx.setPixel x, y, color.gray |
else |
gfx.setPixel x, y, color.aqua |
end if |
end while |
``` |
Let's poke at this with the REPL to make sure it's clear. If the program above is still running, hit control-C to stop it, then try the following. |
```terminal |
]color.red |
#FF0000 |
]gfx.setPixel 200,200, color.red |
]gfx.pixel(200,200) |
#FF0000FF |
``` |
So `color.red` is defined as "#FF0000": full red, and zero green and blue. Alpha is not included in this string, because alpha is optional when it would be equal to FF (fully opaque). But when we store this color as a pixel and then read it back out, we get the full color string including alpha, "#FF0000FF". This is... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.