text stringlengths 0 897 |
|---|
It does not, however, cause the wumpus to appear on screen. Right now our sprite object is just hanging out in memory, not attached to any display. To make it appear, we need to add it to a sprite display. In the default configuration, display 4 is a sprite display, so we can do: |
```miniscript |
display(4).sprites.push spr |
``` |
You should see a wumpus peeking out from the lower-left corner of the screen. To bring it more into view, assign to its `x` and `y` properties: |
```miniscript |
spr.x = 480 |
spr.y = 320 |
``` |
That will move the sprite into the center of the screen. |
If you don't see the sprite, then the most likely cause is that you made a typo in the file path. You could print out `spr.image` and if it is null, repeat the `file.loadImage` line above, checking your typing and capitalization very carefully. |
{i:"`Sprite` class"} |
PRINT>So far, you've seen the `image`, `x`, and `y` properties of the `Sprite` class. The full set is presented in the table at the top of the next page. We'll cover the rest before the chapter is done. |
EBOOK>So far, you've seen the `image`, `x`, and `y` properties of the `Sprite` class. The full set is presented in the table below. We'll cover the rest before the chapter is done. |
{gap:20} |
{caption:"Properties and methods for the Sprite class (where `spr` is any Sprite object). The last three rows are functions; all the others are properties which may be read or assigned."} |
|`spr.image`|image to use for the sprite's appearance| |
|`spr.x`|horizontal position of the center of the sprite in the display| |
|`spr.y`|vertical position of the center of the sprite in the display| |
|`spr.scale`|scale factor: 1 is normal size, 0.5 is half size, etc.| |
|`spr.rotation`|sprite rotation angle in degrees| |
|`spr.tint`|tint color (white for no tint)| |
|`spr.localBounds`|defines rectangular bounds relative to the sprite itself| |
|`spr.worldBounds`|get the bounds of the sprite on screen| |
|`contains`(*pt*)|return true if the sprite bounds contains the given x/y point| |
|`overlaps`(*other*)|return true if the sprite is touching other sprite or Bounds| |
## Animating Sprites |
Sprites excel at animation! And to do it is simple: you only need to change one or more of the sprite's properties (like position, scale, and rotation) from frame to frame. Let's first try a simple animation of our sprite's `rotation` property. |
```miniscript |
for ang in range(0,360) |
spr.rotation = ang |
yield |
end for |
``` |
{i:"`yield`"} |
You should see the wumpus slowly spin counter-clockwise. The `yield` call here makes the script wait until the next frame; frames happen 60 times per second. Since we're changing our angle by 1 degree on each frame, it takes 6 seconds to complete the loop. If we wanted to spin faster, we could simply change the angl... |
What about position? That's just as easy: |
```miniscript |
for x in range(100, 900, 10) |
spr.x = x |
yield |
end for |
``` |
Type that in, and watch the wumpus zoom across the screen from left (x=100) to right (x=900). Now you try: make a loop that moves the Wumpus vertically instead of horizontally. (Remember that `y` values in Mini Micro range from 0 at the bottom of the screen, to 640 at the top.) |
Finally, let's look at the `scale` property. This is a factor that's multiplied by the sprite's natural size. When `scale` is 1, then one pixel in the image appears as one pixel on screen. If you set `scale` to 2, then each image pixel covers two pixels on screen. You're not limited to whole numbers, either; you ca... |
But this section is about animation, so let's animate! This time, how about a `while` loop: |
```miniscript |
while true |
spr.scale = 1 + sin(time)/2 |
yield |
end while |
``` |
{i:"`time`;`sin`"} |
The scale here is set using the built-in `time` function, which returns the time in seconds since the start of the program. This is passed through `sin` to produce a scale factor that slowly changes between 0.5 and 1.5. Just press Control-C when you've had enough of this animation. |
Our furry Wumpus is a single image, so loading it was very easy. But most animated characters have many different versions of the image, in different poses or with different expressions. Sometimes these are stored on disk as separate little image files, but other times, multiple sprite images are arranged into one bi... |
sprite sheet |
: a single image file that contains multiple smaller images meant for use as sprites |
Mini Micro has examples of both styles in the `/sys/pics/` directory. For example, `cd` down into "/sys/pics/KP" and do a `dir`. You will see eight images of K.P., a cute little character who looks like a handheld video game with arms and legs. (Remember that you can use the `view` command to preview an image in Min... |
But now go back up to the `/sys/pics` directory (perhaps by using `cd ".."`), and view the file "Enemies.png". |
 |
Here you can see one image file that includes quite a lot of little pictures: four versions of four different enemies, plus eight facial expressions, and a box of instructions on the right. (That's a feature most sprite sheets don't have, but this one does.) |
To make a sprite from a sprite-sheet is a two-step process: (1) load the sprite sheet itself; (2) extract the smaller image you need using `Image.getImage`. For example: |
```miniscript |
sheet = file.loadImage("Enemies.png") |
spr = new Sprite |
spr.image = sheet.getImage(0, 128*3, 128, 128) |
display(4).sprites.push spr |
spr.x = 480; spr.y = 320 |
``` |
The only real difference between this and the Wumpus example you did before is the `getImage` call on line 3, which plucks out a square area that starts at 0 on the left, 128\*3 pixels up from the bottom, and is 128 pixels wide by 128 pixels tall. |
This leads to the final common trick for animating sprites: changing the image over time. You probably noticed in the Enemies sprite sheet that there were two versions of the colored enemies, differing only in which foot is down. By alternating between these two images, we can give the appearance of the enemy marchin... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.