Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
log_y = screen_height // 3
elif screen_height < 48:
log_y = screen_height // 2
scenes = []
effects = [
Snow(screen),
Print(screen,
Fire(screen.height - 10, 80, "*" * 70, 0.8, 60, screen.colours,
bg=screen.colours >= 256),
0,
speed=1,
transparent=False),
Print(screen,
StaticRenderer(images=YULE_LOG),
x=log_x,
y=log_y,
colour=1,
speed=1,
transparent=True),
]
if screen_height > 40:
effects += [
Print(
screen,
FigletText("MERRY", font='univers'),
1,
speed=1,
start_frame=5),
Print(
screen,
ascii_image += " "
else:
if colours >= 256:
ascii_image += "${%d}" % (232 + col * 23 // 256)
else:
ascii_image += "${%d,%d}" % (
7 if col >= 85 else 0,
Screen.A_BOLD if col < 85 or col > 170 else
Screen.A_NORMAL
)
ascii_image += self._greyscale[
(int(col) * len(self._greyscale)) // 256]
self._images.append(ascii_image)
class ColourImageFile(StaticRenderer):
"""
Renderer to convert an image file (as supported by the Python Imaging
Library) into an block image of available colours.
.. warning::
This is only compatible with 256-colour terminals. Results in other
terminals with reduced colour capabilities are severely restricted.
Since Windows only has 8 base colours, it is recommended that you
avoid this renderer on that platform.
"""
def __init__(self, screen, filename, height=30, bg=Screen.COLOUR_BLACK,
fill_background=False, uni=False, dither=False):
"""
:param screen: The screen to use when displaying the image.
scenes.append(Scene(effects, 100 + screen.width))
# Scene 2 - Chase ghosts after a power pill
scenes.append(EatingScene(screen))
# Scene 3 - Thanks...
effects = [
Print(screen, FigletText("Thank you,"), screen.height // 3 - 3,
colour=Screen.COLOUR_RED),
Print(screen,
StaticRenderer(images=[namco]),
screen.height * 2 // 3 - 2,
colour=Screen.COLOUR_RED),
Print(screen,
StaticRenderer(images=["< Press X to exit. >"]),
screen.height - 1)
]
scenes.append(Scene(effects, 0))
screen.play(scenes, stop_on_resize=True, repeat=False)
def __init__(self, screen):
"""
See :py:obj:`.Sprite` for details.
"""
super(CrossHairs, self).__init__(
screen,
renderer_dict={
"default": StaticRenderer(images=["X"])
},
path=MouseController(
self, screen, screen.width // 2, screen.height // 2),
colour=Screen.COLOUR_RED)
def test_colour_maps(self):
"""
Check that the ${} syntax is parsed correctly.
"""
# Check the ${fg, attr, bg} variant
renderer = StaticRenderer(images=["${3,1,2}*"])
output = renderer.rendered_text
self.assertEqual(len(output[0]), len(output[1]))
self.assertEqual(output[0], ["*"])
self.assertEqual(
output[1][0][0],
(Screen.COLOUR_YELLOW, Screen.A_BOLD, Screen.COLOUR_GREEN))
# Check the ${fg, attr} variant
renderer = StaticRenderer(images=["${3,1}*"])
output = renderer.rendered_text
self.assertEqual(len(output[0]), len(output[1]))
self.assertEqual(output[0], ["*"])
self.assertEqual(output[1][0][0], (Screen.COLOUR_YELLOW, Screen.A_BOLD, None))
# Check the ${fg} variant
renderer = StaticRenderer(images=["${1}XY${2}Z"])
output = renderer.rendered_text
self.assertEqual(len(output[0]), len(output[1]))
self.assertEqual(output[0], ["XYZ"])
self.assertEqual(output[1][0][0], (Screen.COLOUR_RED, 0, None))
self.assertEqual(output[1][0][1], (Screen.COLOUR_RED, 0, None))
self.assertEqual(output[1][0][2], (Screen.COLOUR_GREEN, 0, None))
def test_height(self):
"""
Check that the max_height property works.
"""
# Max height should match largest height of any entry.
renderer = StaticRenderer(images=["A\nB", "C "])
self.assertEqual(renderer.max_height, 2)
def test_cycle(self):
"""
Check that Cycle works.
"""
# Check that cycle swaps colours every other frame.
screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
effect = Cycle(screen, StaticRenderer(images=["hello"]), 2)
effect.reset()
# First 2 calls should do nothing and use black.
effect.update(0)
screen.centre.assert_not_called()
effect.update(1)
screen.centre.assert_called_with("hello", 2, 0)
# Next 2 calls should do nothing and use red.
screen.centre.reset_mock()
effect.update(2)
screen.centre.assert_not_called()
effect.update(3)
screen.centre.assert_called_with("hello", 2, 1)
# Check there is no stop frame
self.assertEqual(effect.stop_frame, 0)
def test_banner(self):
"""
Check that BannerText works.
"""
# Check that banner redraws every frame.
screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
canvas = Canvas(screen, 10, 100, 0, 0)
effect = BannerText(canvas, StaticRenderer(images=["hello"]), 2, 3)
effect.reset()
effect.update(0)
self.assertEqual(canvas.get_from(canvas.width - 1, 2),
(ord("h"), 3, 0, 0))
effect.update(1)
self.assertEqual(canvas.get_from(canvas.width - 1, 2),
(ord("e"), 3, 0, 0))
# Check there is some stop frame - will vary according to screen width
self.assertGreater(effect.stop_frame, 0)
# This effect should ignore events.
event = object()
self.assertEqual(event, effect.process_event(event))
Simple class to make an iterator for a PIL Image object.
"""
def __init__(self, im):
self.im = im
def __getitem__(self, ix):
try:
if ix:
self.im.seek(ix)
return self.im
except EOFError:
raise IndexError
class ImageFile(StaticRenderer):
"""
Renderer to convert an image file (as supported by the Python Imaging
Library) into an ascii grey scale text image.
"""
# The ASCII grey scale from darkest to lightest.
_greyscale = ' .:;rsA23hHG#9&@'
def __init__(self, filename, height=30, colours=8):
"""
:param filename: The name of the file to render.
:param height: The height of the text rendered image.
:param colours: The number of colours the terminal supports.
"""
super(ImageFile, self).__init__()
with Image.open(filename) as image:
"""
super(Box, self).__init__()
if uni:
box = u"┌" + u"─" * (width - 2) + u"┐\n"
for _ in range(height - 2):
box += u"│" + u" " * (width - 2) + u"│\n"
box += u"└" + u"─" * (width - 2) + u"┘\n"
else:
box = "+" + "-" * (width - 2) + "+\n"
for _ in range(height - 2):
box += "|" + " " * (width - 2) + "|\n"
box += "+" + "-" * (width - 2) + "+\n"
self._images = [box]
class Rainbow(StaticRenderer):
"""
Chained renderer to add rainbow colours to output of another renderer.
The embedded rendered must not use multi-colour mode (i.e. ${c,a}
mark-ups) as these will be converted to explicit text by this renderer.
"""
# Colour palette when limited to 16 colours (8 dim and 8 bright).
_16_palette = [1, 1, 3, 3, 2, 2, 6, 6, 4, 4, 5, 5]
# Colour palette for 256 colour xterm mode.
_256_palette = [196, 202, 208, 214, 220, 226,
154, 118, 82, 46,
47, 48, 49, 50, 51,
45, 39, 33, 27, 21,
57, 93, 129, 201,
200, 199, 198, 197]