peterbrittain / asciimatics

A cross platform package to do curses-like operations, plus higher level APIs and widgets to create text UIs and ASCII art animations
Apache License 2.0
3.61k stars 238 forks source link

particles #369

Closed JoinSer closed 1 year ago

JoinSer commented 1 year ago

Hi, I have a question if you don't mind. Regarding the asciimatics library. I don't understand how to use the classes that belong to the ParticleEmitter class, for example, the Rocket class in effects [], I can't write it, it doesn't work. But classes related to ParticleEffect work correctly. Is it possible to take a test if it is not difficult? What to import and how to use it in animation. Thanks a lot!

from asciimatics.particles import Rocket
def animation_second(screen):
     scenes = []
     effects = [
         Rocket(screen, 10, 10, 10)]

    scenes.append(Scene(effects, 0, clear=True))
    screen.play(scenes, stop_on_resize=True, repeat=False)

 Screen.wrapper(animation_second)
peterbrittain commented 1 year ago

Hi @JoinSer.

The problem is that Rocket is a ParticleEmitter, but the Screen needs an Effect. If you look at https://asciimatics.readthedocs.io/en/stable/animation.html#particle-systems you'll see that you need a ParticleEffect. This is a special type of Effect that handles Particle systems for you.

If you want to look at the code in more detail, try looking at the classes that the particles.py sample uses. That should give you the code you need... Take a look at he how the firework effects use the Rocket class.

Hope that helps!

JoinSer commented 1 year ago

Hi @JoinSer.

The problem is that Rocket is a ParticleEmitter, but the Screen needs an Effect. If you look at https://asciimatics.readthedocs.io/en/stable/animation.html#particle-systems you'll see that you need a ParticleEffect. This is a special type of Effect that handles Particle systems for you.

If you want to look at the code in more detail, try looking at the classes that the particles.py sample uses. That should give you the code you need... Take a look at he how the firework effects use the Rocket class.

Hope that helps!

I looked at the code in particles.py, but I still can't figure out how to use the rocket class in the animation. Is it possible to get a piece of code from you, how it could look like?

peterbrittain commented 1 year ago

That animation uses the Rocket by putting it in a sequence of emitters controlled by the firework effects. For example, see:

https://github.com/peterbrittain/asciimatics/blob/365493d3a110fc00e371e33da1eba3ef282002cc/asciimatics/particles.py#L746-L758

The first emitter is the Rocket. When this dies, the next emitter is created, which makes the exploding ring. This all relies on the code inside the base ParticleEffect class, here:

https://github.com/peterbrittain/asciimatics/blob/365493d3a110fc00e371e33da1eba3ef282002cc/asciimatics/particles.py#L204

So, if you want an animation that just uses the Rocket emitter, you could use the RingFirework class as your Effect and simply remove the _next() method, so that it doesn't create the explosion.

peterbrittain commented 1 year ago

I'm assuming that was enough info... Happy to talk further if not.