text stringlengths 0 897 |
|---|
wait |
burstSnd.play |
wait |
end for |
``` |
Lines 22-23 set up a list of colors to use for the firework bursts. This is a long list, so we simply hit return after a comma, and continue it on the next line. (Note that this might slightly confuse the code editor, and cause it to mark the closing square bracket in red — don't let that alarm you.) |
Next, we define a couple of synthesized sounds for the rocket launch and burst. Remember that the parameters to `Sound.init` are *duration*, *frequency*, *envelope*, and *waveform*. |
We want to use the stereo pan (i.e. left-right balance) to make these sounds appear in the left speaker when they occur on the left side of the screen, and in the right speaker when they occur on the right (and smoothly interpolate in between). The `panForX` function does that calculation, converting the *x* coordinat... |
D> Code a little, test a little! |
Once that's working, delete that `for` loop starting on line 35, and add this code instead. |
{caption: "Listing 3 (Fireworks `Particle` class).", number-from: 35} |
```miniscript |
// make a Particle class |
Particle = {} |
Particle.x = 0 |
Particle.y = 0 |
Particle.vx = 0 |
Particle.vy = 0 |
Particle.color = color.yellow |
Particle.trail = null |
Particle.maxTrails = 5 |
Particle.gravity = -15 |
Particle.dragFactor = 0.99 |
Particle.update = function(dt=0.1) |
// apply gravity and drag to velocity |
self.vy = (self.vy + self.gravity*dt) * self.dragFactor |
self.vx = self.vx * self.dragFactor |
// apply velocity to position |
self.x = self.x + self.vx * dt |
self.y = self.y + self.vy * dt |
// draw, appending to the trail |
gfx.setPixel self.x, self.y, self.color |
if self.trail == null then self.trail = [] |
self.trail.push [self.x, self.y] |
// erase when the trail is too long |
if self.trail.len > self.maxTrails then |
pos = self.trail.pull |
gfx.setPixel pos[0], pos[1], color.clear |
end if |
end function |
Particle.die = function() |
// erase trail |
for pos in self.trail |
gfx.setPixel pos[0], pos[1], color.clear |
end for |
// remove from global list |
particles.removeVal self // (removeVal is from listUtil) |
end function |
// keep a list of all Particle instances |
particles = [] |
p = new Particle |
p.vx = 30 |
p.vy = 90 |
for i in range(90) |
p.update |
yield |
end for |
p.die |
``` |
In Listing 3, we define a class called Particle. This represent things that move according to simply physics, like the rockets and the embers (i.e. burst particles). The physics is quite simple: in addition to the position (*x* and *y*), we also keep track of velocity (*vx* and *vy*). On every step, the vertical vel... |
To draw a particle, we're just using `gfx.setPixel`. But we also keep track of the last several positions it was drawn, in the `trail` list. So when we draw the particle at a new position, we add that position to the trail; and when the trail is too long, we pull off the oldest point and erase it (by setting the pixe... |
The `Particle.die` function is called when we want to remove a particle completely. It erases all the pixels in the trail, and then removes the particle from the global `particles` list. Note that this `removeVal` method we're using here is not standard MiniScript; it is something added by the `listUtil` module impor... |
Finally, all that code at the end (lines 78-85) is, again, test code just to make sure that things are working so far. When you run this, you should see a particle leap up from the 0, 0 corner of the screen, arc across the screen, and then disappear. |
When that's working, delete lines 78-85, and then continue on with Listing 4. Here we make a *subclass* of Particle, to handle the special case of a particle that is actually a Rocket (i.e. launched from the ground, and not yet burst). |
subclass |
: a class created from another class using `new`, so that it inherits all of the values and methods of that other class |
superclass |
: the more general class that a subclass is created from |
Subclasses can be a very handy OOP (object-oriented programming) technique. They allow us to make a more specialized version of a class, one that is like the superclass in most ways, but also specialized in certain ways. In this case, the superclass is our general-purpose `Particle` class, and the subclass is `Rocket... |
{caption: "Listing 4 (Fireworks `Rocket` class).", number-from: 78} |
```miniscript |
// make a subclass of Particle that represents a rocket |
Rocket = new Particle |
Rocket.timeTillBurst = 3 |
Rocket.update = function(dt=0.1) |
// do standard particle update... |
super.update dt |
// then, update time till burst, and burst when it's time |
self.timeTillBurst = self.timeTillBurst - dt |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.