|
- #!/usr/bin/env python3- ?6 H) Y1 h' |
- #
) _5 q3 u X4 L - # unimatrix.py* p7 M/ ^8 N( s: D. R# |1 M! y
- # <https://github.com/will8211/unimatrix>" I3 y% R; x y1 s
- #
% N3 ]0 O" `& F9 v5 V: z1 m - # Python script to simulate the display from "The Matrix" in terminal. Uses
( A; S6 r$ v& _5 U, l0 m0 E - # half-width katakana unicode characters by default, but can use custom
4 V. z6 T" C/ r) O2 a - # character sets. Accepts keyboard controls while running.
9 [ p6 `2 b; d - #: n/ e- T8 ^# [
- # Based on CMatrix by Chris Allegretta and Abishek V. Ashok. The following, v5 [. V5 ~$ k" `8 T! s. d
- # option should produce virtually the same output as CMatrix:
% Z7 F1 N) ^9 c( }' ` - # $ unimatrix -n -s 96 -l o1 t/ Y* N) } C
- #
. k% S, _) x! \2 z - # Unimatrix is free software: you can redistribute it and/or modify it under" A1 r8 N3 e) r6 a# g
- # the terms of the GNU General Public License as published by the Free Software
3 I! V7 F3 v" P0 f2 g - # Foundation, either version 3 of the License, or (at your option) any later) {. T0 q7 N7 C4 }/ k
- # version.
. [( ~8 c5 ]- E4 g h - #
5 K- r: a; `+ o4 h$ N6 T8 i" _ - # Unimatrix is distributed in the hope that it will be useful, but WITHOUT ANY
+ S, |, @, Q+ x( w9 \ - # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
& q, Z0 w+ n3 M; [7 Z/ J1 I - # A PARTICULAR PURPOSE. See the GNU General Public License at
; X# n& @+ C* J# U - # <http://www.gnu.org/licenses/> for more details.
9 L4 n+ v) }3 y1 y2 ^ - #
7 M q( ]4 D2 g$ |6 M - # Created by William Mannard4 ~" a2 a# n I2 u
- # 2018/01/19
/ H+ h. v4 J5 S4 h+ f
/ W& Z, d9 M# @9 _3 i- import argparse- G! Q8 o! t, F; C0 H9 a. r2 y
- import curses
3 {9 q4 f f2 \. J, N/ c8 v - import time2 } ^( |; M# L& [( ~1 Z o
- from random import choice, randint% f) F6 ^' E( D3 g" Z. U K( I
- 3 W8 c9 L* R" I
- help_msg = '''
: Z: |% Z8 i- ] - USAGE
( o( Y8 Z1 N8 G6 ]8 ~* {1 z1 L - unimatrix [-a] [-b] [-c COLOR] [-f] [-g COLOR] [-h] [-l CHARACTER_LIST] [-n]
( m0 t5 H$ x& A, Z' b - [-o] [-s SPEED] [-u CUSTOM_CHARACTERS]
, N T* L |) Q1 B - % |7 B$ {) \6 u/ E T! s
- OPTIONAL ARGUMENTS0 ?$ Q% b% }( |4 Y% F
- -a Asynchronous scroll. Lines will move at varied speeds.
. g7 y0 V; T( t; D/ J7 p
9 P3 U2 e+ z7 G1 s3 e- -b Use only bold characters+ q! ~3 |. d) Q% s5 g
% M- [! l6 S, L- W" a: r) Q9 M- -c COLOR One of: green (default), red, blue, white, yellow, cyan,
, T6 H1 w+ [) M+ T& e1 P8 M - magenta, black
! @$ y+ ?! {5 K$ Y& m - $ t Z* z4 s( _7 g+ ~+ `) h
- -f Enable "flashers," characters that continuously change.
, e2 A: w# p2 n4 [ - 2 x' J: D. W, r# _/ Q3 B
- -g COLOR Background color (See -c). Defaults to keeping
8 W$ h0 e% [( [ - terminal's current background.) @) h: m) x, k* }
: l+ W" T b1 `- -h Show this help message and exit% I3 Q3 D) ]/ Z" L! a
- ) D( S' O- [- A0 v8 `
- -l CHARACTER_LIST Select character set(s) using a string over letter
: [) r! l5 l# i/ `6 Y - codes (see CHARACTER SETS below.). i$ G* t/ V$ \1 [ C, e# p/ L5 K! @
- : e# f$ u7 }3 Z9 P+ \& Q
- -n Do not use bold characters (overrides -b)$ Y6 R O- b* b5 m w
9 i2 Y0 K: G1 o$ a9 o# [# a7 s2 I; k- -o Disable on-screen status
, p. g* @8 ?. Q i$ X" P$ M' Y
) z. o; x4 M/ C" F% j5 d+ V- -s SPEED Integer up to 100. 0 uses a one-second delay before
8 o) m9 R! q( L7 ?& o7 S. ]+ Z& l - refreshing, 100 uses none. Use negative numbers for9 ~0 B5 ~ t7 h* k2 Z B
- even lower speeds. Default=854 b# P9 L' m' v/ g3 V+ {& [
- 5 n: x$ |/ A' @4 d% ^/ [/ ^
- -t TIME Exit the process after TIME seconds3 ^# u& q$ n& B
6 q, x' P3 T7 `/ {! d5 H- -u CUSTOM_CHARACTERS Your own string of characters to display. Enclose in
+ C- |/ Z, q9 z( L4 a3 J - single quotes ('') to escape special characters. For+ K1 Y E4 z# D8 k/ p! ~' @1 p
- example: -u '#$('3 W: R. c, k4 @4 w+ N
- & G. J. Z v, w# f0 G
- -w Single-wave mode: Does a single burst of green rain,+ ?, S* B3 ^% y9 j3 \9 m0 ^+ Q1 m1 N5 Y
- exits. You can put in a .bashrc file to run when your' _- w& r9 Z. c
- terminal launches. Works well with speed at 95.! c% A4 ~3 W: X4 C( g! I
, e: a0 W5 m. W6 }* v- LONG ARGUMENTS
& @8 @; i! Q# Q0 R5 w4 K - -a --asynchronous
. A6 y' f6 H% n' a! H2 L* s - -b --all-bold
* w* g* j3 r6 } - -c --color=COLOR( ^1 H4 I7 n, G$ c% p0 K
- -f --flashers
q( J3 ^6 s) | n- }9 I% T6 D - -g --bg-color=COLOR/ A$ S: `7 G- _: w
- -h --help
" I% r& N$ K" `! J0 _. A1 r$ [* y9 B' i$ [ - -l --character-list=CHARACTER_LIST: ^! ] k* j# t) ]% M6 [7 `
- -s --speed=SPEED
4 r I; M: x, g* o - -n --no-bold9 x/ ~) c$ ]8 I. [, D
- -o --status-off5 E/ R- j( l5 a1 N0 V& _# }
- -t --time
" }( V$ E7 R P2 g- r/ V" ` - -u --custom_characters=CUSTOM_CHARACTERS i% q5 [! i7 l, H4 I7 Y
- -w --single_wave5 [, v& \/ k# A+ y \
0 q, Z5 a( L- K! ~: f6 x- CHARACTER SETS
( b: J: c% {! A4 Y& w1 \6 i - When using '-l' or '--character_list=' option, follow it with one or more of5 d+ Y* d _% t! l% E1 d
- the following letters:
6 ]3 m8 l% Z N: L3 p
5 K6 n9 t H+ o D" s0 ~- a Lowercase alphabet' C6 Z6 n& u& I7 \
- A Uppercase alphabet, m* K! @; Q+ \0 v
- c Lowercase Russian Cyrillic alphabet; V) S# A: g. L1 Q
- C Uppercase Russian Cyrillic alphabet G7 c% X5 N# K2 F& I5 ?
- e A few common emoji ( ☺☻✌♡♥❤⚘❀❃❁✼☀✌♫♪☃❄❅❆☕☂★ )
( }) n$ {# U6 v" W2 a2 n- k/ p! Q O - g Lowercase Greek alphabet
! k) Q0 M4 |( g4 Q# s7 C - G Uppercase Greek alphabet" y/ I# x- @9 J' P( t- g
- k Japanese katakana (half-width)) r* b s/ N# u+ m2 M
- m Default 'Matrix' set, equal to 'knnssss'' a1 U; i4 o) X% A/ T
- n Numbers 0-9/ y' D! ]" y1 u" m+ J3 V
- o 'Old' style non-unicode set, like cmatrix. Equal to 'AaSn'0 g0 e D9 _6 ^6 H% z2 R6 j/ Y
- p Klingon pIqaD (requires 'Horta' family font)*' r7 U! L& Q1 z1 D/ {, W! x
- P Klingon pIqaD (requires 'Klingon-pIqaD' or 'Code2000' family font)** o5 ] ]: C* u" ]1 u( Z
- r Lowercase Roman numerals ( mcclllxxxxvvvvviiiiii )
- [3 u9 i, g! u2 ?6 l9 w9 R - R Uppercase Roman numerals ( MCCLLLXXXXVVVVVIIIIII ), m5 A7 z0 J% x# t/ `! o4 E! b! Z
- s A subset of symbols actually used in the Matrix films ( -=*_+|:<>" )
8 y( [! O5 ?! C. ^: T - S All common keyboard symbols ( `-=~!z#$%^&*()_+[]{}|\;':",./<>?" )* ]' P% R- C; [+ L2 ]6 f
- u Custom characters selected using -u switch
; c1 M2 \8 @& M( y: V6 C/ h - + B. J; v7 l# H
- For example: '-l naAS' or '--character_list=naAS' will give something similar! l- y' Q+ D7 z* _# w
- to the output of the original cmatrix program in its default mode.& } l9 N7 R) h0 |# J& P: Y1 \) \0 n
- '-l ACG' will use all the upper-case character sets. Use the same
6 m3 [8 d$ m5 O7 Z5 O5 L4 y, h% v - letter multiple times to increase the frequency of the character set. For
- r& }$ M: i5 X) ~ - example, the default setting is equal to '-l knnssss'." \" y) o. B, A; k( l0 F$ N
- 9 l& Y3 j( Z8 d1 ]
- * With most modern Linux terminals (gnome-terminal, konsole, lxterminal,3 l$ V6 h4 d( S8 ?
- xfce4-terminal, mate-terminal) simply having the font installed system-wide6 s n3 q. ~, z
- is enough. The terminal will fall back to it for the Klingon, meaning that9 }4 N- `8 Y$ j; L2 ?
- you don't have to select the font in your terminal settings. 'Horta' seems
+ E$ U; _# U$ ]8 |) w; d - not to work in Konsole. Fonts may need to be set manually as fallbacks in
" }& G/ V3 D% G- T - .Xresources for older terminals, such as urxvt and xterm.; ^9 A0 J; u6 k3 U' t j# J
/ F: ?. X- W$ J" @( w$ ^- KEYBOARD CONTROL
# m+ X+ g$ j0 Z; T: }, X- Z; z% { - SPACE, CTRL-c or q exit
$ } Z/ w0 a* C/ m" h - - or LEFT decrease speed by 1
; A" _* u* K) b) B - + or RIGHT increase speed by 1) n/ K) F. [3 b
- [ or DOWN decrease speed by 10' ` J1 {3 S# H+ @
- ] or UP increase speed by 10* t* |2 T. c; }; x
- a toggle asynchronous scrolling
3 ?) i6 a6 L; k: t# h/ s$ X - b cycle through bold character options7 b" p5 ?, I6 x, b }7 T
- (bold off-->bold on-->all bold)
1 o7 I; c4 U" H. q8 z - f toggle flashing characters
6 t- g) y" `/ ` - o toggle on-screen status
: M! V2 @$ ^$ j% Y& }) _3 ~ - 1 to 9 set color: (1) Green (2) Red (3) Blue (4) White, t. \/ O: X' S/ u5 K( B
- (5) Yellow (6) Cyan (7) Magenta (8) Black; w4 w, P1 g7 p7 _) Z6 c
- (9) Terminal default
* c U1 M& N; k# ? - ! to ( set background color (same colors as above, but pressing: L4 d- R9 f( \1 R- @
- shift + number)
( J0 H7 a$ y2 q- Y0 i _
$ ?0 i7 h: i# E& b/ x2 c! x+ j- EXAMPLES2 f* N$ I ?/ I# D0 S% E
- Mimic default output of cmatrix (no unicode characters, works in TTY):7 y. Z0 q: x& G- V( r$ g4 d" b
- $ unimatrix -n -s 96 -l o& N/ @0 K3 Q' W. b5 B6 l6 H( L
- $ @6 J0 U" t; k! a( I& F2 |
- Use the letters from the name of your favorite operating system in bold blue:
" |+ Z2 @. i" {" J - $ unimatrix -B -u Linux -c blue" b, O$ o9 @4 ~& e) h# u2 U& ~3 a4 q
- & `' \' i o2 O: D4 U9 e
- Use default character set, plus dollar symbol (note single quotes around
4 W# E- P& m3 r. E, [ - special character):- w& M* b/ K: ~. v1 C" f( a
- $ unimatrix -l knnssssu -u '$'5 o3 |/ `+ R6 ^' F. F0 a) i
: C, ]5 Z% r* o/ U- No bold characters, slowly, using emojis, numbers and a few symbols:7 u: c# W" J& T. {3 c# Q, q/ h
- $ unimatrix -n -l ens -s 50
* u7 y. y# S% t - '''* |5 ~6 s5 z& `0 Y
8 L3 z& y/ h- o) V9 z# S) S8 Z- ### Set up parser and apply arguments settings
6 h8 d1 T# j+ V$ f/ ~ - ' S0 Q% y9 H* N# `9 r
- parser = argparse.ArgumentParser(add_help=False)
: V: a1 c8 }2 B) q
5 f% k5 B! b) p9 Q- parser.add_argument('-a', '--asynchronous',
) p8 E$ M! e; z2 r; ]# y1 I2 s - action='store_true',! c8 I% |" }# }/ R6 l% B" b( c
- help='use asynchronous scrolling')$ C6 R/ ~0 J0 ] z I3 R% W
- parser.add_argument('-b', '--all-bold',
/ F _! a4 ^: _3 z S+ K: m6 H7 s - action='store_true',3 u& ^/ b3 z1 E. T4 |8 `
- help='use all bold characters')
Y; U; ~1 Y7 h: E - parser.add_argument('-c', '--color',
- D/ o4 ` A$ m W5 D4 V6 t - default='green',# T# }- F9 J d! O6 T
- help='one of: green (default), red, blue, white, yellow, \; F- C) b; m5 r7 }
- cyan, magenta, black',5 M5 \0 K( m/ p* ^
- type=str)
' K+ d1 e& s2 D/ |% ~ - parser.add_argument('-f', '--flashers',
+ n7 B# H0 H5 L; U2 o( l( I+ [& l - action='store_true', I p J# h2 v; }; R
- help='some characters will continuously change in place')7 b& A. V4 y" t, ]5 [$ e5 D
- parser.add_argument('-g', '--bg-color',$ I6 [& L6 o9 E" B* n; v
- default='default',
, @$ A% [; |. z( D) i' C) y - help='background color (see -c)',8 A9 l/ ?) h/ i1 h* e7 q. @. w
- type=str)9 Y' q" x" k" X/ `' j
- parser.add_argument('-h', '--help',
9 x2 u) J" V6 Y0 _ - help='display extended usage information and exit.',- } b6 i0 }# G3 |5 g# {
- action='store_true')
$ f1 u: P d9 l - parser.add_argument('-l', '--character-list',
4 [6 K) Y. R% d4 s( { - help='character set. See details below',
9 V9 h: s# @$ E# x0 U - type=str)9 g# ^! b& D. p
- parser.add_argument('-n', '--no-bold',& g' s+ w. g _7 `1 V
- action='store_true',+ F1 W0 D2 A O4 a2 C
- help='do not use bold characters')3 w: S, |2 j8 k9 |
- parser.add_argument('-o', '--status-off',: e3 K* S* X1 u5 P5 R
- action='store_true',
8 W3 i' G3 B$ j3 | - help='Disable on-screen status') X, }+ c" `* Z5 H& y
- parser.add_argument('-s', '--speed',
+ h: X1 B; B7 Q - help='speed, integer up to 100. Default=85',& R3 Z6 b4 r& o/ `% `
- default=85,
% ~5 h2 X" n, F2 h% n) j - type=int)0 L( {: D9 y' Q4 a2 {: y0 J
- parser.add_argument('-t', '--time',7 V) h/ E8 _7 ]3 D
- help='time. See details below',
: o: j1 q& e- }% s1 C4 r6 s - type=int)
9 E6 B6 h" c% o% F - parser.add_argument('-u', '--custom-characters',0 n' D' t3 v2 m5 g+ c3 J' K
- help='your own string of characters to display',- L4 J( X- |/ h R
- default='',' V* I$ `5 ?! U' N( e
- type=str)$ v' Q& n# G& z( q% c8 L* F& v
- parser.add_argument('-w', '--single-wave',
( P; e) P: a" E - help='runs a single "wave" of green rain then exits',
7 } H' t9 u$ @8 }, P6 e - action='store_true'), y* i2 L* v0 v* Z+ o- ^
- % h6 P6 V; @6 f* h. G+ I
- args = parser.parse_args()" K% j, A0 S9 v7 Z7 h% W& m
- ( |6 T3 r! o5 r3 C# C/ m
- if args.help:
8 x* f3 C7 V6 x+ I7 i - print(help_msg): V6 |# M8 {. M' U. j
- exit()) k5 e7 K0 J2 C( M* s- V, }6 W
- z" H# A! F7 _7 G1 V
- char_set = {
" k4 B6 F1 k9 C4 `/ I2 N( _ - w$ A# @. {0 ?3 D
- 'a': 'qwertyuiopasdfghjklzxcvbnm',
+ q2 ^: U/ b& l; I1 W: o( c - 'A': 'QWERTYUIOPASDFGHJKLZXCVBNM',3 o- p ^: a8 p( U: B
- 'c': 'абвгдежзиклмнопрстуфхцчшщъыьэюя',7 P6 s& ?. ]/ m9 u" `6 S+ e: t
- 'C': 'АБВГДЕЖЗИКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ',+ Y5 ]4 x* n+ w# H& m
- 'e': '☺☻✌♡♥❤⚘❀❃❁✼☀✌♫♪☃❄❅❆☕☂★',) K9 a% o P& }6 [' A
- 'g': 'αβγδεζηθικλμνξοπρστυφχψως',
: ] m, q |2 u7 u6 R" O! Q) z - 'G': 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ',$ ]" Q$ Z- L/ V8 |) U/ v2 r3 }
- 'k': 'ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン',
9 U- O$ K7 Z/ t6 @: p - 'm': 'ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン1234567890'" h3 I/ Y, C8 m" M/ Y6 V1 z) T
- '1234567890-=*_+|:<>"-=*_+|:<>"-=*_+|:<>"-=*_+|:<>"',* o) s1 }' V5 Z$ ~7 m
- 'n': '1234567890',$ g* V; R5 z/ e
- 'o': 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890'; e% e# A% W" T/ u/ t" Q- i
- '`-=~!@#$%^&*()_+[]{}|\;\':",./<>?"',8 x% k: D, D6 s4 p' H' `
- 'p': '',
0 D" ?4 n3 D, n! D' x - 'P': '',, A- }7 C' r ?% c a' ?
- 'r': 'mcclllxxxxvvvvviiiiii',/ r7 q' [9 u5 O% j+ F
- 'R': 'MCCLLLXXXXVVVVVIIIIII',
5 s# X, e& ~: h4 p5 t# u - 's': '-=*_+|:<>"',8 d. D# b6 y8 b, q1 H2 D `
- 'S': '`-=~!@#$%^&*()_+[]{}|\;\':",./<>?"',6 x' r0 @" ^( i% ?
- 'u': args.custom_characters}. a/ ~, L! S& U" c4 a
% p4 l# [( {& g9 k8 [" ^# T- colors_str = {
. U6 I1 k5 e8 t2 T& x: W - 'green': curses.COLOR_GREEN,
9 t) o; }/ M) U$ T- m3 r! a) E$ s - 'red': curses.COLOR_RED,
8 x/ d I P) @ - 'blue': curses.COLOR_BLUE,
B( F0 E6 _' S+ X5 @ - 'white': curses.COLOR_WHITE,
% F" |7 J( R' P - 'yellow': curses.COLOR_YELLOW,& _1 ]5 K+ ~: U( V
- 'cyan': curses.COLOR_CYAN,8 R; U7 G$ t/ c. }8 ^4 f
- 'magenta': curses.COLOR_MAGENTA," G$ J9 B, t5 q3 {' t! r7 J" h! @, o
- 'black': curses.COLOR_BLACK," \1 L) Y7 _: P7 `+ n+ H
- 'default': -1}
E' M0 W% D S
" B% q. C8 g t- start_color = colors_str[args.color]
6 a+ _1 \( U; F1 Y - start_bg = colors_str[args.bg_color]. H8 F; e: r! d; w9 I& ]& p$ I
$ a2 C3 n# B# q3 `8 d( M- speed = args.speed! v$ z% d# e/ N- D N
- start_delay = (100 - speed) * 10
3 ]: c$ f$ s( ~9 p - 1 J, l$ q0 N7 b! ~; S
- runtime = None. n p- u! i" {" N$ y4 M* C/ c
- : G: `; ^0 d9 I4 \
- if args.time:* l9 C6 u2 U3 t* ]
- runtime = args.time0 m2 M+ P1 R& D/ V. z0 Z9 [
) J. M0 [: S: { t4 @& x2 _- # "-l" option has been used
% _$ `( P% W, d: w7 b4 v5 z - if args.character_list:
0 y8 t* F. n6 b; r0 W. q. k* Z$ R: U* f - chars = ''% n9 X- x" Y3 x. r; T( ]. }
- for letter in args.character_list:
2 ~9 W/ b$ Q: l6 O* z8 h - try:
2 X( T# @$ A1 @* T) Z% G - chars += char_set[letter]
5 z; |& v; ~0 I6 c! T - except KeyError:6 D% L, g+ O( e- R7 C" |
- print("Letter '%s' does not represent a valid character list."
/ l% [4 _' p- ~5 m& D( z1 O - % letter)
& {, t' G2 f2 U1 Q8 M1 S - exit(): r2 T- l, G1 P! V: v; i
- , J G/ h% Y3 W
- # "-l" not used, but "-u" is set' Q! K5 m, @4 \( \( @
- elif args.custom_characters:
+ j! [ G- P" i" q - chars = args.custom_characters' s! Z' J4 U- Q9 K. f1 F
O+ {% P5 \! t; V/ h; F) ]- # Neither "-l" nor "-u" has been set, use default characters' S7 v5 h( T( j3 K1 N
- else:- T0 u5 x$ q# T% c/ m5 O# i8 \
- chars = char_set['m']
1 X* g; z8 L, \6 @3 \& e9 o
* g( G9 P4 E1 M! o- if args.no_bold:) @9 Q+ t, U q! X+ V
- args.all_bold = False
! V* `" J& v6 |) m% \
+ `$ G% d" j* b: A- chars_len = len(chars) - 1
# w8 ~3 T% K- p( m B5 j - 7 s: w; m) M5 [4 ^! l- E
& w8 K+ D+ X7 t( d9 f5 Z2 z- ### Classes/ O* A, b( u8 r/ x8 x2 {; V" x5 d1 c
- & g+ \/ v$ e$ p3 c3 j
- class Canvas:& V) [. ]; [; d7 u$ k/ N
- """6 h7 A7 k% Z2 f0 [" L6 b
- Represents the whole screen and stores its height and width. Gets& I' _# l! D. v# B
- overwritten whenever the screen resizes. Serves as a container for columns.
/ o, S9 B4 g5 g1 {' m( G, t - """) D! ?- ]3 O" |2 } J7 ~. A
$ h! H6 I9 n$ D8 F- L- def __init__(self, screen):
; P) {1 N; x3 e) _' F8 O% R - screen.clear(): k- a* ?- K/ Y6 x% `
- rows, cols = screen.getmaxyx()* ~0 l" m+ }% g" {8 V1 V- Y) S
- self.col_count = cols. I0 Z3 |. D( a5 A' A' R+ }
- self.row_count = rows
0 W! k% z* O# _ z- n' r - self.size_changed = False
1 _0 B0 a5 u- V. G" w: G4 b: Z - self.columns = []: J( ~( `7 {6 G2 v9 F+ i
- for col in range(0, cols, 2):' [6 e3 f- A( B
- self.columns.append(Column(col, self.row_count))
) M% P0 b# @, ^$ }/ d0 T3 i - self.nodes = []
' N% s' r9 x8 M4 \! l' W9 z u: f7 K - self.flashers = set(), X, ^& q, C0 `- V4 P
- / D, K P: p* f9 k: p/ I
- # Draw a background& o6 \! o" Y4 g) f0 A9 r5 D
- for x in range(self.row_count):8 [& i- q, H$ c; f A
- try:' S V, e9 g% P* W$ _: q
- screen.addstr(x, 0, ' ' * self.col_count, curses.color_pair(1))
1 c1 o# P: N' M( U. {' k - except curses.error:) K+ V3 g6 I: e: N/ e! R3 x
- pass- o+ [- @- F7 Z+ E4 B
- 0 E8 R' O5 T) c: N* O; y( Q
4 f9 b) u' S% L5 B- class Status:
2 `0 b% B# E6 z; S3 u9 g9 I - """
; r8 I5 B' a% r( k3 G - Displays a status message at top left when a setting is changed., L4 v- `+ Y3 i: C2 W! n
- """) W8 [8 @3 G4 A9 c
% U6 A1 E% Z. r- def __init__(self, screen):
S4 N1 }# D8 S+ Z2 Y: C - curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
0 d! o$ f3 v7 n% l$ v8 N+ u - self.screen = screen) U( d* H: K: x/ o( t
- self.countdown = 0
6 c& R: ^# r2 }+ I( a+ H - self.last_message = ''. h& I2 P6 H; H$ @ I, J
9 O6 E6 N. v8 d. t& n0 X4 ^- def update(self, message, delay):" f+ j+ J0 f% K: Z8 w3 U
- """+ |' O) K8 m% x
- Writes new message to the status area: I$ }' y' O- W" J0 e
- """
, w6 y5 y. N: ]7 g( ^. J' H - if not args.status_off:( s" X6 T! n. S& b
- message_str = message.ljust(11)
+ E; B3 I) J4 c - self.screen.addstr(0, 0, message_str, curses.color_pair(3))- S# i1 r) I, v! R6 Z n1 T8 N& |3 w
- self.last_message = message_str
* Y% i! K- ^- \+ J - # More frames for faster speeds:
2 q; J' [* J, m' M" l; H" Z# `" Q8 F - self.countdown = (100 // (delay // 10 + 1)) + 2
: u3 O. d/ D% C% ]- k% J - H. B; `8 h5 K7 q$ n
- def refresh(self):* v7 B0 M6 t! ?) O+ d
- """* b; g& S7 f- K# t" i3 y7 t& p
- Used to keep refreshing status message until countdown runs out
' ~0 N) L" ?6 z% r - """
$ V3 u% g u* m: Y" A7 P( ? - message_str = self.last_message
% m6 H/ ] V" ` - self.screen.addstr(0, 0, message_str, curses.color_pair(3))
* ~; j, h6 m& R' i: q
! R/ @5 J4 c$ Y0 t9 X9 J- def clear(self):
1 L) d: a# w6 b4 U: \8 |2 s9 M6 J - """
& t5 O+ a9 w$ ~8 a, X9 x - Erases message with spaces when the countdown runs out( {8 e) a$ |1 e: e
- """
% ^& \! ^) O Y0 d* k+ o8 ] - self.screen.addstr(0, 0, ' ' * 11, curses.color_pair(1))
) i, |* M8 x; q2 Q/ s1 m/ _3 ^2 g - ( J7 [/ p( j8 ~( h |: x# a7 P0 u
! J5 W8 x) Z8 I- class Column:
0 _ m! j" f% x. { - """- s* M4 O1 v8 p! b
- Creates nodes (points that move down the screen) that are then stored in
# Y* ?. r% P. r4 Y) t- X - canvas.nodes. Countdown timer determines time to spawn new node.
. b& q& I7 z" P, ~ - """8 M( |* {; T# A
; R; F8 h4 b$ ?! ^) ^- def __init__(self, x_coord, row_count):6 }- r" Z, G! G8 i' w
- self.drawing = None # None means not yet. Later will be True or False+ }" z+ s+ J7 Y& O
- self.x_coord = x_coord) \! J8 L! t8 \
- self.timer = randint(1, row_count)
! a0 {$ c3 a' v( @) p - self.async_speed = randint(1, 3)
8 F" i! D* y: D& s - if args.single_wave:8 V7 T+ j4 t! v+ i! i8 F. D. x
- # Speeds it up a bit' s8 n \/ D' }8 d3 \
- self.timer = int(0.6 * self.timer)3 s2 R v \. b7 n9 d( g
- 9 A/ K. Y! | p
- def spawn_node(self, canvas):
4 y$ ?+ u6 _( k: T/ R9 A* @7 P" k8 X* [ - """
% E/ K; M. d: S. i$ @+ B - Creates nodes: points that move down the screen either writing or' ^, p. m' J$ Z8 m1 q, X. [
- erasing characters as they go down; v$ H/ G; W! H6 W& @& q
- """; y) A. L$ o* f
- if args.single_wave and self.drawing is False:
0 R, t2 e! ^$ g) q' `5 q3 B; ?, Y - return- j' k% o4 R4 L( e# B5 S5 d
- 7 C \* z7 w' H8 ]9 h
- self.drawing = not self.drawing
. ~( e( X5 L4 X3 n8 |. D; d
1 S4 I, H5 P- F8 S! x7 W a4 _- # Multiplier (mult) is for spawning slow-moving asynchronous nodes
& e' k, v2 G; J0 P9 @# a+ U }8 Z2 C - # less frequently in order to maintain their length
% ^: s0 Q3 t. W& A. S1 |2 Q) ~ - if args.asynchronous:
6 C# G6 i3 j# P6 r' t - mult = self.async_speed
. i7 q. |( g* t3 t2 x& P% N, _, j - else:- e0 c: i/ Y! B3 b4 |
- mult = 1
: R4 n) e8 ]8 k7 M3 I% y
: ?5 G2 V1 e4 |) T% g+ N" ]% U0 }- if self.drawing:
1 L0 ^( ?' H+ V% A' _# t - # "max_range" prevents crash with very small terminal height- Q. D" |& o4 ]0 q+ [+ t5 Q# e
- max_range = max((3 * mult), ((canvas.row_count - 3) * mult))% {3 Y, h8 u' R9 ~
- self.timer = randint(3 * mult, max_range)
_2 J3 `4 K7 E - if args.single_wave:% _9 k, ^2 _2 j! M7 C
- # A bit faster for single wave mode
S) }1 \. l4 E' i& | - self.timer = int(0.8 * self.timer); v9 s' \) P" k" P' @& L$ s6 ?
- else:
* g4 ~3 g& Z" o: U0 Q - self.timer = randint(1 * mult, canvas.row_count * mult)6 a4 J) _$ e4 T
& h4 g R8 _7 h0 C" Q8 G. I9 i% x6 _- s- x = self.x_coord
) l4 `* k" j6 ]. I7 {( _) q - n_type = 'eraser'# z# _$ G2 r& L4 L2 \) B
- async_speed = self.async_speed% \3 g3 h$ Z. ~
- white = False
/ r( R& |$ c+ X9 k - if self.drawing:: [3 b0 s4 F; d0 r
- n_type = 'writer'4 B; c/ z) m: i- Y1 ^# I( N
- if randint(0, 2) == 0:6 {7 l2 R( N( _- P5 l- G: g
- white = True$ v# R; [+ m4 K' W
, Z& B1 e# e# |- canvas.nodes.append(Node(x, n_type, async_speed, white))
# ^* g' I3 r- ^, t6 x$ Q - 3 Y: ?% n8 J8 _8 u) f# v1 q
- - ~+ l0 |, w2 i; x" }, w
- class Node:
0 o+ {$ o5 n6 P- w l/ C+ ` - """; b, s+ r% k. w6 C
- A point that runs down the screen drawing or erasing characters.$ Y$ x$ Z. i2 F- H
- n_type -> 'writer' or 'eraser'1 `& h$ t# L- }) q% W
- white -> Bool. If True, a white char is written before the green one.+ s5 p/ M8 x' ^) o+ E6 K/ b1 s- v+ B
- last_char -> Stores last character, since white characters have to be
8 @2 T3 Y9 P* }! j - overwritten with the same one in green one.
0 n: z) f2 O& x, k* l7 H - expired -> Bool. If True, node is marked for deletion0 g$ E, X' ~9 X% Q( {+ x% ]$ f) f
- """
! a) R P% S1 B - ) I( _" z9 W6 ]! N
- def __init__(self, x_coord, n_type, async_speed, white=False):
6 i; I+ y( a# }8 x2 J- k - self.x_coord = x_coord
, S% k$ o7 H# t- u5 \$ b% p* d - self.y_coord = 0
1 J5 N$ a- ^; U( S; J0 T - self.n_type = n_type2 k3 q4 a I. u+ A- S( \, B! E& }
- self.white = white
- y8 `+ U+ F; T# h1 u* f) j2 @4 U - self.last_char = None
/ H$ C, |2 ^. ^, E( b3 w, P k - self.expired = False E! E% @, ]) O" e/ p$ {& X) @% E T
- self.async_speed = async_speed
- {: p9 a& I% l. l - 8 U3 R5 m+ p: Y+ p ~
- * U/ `% c+ M" a" E- ?, U/ G
- class KeyHandler:
# Q* j0 n, o! Y" r2 L9 g( u - """! D% h/ J, U& B9 e
- Handles keyboard input." |7 l: A8 n- e7 T) c8 X
- """
; I$ N5 |" f+ A) ~7 k - ' ], ^, Q1 N6 q f) T
- def __init__(self, screen, stat):
2 [7 J8 b" J. h& f* A - self.screen = screen, K7 P1 U: d, N! o+ m
- self.stat = stat% ~: k. ]! o4 a2 S6 y' ?
- self.screen.nodelay(True)( H4 @- D8 L* ~* a
- self.delay = start_delay
4 n: w4 g3 |0 o6 O - self.fg = start_color! C3 ^" o+ K2 ?4 ]
- self.bg = start_bg E2 E" f. o2 l2 w& y
4 G; A1 x ?9 T: Z. l" k5 \! `- def cycle_bold(self):* I) B# s2 p9 O) x' a! {! w
- """
. C0 E) m7 I: r) t - Called on 'b' press. Cycles though Bold options: P; c8 \- t6 d
- off -> on -> all bold `1 T4 I% D0 [
- """
: u, y2 R C8 u5 r1 h0 l - if args.all_bold:3 I6 I8 E2 x4 O" o% C/ ]+ E
- args.no_bold = True, S9 `% g+ e$ o, ~/ s
- args.all_bold = False
3 I9 r3 b( w9 s/ {4 b2 m( H - self.stat.update('Bold: off', self.delay)) j( ]8 A. f: {4 `7 ]: I
- elif args.no_bold:
& ]* Y' z4 [- G' _: J9 D9 V9 ` - args.no_bold = False5 w& i+ ?* A/ E7 X. y
- args.all_bold = False
$ g$ ^% l+ T0 |& n1 U - self.stat.update('Bold: on', self.delay)
) G1 K" J0 d5 Z/ J - else:
) w; C0 P0 |4 d3 U - args.no_bold = False
! I+ @" ~" q6 @, ~: ]7 V - args.all_bold = True
4 F, a% z! e, q5 n- f/ _' R. D - self.stat.update('Bold: all', self.delay)# n. U0 Q6 j1 N/ i3 B
# O2 w& F% _# g( |3 U! U- `- def get(self):
3 l4 f+ K4 u' y [; G( f - """
! U# a5 ]- R% s$ [ - Handles key presses. Returns True if a key was found, False otherwise.
" _$ Q& h+ t* @ - """
& {; A( J B, W& B - kp = self.screen.getch()
+ e% W3 m0 S7 E+ E' a
2 s( n# R [! f% m6 _; b- if kp == -1:5 e( }: l: B' y5 d% M% y7 m3 p3 w
- return False
2 R; C9 W) n/ E+ L! X - elif kp == ord(" ") or kp == ord("q") or kp == 27: # 27 = ESC
: \; U$ h# k9 d1 X, o0 q, ]. s - exit()
X* A1 P9 J3 [9 W* l! [ - elif kp == ord('a'):8 p, L+ J% L. p9 B! e& Y# V9 p' c
- args.asynchronous = not args.asynchronous/ A2 n" p. S* O. y. }. `* z
- on_off = 'on' if args.asynchronous else 'off'
1 A" n: @8 S& \! {% k( I - self.stat.update('Async: %s' % on_off, self.delay)
$ x6 c1 i4 Y$ o1 k' S - elif kp == ord('b'):2 B8 g* u$ k' c7 e" y. [* F8 f$ b
- self.cycle_bold()
% ]- ]( c1 |; ]4 x$ r4 m - elif kp == ord('f'):( I# S% c; y2 r. m1 ]4 C! ?+ X
- args.flashers = not args.flashers
8 \4 X; Q: q7 H - on_off = 'on' if args.flashers else 'off'
; F% L% H+ }5 |/ ` - self.stat.update('Flash: %s' % on_off, self.delay)' U4 G: B4 a5 E8 K8 f
- elif kp == ord('o'):/ ~4 v8 N0 z: I# A
- self.toggle_status()
1 V, z3 v8 X' R$ L4 I/ r - 5 J) n) F C/ f# _/ @
- # Speed control
' {) O' B! J: M7 C, Z - elif kp == ord('-') or kp == ord('_') or kp == curses.KEY_LEFT:' X& f$ a$ P/ C# G& Z# q. B
- self.delay = min(self.delay + 10, 10990)9 _9 w) K9 z/ w$ R' M- v( u7 k
- self.show_speed()
# ]0 F7 J$ Y# f4 O) O9 P9 Y% q - elif kp == ord('=') or kp == ord('+') or kp == curses.KEY_RIGHT:
" j# Q3 y) H6 ]/ y - self.delay = max(self.delay - 10, 0)
0 u) f2 }( C6 | - self.show_speed()5 b/ k& m( A% t6 H
- elif kp == ord('[') or kp == curses.KEY_DOWN:. \% A& E: _ |8 z3 E) j# f
- self.delay = min(self.delay + 100, 10990)
$ o" @, B9 q- D7 V/ | - self.show_speed()
8 l% v) o8 u. c1 X( h - elif kp == ord(']') or kp == curses.KEY_UP:
5 n) `" x6 ~3 _/ R9 A( I - self.delay = max(self.delay - 100, 0)
; G# J: O i+ G0 y; R k - self.show_speed()6 E2 G+ H& |' Z) j* K
( U \* ]+ ^0 C) `- # Foreground color control
. n+ c/ A+ \ G& ]% u - elif kp == ord('1'):
8 `6 `" Z9 e2 }/ o! X4 l @+ G - self.set_fg_color('Green')
) t: ?; E7 t4 \/ ^% B+ f - elif kp == ord('2'):% Q: c* F+ ~: A7 v, O
- self.set_fg_color('Red')2 H. z7 q$ s* O# }# X9 J
- elif kp == ord('3'):
: t+ S4 Y1 W7 n" @; @2 E - self.set_fg_color('Blue')
- v" w: T0 j3 B0 o - elif kp == ord('4'):& T* \6 n# @ m/ s1 D
- self.set_fg_color('White')
* e) H( J, \# Y/ J2 O/ b - elif kp == ord('5'):5 o6 q2 {9 _% W7 g5 ~3 {5 f9 ]
- self.set_fg_color('Yellow')
) U9 C; h& w' w% O, `' n0 L, L9 \ - elif kp == ord('6'):
4 `5 H& q, x1 U; p$ h1 e4 p - self.set_fg_color('Cyan')+ e0 N" w0 T9 u" C8 u( b8 a
- elif kp == ord('7'):9 @3 e- q- u6 F7 s. p4 i0 [
- self.set_fg_color('Magenta'), d2 F4 t$ w7 Z* }/ D2 v
- elif kp == ord('8'):
1 u# U! e1 e5 c# N - self.set_fg_color('Black'): m3 ~5 R; O6 H" U: [1 F. |
- elif kp == ord('9'):
. ?4 v* F: N# b. ^8 }5 w - self.set_fg_color('default')
7 k, V+ A3 Z2 P2 `# {% l6 B - " J5 {7 f+ i l+ L, f
- # Background color control
9 ?9 K. i/ I, ?+ b4 C2 A/ ^; `) _ - elif kp == ord('!'):
, P% K; s) I. [0 j - self.set_bg_color('Green')
1 `: T0 N7 j* B8 m/ q- g | - elif kp == ord('@'):% T- b+ |4 W. T4 Z! j" F5 I( f
- self.set_bg_color('Red')
/ `' P4 j" R v$ j- y9 U - elif kp == ord('#'):7 B. k6 c/ Y# p9 ]* G6 _5 j
- self.set_bg_color('Blue')
! c, `% z9 u9 Z, H) U- k - elif kp == ord('$'):
+ j! ], a: {( Y4 A v - self.set_bg_color('White')
' U: A' o8 I& c' t4 Q4 L1 Z7 l - elif kp == ord('%'):
0 g! P- W G! K# W8 {1 \ - self.set_bg_color('Yellow')
2 I T: d- K5 N0 T! Y. m! m. @9 o1 D - elif kp == ord('^'):
8 o8 @5 J: [) g6 M! H6 i# X - self.set_bg_color('Cyan')( c% z& [, W+ z+ ?9 V; Z @) A1 q
- elif kp == ord('&'):
- S, ]4 K2 l1 _. f9 y0 n - self.set_bg_color('Magenta')
. Z9 d% v, J; h% B( l, a: J - elif kp == ord('*'):
" }! f% c9 a/ D/ ~" a2 T8 j0 V+ H - self.set_bg_color('Black')9 s3 J" }& A: t9 x. d
- elif kp == ord('('):9 A% ? S5 ?# `1 _$ i
- self.set_bg_color('default')' p/ }8 v. b `
! O2 N6 V3 Y9 n, z7 Y2 F% [- return True& E3 Y2 g5 k2 v" e, D+ u+ V4 }
- 5 T" i; \* r7 K7 Y
- def set_fg_color(self, name):
1 t( `9 ^6 A9 w2 {% I5 q/ B - """% w+ p. [; g, k4 Y$ @8 T8 E
- Set foreground color
' S9 {( e" U: {! @; m7 q - """
* k5 Z9 u' _- H3 { - self.fg = colors_str[name.lower()]
! I" X) m4 q, i" E: s2 \/ Z6 Y9 \* I - curses.init_pair(1, self.fg, self.bg), L' L8 E: b/ j; l \% {) U5 k
- if name == 'default':
0 I' S4 I1 |- l+ V - name = "Def't color"; w( Y6 v7 i- [0 I5 d
- self.stat.update(name, self.delay)# Z. v4 Y( F2 f4 `
$ b! w/ p& k( ^- def set_bg_color(self, name):
" d; i4 L8 K" v7 u' p0 e5 x - """
; [% r( `, [ g* L0 e8 J - Set background color
- r b6 p* w# o2 C% k8 N5 E& x" y - """
# G% A( g; O" R - self.bg = colors_str[name.lower()]* ]! `+ q4 A! L- y. t/ `
- curses.init_pair(1, self.fg, self.bg)) r9 i* M) u$ Q
- curses.init_pair(2, curses.COLOR_WHITE, self.bg)( y3 Y$ @* E( E- ~( S9 a
- self.stat.update('BG: %s' % name, self.delay)# e2 d9 V. t0 f
- T4 @# E9 \( u% Z
- def show_speed(self):8 H2 Y G$ F) |/ |9 {/ H h
- """
" e" g- H: ]# P - Display current speed (-999 to 100) when it is changed by keypress
2 q1 s+ B' U% u% x" E" U6 t - """
) I% X0 v' I5 A9 Z$ X* D$ M - self.stat.update('Speed: %d' % (100 - self.delay // 10), self.delay)( g4 R0 r! q) V
- " T( S9 A# k% D/ P2 L- d, c
- def toggle_status(self):
( w$ s% A4 K# u% t5 N' H0 L - """( g# s7 ^+ H) [2 _( C) ~
- On 'o' keypress, turn status display on or off" @6 ]& t$ x7 y$ c/ I, O+ j0 P/ O, W
- """+ h$ A* `5 N' }, C. O0 X9 H
- args.status_off = not args.status_off
: n2 K1 n P) V1 T4 @ \2 v - on_off = 'off' if args.status_off else 'on'1 n& l- O7 i5 r( J, V8 ]0 x9 g
- self.stat.update('Status: %s' % on_off, self.delay)
6 H! k# t- [8 A1 f6 x - 9 ^( \: h! z" H8 ?1 H- |
* S; B/ _" _" }8 j- }& P- class Writer:) H4 E/ T9 Z- h6 K+ |8 J, w
- """
% R, N& L% U! e9 s - Initializes character writing options and contains methods for writing and
6 y4 l. g# q ?) g - erasing characters from the screen.0 T0 `) r) d4 x. L; O$ ~9 S
- """$ o. Y$ ]# l+ |& {
- . u( H/ y; ~, l) H* m" E
- def __init__(self, screen):: l. k& J5 g- M4 H2 M7 E: x
- self.screen = screen! a/ y- Y5 _! ~- {* l
- self.screen.scrollok(0)
0 N4 d) g! k7 v6 q L" _' N/ p - curses.curs_set(0)% C. Y& g2 ]- _# n p
- curses.use_default_colors(): K, m5 K' A" B9 H1 g
- curses.init_pair(1, start_color, start_bg)
' h, o3 o2 j2 r - curses.init_pair(2, curses.COLOR_WHITE, start_bg)
, F. c% E8 t6 M% O - curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)$ ^/ B( ^" g& N6 r$ }) C) p( ]
- self.white = curses.color_pair(2)$ C+ \% v# \" ]& @5 |! F
- $ T+ i5 j8 M5 K- T3 Z
- @staticmethod
" z7 L( O$ A9 {* |" w - def get_char():
- ]9 U( E" l( Y A* c5 c% N - """
- B9 N# u3 z4 L5 \ - Returns a random character from the active character set
& O/ C( g% \4 D5 a - """
1 L5 [' d: ^$ J% u* k - return chars[randint(0, chars_len)]
% R7 }0 a+ I- {. q9 B. h% h
. V$ D6 Y* y& U. u" a- @staticmethod
0 Z# F1 K' a: S- F+ O6 M - def get_attr(node, above=False):
3 x" C0 l9 k: F( A4 P [$ } - """1 `2 r. J' S7 d! t% Q X
- Returns either A_BOLD attribute or A_NORMAL based on Bold setting0 f8 O) C3 Q( N* y+ G
- "above=True" means it an extra green character used to overwrite the% S- W$ A( h* A
- while head character.
, ^; O0 L3 M2 [, e - """, G- u6 x; `# x
- if args.no_bold:
7 r$ m$ b- P) N* W7 Q1 @ - return curses.A_NORMAL; j8 H3 k, V r) I q3 e
- elif args.all_bold:2 I( f( \/ O2 i; b/ w
- return curses.A_BOLD
0 _# }2 ~8 f! q: w6 z1 k - else:" E5 R6 D& o4 E/ }; o! `2 y: {1 g
- if node.white and not above:: C& X' x# h3 c
- return curses.A_BOLD
3 ] ^7 Y b8 n, n. D! v - else:
+ ~5 U0 Q ?, u. x - return choice([curses.A_BOLD, curses.A_NORMAL])$ h9 J2 i% t. ^- y' Z& J
( e( i1 f4 e5 M' z9 Y+ ?8 B- def draw(self, node):$ E6 f4 k# e4 j
- """
. P$ ^1 E. a3 ^4 a4 x$ H - Draws characters, included spaces to overwrite/erase characters.
1 g) n% g9 N5 b0 ~ - """3 |9 E/ H4 P" `! v1 c X8 r3 H
- y = node.y_coord, D/ }3 K; d4 `7 p3 `5 `
- x = node.x_coord
9 g5 p4 d: m1 ~ - character = ' '
0 r; a. ` N( ]" o! o+ \( S+ Q - attr = self.get_attr(node)% t! |1 M: q6 k' I& C1 L) t$ k4 n
- color = curses.color_pair(1)
( Z- q4 g& U5 b, X$ k9 c* C' _! E - if node.n_type == 'writer':
0 P [* a" c4 s4 z7 x/ \ - if not node.white and node.last_char:! S: z* I9 j2 N$ G# n+ V
- # Special green character for overwriting last white one
5 S Z, g! F4 f$ Z2 P - # at bottom of column that was not being overwritten.: }2 e/ D, ~: H( j ~) V: B
- character = node.last_char
, v7 ]3 c% M( S, F; j7 G - else:
% q; I. X2 b( `2 a/ [' a Y - character = self.get_char()
/ {3 q5 F' L: Q- | - if node.white:
# T4 L# L. t: _. c0 ^( L - color = curses.color_pair(2)3 `; f7 f d4 ]" H5 z0 M1 X
- 6 D0 L$ o+ y2 _! Q4 s' Y7 T& e
- try:. R W7 H" Y4 j9 g8 @: z9 j7 U
- # Draw the character
# m7 v, S n0 q4 R% [7 G, Q8 N - self.screen.addstr(y, x, character, color | attr)
0 a. \( w2 M5 {% a* s3 o - if node.white:
% p$ u- b+ T' F4 k7 ?8 |% S' i7 v1 H - if node.last_char:
# z8 L1 F0 C8 ^1 p: r/ D6 f - # If it's a white node, also write a green character above m2 B/ z t' d8 p2 ^0 o* L! z- u
- # to overwrite last white character
1 Z; n% f4 ` B- ?2 y: q - attr = self.get_attr(node, above=True)6 W% s7 J; }2 k
- self.screen.addstr(y - 1, x, node.last_char,
8 U3 J4 Z; i) ~2 W - curses.color_pair(1) | attr)
% C* l8 e9 @' ` - node.last_char = character
/ b$ C) P1 a3 y6 p# k6 S - except curses.error:
. J( S# g: ~ K" T i6 |/ c - # Override scrolling error if characters pushed off the screen.% s. _4 V2 g+ \, r2 e* `5 \
- pass
. X+ I1 G$ v8 A1 o) K - , X+ Y0 v0 d! z; n X/ W
- def draw_flasher(self, flasher):
, H: d6 N6 d3 y - """
1 X6 }' H, ?. _* r. Q# A - Draws characters, included spaces to overwrite/erase characters.
6 j( _0 n0 s- S# a - """% \ F; o% m8 ?( y
- color = curses.color_pair(1)
: e8 A# @& ~' ?4 f, I - attr = choice([curses.A_BOLD, curses.A_NORMAL])
; Q; I' q) x! k0 u - y = flasher[0]
2 h4 A3 G9 K' {) ^- S" R/ {2 O) C4 a5 c - x = flasher[1]
+ u/ R# \: T8 Q3 Q' R3 C. N - try:" z" p# w, r$ a7 v5 u) u7 x
- self.screen.addstr(y, x, self.get_char(), color | attr)
$ k! y. a! P ^ - except curses.error:
) i- F' }! |" G2 X: ^ - pass1 V7 m/ ]3 i9 c( T4 ~
- 8 @3 ^0 E1 x. u% `, }1 ?& r
- 9 ` }( S& w3 b( W& }& H
- ### Main loop* T+ g. k) w7 U E, F+ S9 t. i
- % \3 J& z' j/ T5 n! t
- def _main(screen):
9 Z+ W; d, a! E( ^4 _9 S - writer = Writer(screen)9 ]- O% t" B% S/ c9 j# X: h8 q
- stat = Status(screen)
/ i0 r6 \% y: ?( L) T! P7 a2 G - key = KeyHandler(screen, stat)
- D( o! b' F$ W) f$ k - # Prevent single_wave mode from shutting down too early:
6 h# l6 W1 a$ n6 H% ~8 f) L - if args.single_wave:
, L ?) M" o, J- T7 Z" r - wave_delay = 108 @6 b( V8 {9 O1 j. @# X" B
- else:
- V. s7 U; H7 f - wave_delay = 0
0 q9 W% J2 z0 M4 D
8 h; H) L8 I _6 {* d# L; m' s- starttime = time.time()3 k) p$ y% f% n- I& K* m" B2 X
7 M( |& K' B. u! ], S4 I9 ?- # Keep restarting however many times the screen resizes) p. N! f- {' P$ o7 ^
- while True:4 d+ q% S( x. Q8 B2 y3 w# \
- canvas = Canvas(screen)) Z2 l" R2 a# v) {2 ~
- # Set a rhythm for asynchronous movement
+ T" w$ v* E' ^* i/ Z! ^% j - async_clock = 5
( z% `/ M& b+ o8 l - # Loop to draw the green rain
( H" ^2 |! ]: I! O" l9 Q - while not canvas.size_changed:
R2 Z# |3 _ C! @. ]' L - if runtime and time.time() - starttime > runtime:
% ?: Q: r4 x6 Q P0 _6 a5 X7 O - exit()$ |/ {- `+ k- T4 _4 j3 Z
- # Catch keypress
- r; B% [3 I; Z: v: J* b( A - if key.get():9 \% }2 n! p! k) v" N
- continue7 _( p$ ?& J% \1 K* ]7 |
- # Spawn new nodes* R( O' J5 f4 W0 W( s
- for col in canvas.columns:
5 } g7 [3 Y" \; S* h3 n - if col.timer == 0:
p H) _6 i& Y6 y P - col.spawn_node(canvas)+ X" x3 h( S+ m$ M9 j- @
- col.timer -= 1
0 `& ~7 ^6 h: x - 7 R# u5 E3 g8 }0 o$ \
- for node in canvas.nodes:( z# [/ G/ K8 f! G6 c
2 d% W4 \0 [, A' Z/ D J- if args.flashers:+ |8 K; L) q& Q; K
- if node.n_type == 'writer' and not randint(0, 9):6 \) J; n) l5 z3 ?$ U9 C
- canvas.flashers.add((node.y_coord, node.x_coord))
6 A) `5 F8 N+ H/ Q6 k* q - elif node.n_type == 'eraser':2 o9 o3 ]5 [/ Y$ q) j
- try:
! i# n! r2 F- o2 P; B: c2 J9 p1 e4 b - canvas.flashers.remove((node.y_coord, node.x_coord)): Q5 z" f8 ~ J) W5 f6 k! o
- except KeyError:, Z8 J3 j- p' q, }+ s
- pass U6 S1 _- [- W1 |6 e
5 x9 Z1 L- ^3 m7 y1 `( l* }- if args.asynchronous:
) `2 C% e0 J! A4 k1 [) J) k. K' j - if async_clock % node.async_speed == 0:
4 ?) k4 ~& j9 R* d - writer.draw(node): K7 {) h4 n$ @2 R7 T |3 @! G
- node.y_coord += 18 w6 \% A; Q! r# p" q: t$ B
- else:% f h. P, r( f0 A
- writer.draw(node), s! E1 H- w8 Y# R
- node.y_coord += 1
: W$ s- Z8 X$ V+ { - 6 p, O* r/ R+ U! A' z
- # Mark old nodes for deletion
" L' g6 |3 Y D3 Z& [% M - if node.y_coord >= canvas.row_count:) A5 O# F; b! C5 k
- if node.white:7 l% k$ o: K9 {; u4 q
- # Stop white nodes from staying 'stuck' on last row.
, G W* d2 I/ P* t* u: |3 n0 L - # Creates a special green node with a last_char; d: \: ?0 T& b# j8 g* }2 k7 l
- # attribute to overwrite last white node.% _) R! X- T4 W* e# H
- node.white = False
3 X, d7 W5 M# x- D9 X - node.y_coord -= 1+ r# h0 t% ~7 [& E% G D; i" }2 J
- else:, Z+ t2 [7 L# X# S ]
- node.expired = True( O% e- X$ V2 z5 ~; i/ L7 X& v
- / ?4 A; \+ D8 h6 l% }
- if args.flashers and (not async_clock % 3):
s9 Q; c! [# s% D6 S, t4 w# K - for f in canvas.flashers:
v6 _( j- i9 [! N0 P# G2 ^& |) p& Q8 p - writer.draw_flasher(f)) G( H8 B2 e2 U
- & D7 d2 g+ X- l b! K0 G& J8 e' t' I
- # Rewrite nodes list without expired nodes5 I( n% l0 K; I9 S
- canvas.nodes = [node for node in canvas.nodes if not node.expired]8 r6 B. r+ N9 r4 d8 `
- _, y* h8 d" o9 w0 n* ?/ r7 K- if args.single_wave:
0 s3 M8 B( Q9 J& M - if len(canvas.nodes) == 0 and wave_delay < 0:2 e5 y' J9 f- ?6 _: \7 a$ a5 A9 s
- exit()! d% u4 M3 z6 ]- X/ E% X
- wave_delay -= 1
2 Z3 L8 i& R" r, ^( w - : G9 S# y8 s7 t: b- [( y
- # End of loop, refresh screen$ I n( S$ \8 V/ y/ ~
- if stat.countdown > 0:! a( |; Z7 u. H0 ]
- if stat.countdown == 1:& b1 c7 b1 ?) D- ?) |% y* e
- stat.clear()
9 H- K0 P0 Z+ g# P1 J - else:
! I1 ~, ?& F$ w' K( A, v, V6 v5 r- d* l. h - stat.refresh()
- [9 Y$ q, w6 m0 D4 u - stat.countdown -= 1
) `" O( k' g) ^: W - screen.refresh()
r; T7 i" P ~5 V. m. f* ]% ] - : i/ q. X/ U& u9 U8 r. E! v$ j/ ^+ k
- # Check for screen resize
8 U" t* G4 x- }+ a, r" |. w8 q - if screen.getmaxyx() != (canvas.row_count, canvas.col_count):7 k7 n. _! [- O* X
- canvas.size_changed = True& b! r) L( G+ Q- v! e. W' R
+ W C- ^4 ~3 v: N3 u9 {6 e% F- # Add delay before next loop
. c- y q7 d5 F: N; j7 n - curses.napms(key.delay)
( c2 o! V. \' ~/ k, ?. {0 e% m
7 y/ H1 i! j1 e/ y/ V" U' F- # update async clock8 T5 J H+ E2 o
- if async_clock:4 P; Q2 A6 l$ m
- async_clock -= 10 c- T" r5 G4 H3 t) ~5 y
- else:
+ b: f* V0 a6 Y1 M. M2 s - async_clock = 5; E% ?& A9 p/ i) A
+ a* \4 q3 \' s4 \ Y# J* k% y- 7 x0 l! [( f! F- I, A4 u% Q
- def main():
% N8 M/ W% M2 f - # Wrapper to allow CTRL-C to exit smoothly:
. M/ k% r Y0 X8 \0 U F - try:
) `. e. H5 v: l - curses.wrapper(_main)( G! b* n: ]' {1 e
- except KeyboardInterrupt:! c/ [" C+ A, t/ e6 i
- pass4 S4 Q6 Q/ ?" N
4 @1 c) N: E- K% W5 D+ S+ ~9 M
2 J9 Y. D+ f! H4 o0 j" d- if __name__ == '__main__':2 _! M/ }, A4 a; k0 a- X, c. i
- main()6 A8 S# L3 E* `1 |+ Q
复制代码 |
|