How to run an ASCIImatics animation only one time in Python?

356 Views Asked by At

I'm trying to get the animation from ASCIImatics to run only one time then break into my normal code. Like an intro you could say. Is there a way to only run the animation one time then once you hit space bar it'll break the animation and go to the normal code body? Currently the animation runs on a loop after you hit space bar, and as I said I'm trying to only have it run once. Thanks!

from random import randint
from asciimatics.effects import Print
from asciimatics.particles import Explosion, StarFirework, DropScreen, Rain, \
    ShootScreen
from asciimatics.renderers import SpeechBubble, FigletText, Rainbow
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.exceptions import ResizeScreenError
import sys
from asciimatics.effects import Cycle, Stars
import time
 
def demo(screen):
    screen.set_title("ASCIIMATICS demo")
 
    scenes = []
 
    # First scene: title page
    effects = [
        Stars
            (screen, 200),
        Cycle(screen,
              Rainbow(screen, FigletText("Welcome to", font="big")),
              y=screen.height // 4 - 5),
        Print(screen,
              Rainbow(screen, FigletText("K y l e ' s   M i n i   G a m e s !", font="big")),
              screen.height // 2 - 3,
              start_frame=7),
        Cycle(screen,
              SpeechBubble("Press space bar to play"),
              screen.height - 3,
              #transparent=False,
              start_frame=10)
    ]
    scenes.append(Scene(effects, 0, clear=True))
 
    # Next scene: just dissolve the title.
    effects = [
        ShootScreen(screen, screen.width // 2, screen.height // 2, 100),
    ]
    scenes.append(Scene(effects, 10, clear=False))

    screen.play(scenes, stop_on_resize=True)

if __name__ == "__main__":
    while True:
        try:
            Screen.wrapper(demo)
        except ResizeScreenError:
            pass
1

There are 1 best solutions below

0
On

It depends what you want to achieve...

If you just want to play through all your Scenes once, simply specify repeat=False when you call play(). This will move on from the first Scene when you press space and will automatically stop after 10 frames on the second Scene (as specified by your code).

However, if you want to ensure that the space bar has no effect while you clear the screen in that second Scene, you'd need to create a custom Scene to catch and ignore the space bar. The GameController class in the ray_casting.py demo shows how to do this.