text stringlengths 0 897 |
|---|
```miniscript |
img1 = sheet.getImage(0, 128*3, 128, 128) |
img2 = sheet.getImage(0, 128*2, 128, 128) |
while true |
spr.image = img1 |
wait 0.2 |
spr.image = img2 |
wait 0.2 |
end while |
``` |
You should see the yellow enemy grumpily stamping his feet. |
At this point we've just done very simple, quick animation demos at the REPL prompt. In real games, things get a little more complicated: you probably have multiple sprites all doing different things. How do you keep track of what to change on every frame? ...Object-oriented programming to the rescue! |
The solution is to create a subclass of `Sprite` for each kind of sprite you need in your game, all with an `update` method to be called on each frame. Each such object is responsible for keeping track of its own state, so it knows what it needs to change and when. Then the main loop has nothing to do but call `updat... |
{i:"`/sys/lib`,`mathUtil`"} |
{caption:"Stampy demo."} |
```miniscript |
import "mathUtil" |
clear |
sheet = file.loadImage("/sys/pics/Enemies.png") |
// Make a subclass of Sprite called Stamper |
Stamper = new Sprite |
Stamper.frames = [] // (image for each frame of animation) |
Stamper.frames.push sheet.getImage(0, 128*3, 128, 128) |
Stamper.frames.push sheet.getImage(0, 128*2, 128, 128) |
Stamper.target = {"x":480, "y":320} |
Stamper.lastStepTime = 0 |
Stamper.curFrame = 0 |
Stamper.update = function() |
if time > self.lastStepTime + 0.2 then |
// Change to the other image (stamp stamp!) |
self.curFrame = 1 - self.curFrame |
self.image = self.frames[self.curFrame] |
self.lastStepTime = time |
end if |
// Move towards the target |
mathUtil.moveTowardsXY self, self.target, 5 |
end function |
// Create a Stamper instance, and add to the sprite display. |
stampy = new Stamper // yes, it's stampy the Stamper! |
display(4).sprites.push stampy |
stampy2 = new Stamper // and he has a friend |
display(4).sprites.push stampy2 |
stampy2.target = {"x":800, "y":100} |
// Main loop: update sprites, and set target on mouse click |
while true |
for spr in display(4).sprites |
spr.update // (assumes all sprites have an update function) |
end for |
if mouse.button then stampy.target = mouse + {} |
yield |
end while |
``` |
When you run this, you should see two enemy sprites appear. One slinks off into the corner and stays there, while the other moves to any point on the screen that you click with the mouse. And both enemies stamp their feet the whole time. |
Let's quickly go over how this works. The first line imports the "mathUtil" module, so we can use the handy `mathUtil.moveTowardsXY` function to move our sprite towards a target. On lines 5-21 we define a Sprite subclass called Stampy, with properties to keep track of both sprite frames (images plucked from the sprit... |
Lines 24-25 create a new `Stamper` instance named `stampy`, and add it to the sprite display. Lines 26-28 create *another* `Stamper` instance, and give it a different initial target. |
Finally, the main loop updates all the sprites (assuming that all sprites have an `update` method, which is true in this demo because they are all derived from `Stampy`). Also, when the mouse button is pressed, it sets the target of our `stampy` sprite to the mouse position (plus an empty dictionary; see note below). ... |
D> What's going on with `mouse + {}` in line 35? Adding two maps together always results in a *new* map, even if one of the maps you're adding is empty. So this is a way of creating a fresh copy of the `mouse` map. Without this trick, we would be telling `stampy` to use the built-in `mouse` map as its target, and wh... |
## Controlling Draw Order |
The last example had two sprites on screen. If you directed the the first one to the lower-right corner, so that it overlapped the second sprite, then you may have noticed that at always appeared behind the second one. |
That's controlled by the order in which sprites appear in the `sprite` list of the display. In that last example, we first pushed `stampy` onto the list at position 0, and then we pushed `stampy2`, which would put it at position 1. You can think of the sprite display as simply walking through its sprite list, drawing... |
Run that example again, and position the moving sprite so that it's partially overlapping the stationary one. Then break the program (Control-C), and type in this: |
```miniscript |
while true |
display(4).sprites.push display(4).sprites.pull |
wait 1 |
end while |
``` |
Every second, this simply loop pulls element 0 off the sprites list, and pushes it onto the end of the list. Changing the order of sprites in a list changes the order in which they are drawn on screen. |
Some games like to sort the sprites to produce a pseudo-3D effect, where sprites higher up on the screen are meant to be further away from the viewer, and so should be drawn behind sprites that are lower on the screen. We can make the Stampy demo do that with two small changes. First, insert this line at the bottom o... |
{number-from: 21} |
```miniscript |
self.sortOrder = -self.y |
``` |
This adds a new `sortOrder` property to the sprites that decreases as Y increases, that is, the sortOrder gets smaller as sprites move up the screen. Then add these lines in the main loop, after `end for` but before the `if mouse.button` line: |
{number-from: 36} |
```miniscript |
// sort sprites by their sortOrder property |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.