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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
#!/usr/bin/env python3
from copy import deepcopy
import sys
from asciimatics.exceptions import ResizeScreenError
from asciimatics.paths import Path
from asciimatics.renderers import StaticRenderer, ColourImageFile, FigletText
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.effects import Print, Sprite, BannerText
namco = """
88888888b. 8888888b. 8888888888b. .d88888888 .d888888b.
88 88 88 88 88 88 88 88 88
88 88 .d88888888 88 88 88 88 88 88
88 88 88 88 88 88 88 88 88 88
88 88 `888888888 88 88 88 `888888888 `8888888P'
"""
dot = """${7,2,7}####
${7,2,7}####
"""
pac_man = """
{0}##########
{0}##################
{0}############${{7,2,7}} {0}######
{0}############${{4,2,0}} ${{7,2,7}} {0}######
{0}##########################
{0}##########################
{0}##########################
{0}##########################
{0}##########################
{0}######################
{0}######################
{0}##################
{0}##########
""", """
{0}##########
{0}##################
{0}############${{7,2,7}} {0}######
{0}############${{4,2,0}} ${{7,2,7}} {0}######
{0}##########################
{0}##########################
{0}############
{0}##########################
{0}##########################
{0}######################
{0}######################
{0}##################
{0}##########
""", """
{0}##########
{0}##################
{0}############${{7,2,7}} {0}######
{0}############${{4,2,0}} ${{7,2,7}} {0}######
{0}##########################
{0}####################
{0}############
{0}####################
{0}##########################
{0}######################
{0}######################
{0}##################
{0}##########
""", """
{0}##########
{0}##################
{0}############${{7,2,7}} {0}######
{0}############${{4,2,0}} ${{7,2,7}} {0}######
{0}####################
{0}################
{0}############
{0}################
{0}####################
{0}######################
{0}######################
{0}##################
{0}##########
""", """
{0}##########
{0}##################
{0}############${{7,2,7}} {0}######
{0}##########${{4,2,0}} ${{7,2,7}} {0}######
{0}##################
{0}##############
{0}############
{0}##############
{0}##################
{0}####################
{0}######################
{0}##################
{0}##########
"""
pac_man_right = """
{0}##########
{0}##################
{0}######${{7,2,7}} {0}############
{0}######${{7,2,7}} ${{4,2,0}} {0}############
{0}##########################
{0}##########################
{0}##########################
{0}##########################
{0}##########################
{0}######################
{0}######################
{0}##################
{0}##########
""", """
{0}##########
{0}##################
{0}######${{7,2,7}} {0}############
{0}######${{7,2,7}} ${{4,2,0}} {0}############
{0}##########################
{0}##########################
{0}############
{0}##########################
{0}##########################
{0}######################
{0}######################
{0}##################
{0}##########
""", """
{0}##########
{0}##################
{0}######${{7,2,7}} {0}############
{0}######${{7,2,7}} ${{4,2,0}} {0}############
{0}##########################
{0}####################
{0}############
{0}####################
{0}##########################
{0}######################
{0}######################
{0}##################
{0}##########
""", """
{0}##########
{0}##################
{0}######${{7,2,7}} {0}############
{0}######${{7,2,7}} ${{4,2,0}} {0}############
{0}####################
{0}################
{0}############
{0}################
{0}#####################
{0}######################
{0}######################
{0}##################
{0}##########
""", """
{0}##########
{0}##################
{0}######${{7,2,7}} {0}############
{0}######${{7,2,7}} ${{4,2,0}} {0}##########
{0}##################
{0}##############
{0}############
{0}##############
{0}##################
{0}####################
{0}######################
{0}##################
{0}##########
"""
ghost = """
{0}########
{0}################
{0}####################
{0}##${{7,2,7}}....{0}########${{7,2,7}}....{0}######
${{7,2,7}}........{0}####${{7,2,7}}........{0}####
${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}####
{0}##${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}######
{0}####${{7,2,7}}....{0}########${{7,2,7}}....{0}########
{0}############################
{0}############################
{0}##########################
{0}####${{7,2,0}} {0}########${{7,2,0}} {0}########
{0}##${{7,2,0}} {0}####${{7,2,0}} {0}####
""", """
{0}########
{0}################
{0}####################
{0}##${{7,2,7}}....{0}########${{7,2,7}}....{0}######
${{7,2,7}}........{0}####${{7,2,7}}........{0}####
${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}####
{0}##${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}######
{0}####${{7,2,7}}....{0}########${{7,2,7}}....{0}########
{0}############################
{0}############################
{0}############################
{0}######${{7,2,0}} {0}########${{7,2,0}} {0}########
{0}####${{7,2,0}} {0}####${{7,2,0}} {0}####
""", """
{0}########
{0}################
{0}####################
{0}##${{7,2,7}}....{0}########${{7,2,7}}....{0}######
${{7,2,7}}........{0}####${{7,2,7}}........{0}####
${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}####
{0}##${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}######
{0}####${{7,2,7}}....{0}########${{7,2,7}}....{0}########
{0}############################
{0}############################
{0}############################
{0}########${{7,2,0}} {0}########${{7,2,0}} {0}########
{0}####${{7,2,0}} {0}####${{7,2,0}} {0}####
""", """
{0}########
{0}################
{0}####################
{0}##${{7,2,7}}....{0}########${{7,2,7}}....{0}######
${{7,2,7}}........{0}####${{7,2,7}}........{0}####
${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}####
{0}##${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}######
{0}####${{7,2,7}}....{0}########${{7,2,7}}....{0}########
{0}############################
{0}############################
{0}############################
{0}########${{7,2,0}} {0}########${{7,2,0}} {0}######
{0}####${{7,2,0}} {0}####${{7,2,0}} {0}####
""", """
{0}########
{0}################
{0}####################
{0}##${{7,2,7}}....{0}########${{7,2,7}}....{0}######
${{7,2,7}}........{0}####${{7,2,7}}........{0}####
${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}####
{0}##${{4,2,4}} ${{7,2,7}}....{0}####${{4,2,4}} ${{7,2,7}}....{0}######
{0}####${{7,2,7}}....{0}########${{7,2,7}}....{0}########
{0}############################
{0}############################
{0}############################
{0}##${{7,2,0}} {0}########${{7,2,0}} {0}########${{7,2,0}} {0}####
{0}####${{7,2,0}} {0}####${{7,2,0}} {0}##
"""
scared_ghost = """
${4,2,4}########
${4,2,4}################
${4,2,4}####################
${4,2,4}########################
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}############################
${4,2,4}############################
${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####
${4,2,4}##${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}##
${4,2,4}############################
${4,2,4}####${7,2,0} ${4,2,4}########${7,2,0} ${4,2,4}########${7,2,0} ${4,2,4}##
${4,2,4}##${7,2,0} ${4,2,4}####${7,2,0} ${4,2,4}####
""", """
${4,2,4}########
${4,2,4}################
${4,2,4}####################
${4,2,4}########################
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}############################
${4,2,4}############################
${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####
${4,2,4}##${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}##
${4,2,4}############################
${4,2,4}##${7,2,0} ${4,2,4}########${7,2,0} ${4,2,4}########${7,2,0} ${4,2,4}####
${4,2,4}####${7,2,0} ${4,2,4}####${7,2,0} ${4,2,4}##
""", """
${4,2,4}########
${4,2,4}################
${4,2,4}####################
${4,2,4}########################
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}############################
${4,2,4}############################
${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####
${4,2,4}##${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}##
${4,2,4}############################
${4,2,4}########${7,2,0} ${4,2,4}########${7,2,0} ${4,2,4}######
${4,2,4}####${7,2,0} ${4,2,4}####${7,2,0} ${4,2,4}####
""", """
${4,2,4}########
${4,2,4}################
${4,2,4}####################
${4,2,4}########################
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}############################
${4,2,4}############################
${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####
${4,2,4}##${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}##
${4,2,4}############################
${4,2,4}########${7,2,0} ${4,2,4}########${7,2,0} ${4,2,4}########
${4,2,4}####${7,2,0} ${4,2,4}####${7,2,0} ${4,2,4}####
""", """
${4,2,4}########
${4,2,4}################
${4,2,4}####################
${4,2,4}########################
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}####${7,2,7} ${4,2,4}########${7,2,7} ${4,2,4}####
${4,2,4}############################
${4,2,4}############################
${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####
${4,2,4}##${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}####${7,2,7} ${4,2,4}##
${4,2,4}############################
${4,2,4}######${7,2,0} ${4,2,4}########${7,2,0} ${4,2,4}########
${4,2,4}####${7,2,0} ${4,2,4}####${7,2,0} ${4,2,4}####
"""
eyes = """
${4,2,4}####${4,2,0} ${4,2,4}####
${7,2,7}..${4,2,4}####${7,2,7}..${7,2,0} ${7,2,7}..${4,2,4}####${7,2,7}..
${7,2,7}........${7,2,0} ${7,2,7}........
${7,2,7}........${7,2,0} ${7,2,7}........
${7,2,7}....${7,2,0} ${7,2,7}....
"""
# Globals used for pacman animation
direction = 1
value = 0
def cycle():
global value, direction
value += direction
if value <= 0 or value >= 4:
direction = -direction
return value
class PacMan(Sprite):
def __init__(self, screen, path, start_frame=0, stop_frame=0):
images = []
images_right = []
colour = Screen.COLOUR_YELLOW if screen.colours <= 16 else 11
for image in pac_man:
images.append(image.format("${%d,2,%d}" % (colour, colour)))
for image in pac_man_right:
images_right.append(image.format("${%d,2,%d}" % (colour, colour)))
super(PacMan, self).__init__(
screen,
renderer_dict={
"default": StaticRenderer(images=images, animation=cycle),
"left": StaticRenderer(images=images, animation=cycle),
"right": StaticRenderer(images=images_right, animation=cycle),
},
path=path,
start_frame=start_frame,
stop_frame=stop_frame)
def _update(self, frame_no):
super(PacMan, self)._update(frame_no)
for effect in self._scene.effects:
if isinstance(effect, ScaredGhost) and self.overlaps(effect):
effect.eaten()
class Ghost(Sprite):
def __init__(self, screen, path, colour=1, start_frame=0, stop_frame=0):
images = []
for image in ghost:
images.append(image.format("${%d,2,%d}" % (colour, colour)))
super(Ghost, self).__init__(
screen,
renderer_dict={
"default": StaticRenderer(images=images),
},
colour=colour,
path=path,
start_frame=start_frame,
stop_frame=stop_frame)
class ScaredGhost(Sprite):
def __init__(self, screen, path, start_frame=0, stop_frame=0):
super(ScaredGhost, self).__init__(
screen,
renderer_dict={
"default": StaticRenderer(images=scared_ghost),
},
colour=Screen.COLOUR_BLUE,
path=path,
start_frame=start_frame,
stop_frame=stop_frame)
self._eaten = False
def eaten(self):
# Already eaten - just ignore
if self._eaten:
return
# Allow one more iteration for this Sprite to clear itself up.
self._eaten = True
self._delete_count = 2
# Spawn the eyes to run away
path = Path()
path.jump_to(self._old_x + 12, self._old_y + 4)
path.move_straight_to(
self._old_x + 12, -8, (self._old_y + 12) // 2)
path.wait(10000)
self._scene.add_effect(Eyes(self._screen, path))
class Eyes(Sprite):
def __init__(self, screen, path, start_frame=0, stop_frame=0):
super(Eyes, self).__init__(
screen,
renderer_dict={
"default": StaticRenderer(images=[eyes]),
},
colour=Screen.COLOUR_BLUE,
path=path,
start_frame=start_frame,
stop_frame=stop_frame)
class EatingScene(Scene):
def __init__(self, screen):
super(EatingScene, self).__init__([], 240 + screen.width)
self._screen = screen
self._reset_count = 0
def reset(self, old_scene=None, screen=None):
super(EatingScene, self).reset(old_scene, screen)
# Recreate all the elements.
centre = (self._screen.width // 2, self._screen.height // 2)
path = Path()
path.jump_to(-16, centre[1])
path.move_straight_to(
self._screen.width + 16, centre[1], (self._screen.width + 16) // 3)
path.wait(100)
path2 = Path()
path2.jump_to(-16, centre[1])
path2.move_straight_to(
self._screen.width + 16, centre[1], self._screen.width + 16)
path2.wait(100)
# Take a copy of the list before using it to remove all effects.
for effect in self.effects[:]:
self.remove_effect(effect)
self.add_effect(
ScaredGhost(self._screen, deepcopy(path2)))
self.add_effect(
ScaredGhost(self._screen, deepcopy(path2), start_frame=60))
self.add_effect(
ScaredGhost(self._screen, deepcopy(path2), start_frame=120))
self.add_effect(
ScaredGhost(self._screen, deepcopy(path2), start_frame=180))
self.add_effect(PacMan(self._screen, path, start_frame=240))
def demo(screen):
scenes = []
centre = (screen.width // 2, screen.height // 2)
# Title
effects = [
BannerText(screen,
ColourImageFile(screen, "pacman.png", 16, 0, True),
(screen.height - 16) // 2,
Screen.COLOUR_WHITE),
Print(screen,
StaticRenderer(images=["A tribute to the classic 80's "
"video game by Namco."]),
screen.height - 1)
]
scenes.append(Scene(effects, 0))
# Scene 1 - run away, eating dots
path = Path()
path.jump_to(screen.width + 16, centre[1])
path.move_straight_to(-16, centre[1], (screen.width + 16) // 3)
path.wait(100)
if screen.colours <= 16:
inky = 6
pinky = 5
blinky = 1
clyde = 2
else:
inky = 14
pinky = 201
blinky = 9
clyde = 208
effects = [
PacMan(screen, path),
Ghost(screen, deepcopy(path), inky, start_frame=40),
Ghost(screen, deepcopy(path), pinky, start_frame=60),
Ghost(screen, deepcopy(path), blinky, start_frame=80),
Ghost(screen, deepcopy(path), clyde, start_frame=100),
]
for x in range(5, screen.width, 16):
effects.insert(0,
Print(screen,
StaticRenderer(images=[dot]),
screen.height // 2,
x=x,
speed=1,
stop_frame=4))
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)
if __name__ == "__main__":
while True:
try:
Screen.wrapper(demo)
sys.exit(0)
except ResizeScreenError:
pass
|