text stringlengths 0 897 |
|---|
yield // wait till next display frame |
end while |
display(0).color = color.rgba(0,0,0,alpha1) |
end function |
// fade to black over `duration` seconds |
fadeOut = function(duration=1) |
fade 0, 255, duration |
end function |
// fade from black to clear over `duration` seconds |
fadeIn = function(duration=1) |
fade 255, 0, duration |
display(0).mode = displayMode.off |
end function |
``` |
Notice how the `fadeOut` and `fadeIn` functions do very little work themselves, instead calling a `fade` function that does all the heavy lifting. This follows the DRY (Don't Repeat Yourself) principle from Chapter 11; without this helper function, `fadeIn` and `fadeOut` would be almost identical. |
After running the above code to define the functions, try them out with: |
```terminal |
]fadeOut; key.get; fadeIn |
``` |
Neat, huh? You can use these functions any time you need a dramatic fade to black in your own programs. |
## Reading the Mouse |
Let's turn now to a different topic: detecting where the mouse cursor is, and the state of the buttons. This is a very useful thing to do, since it lets us go beyond keyboard input and make programs that can be controlled with a mouse (or track pad or touch screen!). |
{i:"`mouse` module;Mini Micro, `mouse` module"} |
This is done in Mini Micro with a very simple built-in module called `mouse`. It has two read-only properties, one read/write property, and one function: |
{i:"Mini Micro, `gfx` module;`gfx` module;graphics properties"} |
{caption:"Properties and function in the `mouse` module. `x` and `y` are read-only; `visible` can be assigned true or false."} |
| `mouse.x` | horizontal position of the mouse pointer (0-959) | |
| `mouse.y` | vertical position of the mouse pointer (0-639) | |
| `mouse.visible` | true if mouse cursor can be seen; false if hidden | |
| `mouse.button(which=0)` | true when the given button is pressed | |
To start exploring the `mouse` module, position the mouse somewhere over the Mini Micro screen, and simply enter `mouse` at the prompt. You'll see something like this: |
```terminal |
]mouse |
{"x": 417, "y": 166, "button": FUNCTION(which=0), "visible": FUNCTIO |
N()} |
``` |
Note the `x` and `y` values shown — these vary with the mouse position. Try moving the mouse, and then entering `mouse` again. In fact, on some systems, you can even read the mouse position beyond the bounds of the screen; for example, if you move the mouse to the left of the Mini Micro window, you'll see a negative ... |
Next, try setting `mouse.visible = false` at the prompt, and observe that the mouse pointer disappears. This is useful for some kinds of games, where you might want to use a sprite (coming up in Chapter 22) in place of the standard mouse cursor. Set `mouse.visible = true` to bring it back. |
Finally, let's look at the `button` method. A gaming mouse can have up to seven buttons, numbered 0 through 6. A more typical mouse has two buttons, with the main button as number 0 and the secondary or right-click button as number 1. Often the scroll wheel can be pushed, which reads as button 3. |
When you don't supply the `which` parameter to `mouse.button`, it reports the state of button 0. So try it: enter `mouse.button` at the prompt, without touching the mouse, and it should report 0, meaning that the button is not pressed. Then enter it again (remember the up-arrow trick!), but this time, hold down the ... |
Now that you can get the mouse position and button state, you can do all sorts of neat things. Try this simple drawing program: |
{caption:"A simple drawing program in Mini Micro."} |
```miniscript |
clear |
gfx.color = color.white |
prev = {} |
while not key.pressed("escape") |
// The mouse might move within this loop, so let's grab a copy |
// of it right away as "m", and use only that. |
m = {"x": mouse.x, "y": mouse.y} |
// If the button is down, draw a line from |
// our previous mouse position. |
if mouse.button then gfx.line prev.x, prev.y, m.x, m.y |
// ...and then remember that as the previous position next time. |
prev = m |
yield // wait for next frame |
end while |
key.clear |
``` |
Run that code, and then try drawing with the mouse. Can you draw a house? Can you use the mouse to draw a mouse? (Fortunately this is not a *Learn to Draw* book, so random scribbles are also perfectly fine.) |
## Importing prewritten code |
{i:"`import`;Mini Micro, `import`"} |
As the last topic for this chapter, we're going to learn about the `import` command. This very useful function loads a file of MiniScript code stored somewhere else on disk. For example: |
{i:"`/sys/lib`,`chars`"} |
```terminal |
]import "chars" |
]print chars.heart |
♥ |
``` |
{i:"`/sys/lib`;`/usr/lib`"} |
The first line above tells Mini Micro to search for a MiniScript source file called `chars.ms` in the current directory, `/sys/lib`, or `/usr/lib`. In computers, "lib" stands for "library," and refers to a place to find reusable code that may be useful to a variety of programs. (You can also define other folders to c... |
```terminal |
]cd "/sys/lib" |
]dir |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.