1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
"""
This module provides `Sprites` to create animation effects with Paths. For more details see
http://asciimatics.readthedocs.io/en/latest/animation.html
"""
import random
from asciimatics.effects import Sprite
from asciimatics.renderers import StaticRenderer
# Images for Sam-ple sprite.
from asciimatics.screen import Screen
sam_default = [
"""
______
.` `.
/ - - \\
| __ |
| |
\\ /
'.______.'
""",
"""
______
.` `.
/ o o \\
| __ |
| |
\\ /
'.______.'
"""
]
sam_left = """
______
.` `.
/ o \\
| |
|-- |
\\ /
'.______.'
"""
sam_right = """
______
.` `.
/ o \\
| |
| --|
\\ /
'.______.'
"""
sam_down = """
______
.` `.
/ \\
| |
| ^ ^ |
\\ __ /
'.______.'
"""
sam_up = """
______
.` __ `.
/ v v \\
| |
| |
\\ /
'.______.'
"""
# Images for an arrow Sprite.
left_arrow = """
/____
/
\\ ____
\\
"""
up_arrow = """
/\\
/ \\
/| |\\
| |
"""
right_arrow = """
____\\
\\
____ /
/
"""
down_arrow = """
| |
\\| |/
\\ /
\\/
"""
default_arrow = [
"""
/\\
/ \\
/|><|\\
| |
""",
"""
/\\
/ \\
/|oo|\\
| |
""",
]
# Simple static function to swap between 2 images to make a sprite blink.
def _blink():
if random.random() > 0.9:
return 0
else:
return 1
class Sam(Sprite):
"""
Sam Paul sprite - an simple sample animated character.
"""
def __init__(self, screen, path, start_frame=0, stop_frame=0):
"""
See :py:obj:`.Sprite` for details.
"""
super().__init__(
screen,
renderer_dict={
"default": StaticRenderer(images=sam_default, animation=_blink),
"left": StaticRenderer(images=[sam_left]),
"right": StaticRenderer(images=[sam_right]),
"down": StaticRenderer(images=[sam_down]),
"up": StaticRenderer(images=[sam_up]),
},
path=path,
start_frame=start_frame,
stop_frame=stop_frame)
class Arrow(Sprite):
"""
Sample arrow sprite - points where it is going.
"""
def __init__(self, screen, path, colour=Screen.COLOUR_WHITE, start_frame=0,
stop_frame=0):
"""
See :py:obj:`.Sprite` for details.
"""
super().__init__(
screen,
renderer_dict={
"default": StaticRenderer(images=default_arrow,
animation=_blink),
"left": StaticRenderer(images=[left_arrow]),
"right": StaticRenderer(images=[right_arrow]),
"down": StaticRenderer(images=[down_arrow]),
"up": StaticRenderer(images=[up_arrow]),
},
path=path,
colour=colour,
start_frame=start_frame,
stop_frame=stop_frame)
class Plot(Sprite):
"""
Sample Sprite that simply plots an "X" for each step in the path. Useful
for plotting a path to the screen.
"""
def __init__(self, screen, path, colour=Screen.COLOUR_WHITE, start_frame=0,
stop_frame=0):
"""
See :py:obj:`.Sprite` for details.
"""
super().__init__(
screen,
renderer_dict={
"default": StaticRenderer(images=["X"])
},
path=path,
colour=colour,
clear=False,
start_frame=start_frame,
stop_frame=stop_frame)
|