text stringlengths 0 897 |
|---|
end function |
// main loop |
mouseWasDown = false |
haveFlash = false |
while true |
// clear the flashes, if we have any |
if haveFlash then |
flashGfx.fillRect 0, 0, flashGfx.width, |
flashGfx.height, color.clear |
globals.haveFlash = false |
end if |
// launch whenever the mouse is clicked |
mouseIsDown = mouse.button |
if mouseIsDown and not mouseWasDown then |
launchAt mouse.x |
end if |
mouseWasDown = mouseIsDown |
// update all particles |
for p in particles |
p.update |
end for |
yield |
end while |
``` |
The `launchAt` function takes an *x* position in unscaled screen coordinates, like the mouse position (and unlike the `gfx` display in this program, which is scaled by a factor of 3). It creates a new `Rocket` instance, just like our test code from Listing 4. Many of the rocket properties — its initial velocity, and ... |
The main loop has several jobs. First, it clears the flash display, if indeed any flashes were drawn the last time through the loop. We do this with `flashGfx.fillRect`, filling with `color.clear`, because if we used `flashGfx.clear`, it would reset the display scale and then we'd just have to set it back. |
Second, it watches `mouse.button` and compares it to the previous value, to determine when the mouse has just been clicked. When that happens, it calls our `launchAt` method to launch a rocket at the mouse position. |
{i:"`yield`"} |
Finally, it loops over all particles and calls `update` on them. And then it calls the `yield` intrinsic, which tells Mini Micro to wait until the start of the next frame (frames are when the screen is updated, roughly 60 times per second). |
Run the program, and click around the screen. A rocket should launch from below the mouse each time you click. Go ahead and spam the mouse button! These fireworks are for *you*; you deserve it! (Just press control-C to break out of the main loop when done.) |
## Going Farther |
This has turned out to be a pretty short program — considerably shorter than the Sea Battle game you did in Chapter 14. Mini Micro allows you to make a lot of fun with not that much code! But since it was so short, perhaps you have time to try a bit more on your own? |
One simple hack would be to make it launch *two* rockets each time the mouse is clicked, instead of just one. This is literally just one extra line in the main loop. Why not give it a try? |
Another tweak is to change how long the embers last before they die. Right now they disappear after 3 seconds. Can you change that to 6 seconds? You will need to change the code in two places, because this time is used both to make the color fade out, and to actually remove the particles. Change it in both places, ... |
{i:"magic number"} |
Then, realize that this is a rather poor design, having that magic number in two different places. (This is similar to the concept of *magic strings* discussed in Chapter 14.) Add a `maxAge` property to the Ember class, and use that instead of the number in both places. |
Finally, we still have all the embers from a particular burst fading out and disappearing at the same time. Wouldn't it be more interesting if that time were randomized a bit too, so some embers last longer than others? Find where the embers are created, and we assign a random velocity. Assign a random `maxAge` ther... |
If you have difficulty with any of this, don't be discouraged! I encourage you to make use of the community resources you can find via the MiniScript web site (`https://miniscript.org`). There is a Discord server, forums, and more. Don't be afraid to ask for help — it is a very supportive and welcoming community, an... |
{pageBreak} |
A> **Chapter Review** |
A> - You built the *Fireworks* demo, celebrating all your hard work and progress up to this point. |
A> - You reviewed and practiced using display layers, mouse input, pixel graphics, and sound. |
A> - You learned about subclasses, superclasses, and the `super` keyword. |
A> - You went "beyond the book" and made some significant changes to the program on your own. Day by day, your coding skills are growing! |
{chapterHead: "Day 22: Sprites", startingPageNum:263} |
{width: "50%"} |
 |
Q> With too many bells and whistles, it gets complicated. With too few, there are inefficiencies. Designing something just powerful enough is an art. |
Q>— Barbara Liskov (2008 A. M. Turing award winner) |
A> **Chapter Objectives** |
A> - Learn about *sprites* and how they are used in games and utilities. |
A> - Explore the SpriteDisplay, Sprite, and Bounds classes in Mini Micro. |
A> - Write code to make sprites move, rotate, scale, and respond to mouse clicks. |
If you look carefully at any two-dimensional (2D) video game, you will see the screen full of little images that move around. This is easiest to see in older games (though still true today). Consider one of the greatest arcade games of all time, *Ms. Pac-Man*. Our heroine, Ms. Pac-Man, travels around the maze eating... |
Now consider how you would create such an effect using the tools you have so far, such as the `drawImage` and `fillRect` methods on `PixelDisplay`. You could certainly keep track of where the ghosts are, and draw them in their correct positions for each frame. But to make them move, you would need to erase them, perh... |
All this drawing would be difficult and inefficient, resulting in a slow, flickery display. Yet these machines from the 1980s drew things like this — and much more complicated scenes, with lots of moving objects overlapping a complex background — at video rates, without breaking a sweat. How did they do it? And more... |
The answer is... sprites. |
sprite |
: an image, often with transparent areas and usually much smaller than the screen, which can be made to appear on screen and efficiently moved, rotated, and scaled, all without disrupting the background or other sprites |
D> OK, fine, some readers will realize that in Mini Micro there is another answer, which is to take advantage of display layers. By drawing your ghost and player images in a different display layer than the maze and dots, you could clear the moving images on each frame without clobbering the background, and thereby ma... |
PRINT>The image on the next page is from a Mini Micro game called *Dr. Yond's Zombie Experiment*. The characters (both human and zombie), the giant floating brain, and the balls of energy are all sprites, as indicated with the white boxes in the figure. These are drawn and animated very efficiently over the pavement ... |
EBOOK>The following image is from a Mini Micro game called *Dr. Yond's Zombie Experiment*. The characters (both human and zombie), the giant floating brain, and the balls of energy are all sprites, as indicated with the yellow boxes in the figure. These are drawn and animated very efficiently over the pavement and bu... |
 |
## Creating and Drawing Sprites |
To make a program like this with your own code, you first have to create a sprite object. You may recall from Chapter 12 (or yesterday's fireworks program) that you create an object in MiniScript using `new` with the desired class. In this case what we desire is the built-in Sprite class. Try it right on the command... |
```miniscript |
spr = new Sprite |
spr.image = file.loadImage("/sys/pics/Wumpus.png") |
``` |
This creates a new Sprite object, and assigns an image (a fuzzy purple wumpus image from `/sys/pics`) to its `image` property. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.