1#!/usr/bin/env python
2#
3# $Id$
4#
5# somebody should probably check the randrange()s...
6
7import curses
8from random import randrange
9
10def next_j(j):
11    if j == 0:
12        j = 4
13    else:
14        j -= 1
15
16    if curses.has_colors():
17        z = randrange(0, 3)
18        color = curses.color_pair(z)
19        if z:
20            color = color | curses.A_BOLD
21        stdscr.attrset(color)
22
23    return j
24
25def main(win):
26    # we know that the first argument from curses.wrapper() is stdscr.
27    # Initialize it globally for convenience.
28    global stdscr
29    stdscr = win
30
31    if curses.has_colors():
32        bg = curses.COLOR_BLACK
33        curses.init_pair(1, curses.COLOR_BLUE, bg)
34        curses.init_pair(2, curses.COLOR_CYAN, bg)
35
36    curses.nl()
37    curses.noecho()
38    # XXX curs_set() always returns ERR
39    # curses.curs_set(0)
40    stdscr.timeout(0)
41
42    c = curses.COLS - 4
43    r = curses.LINES - 4
44    xpos = [0] * c
45    ypos = [0] * r
46    for j in range(4, -1, -1):
47        xpos[j] = randrange(0, c) + 2
48        ypos[j] = randrange(0, r) + 2
49
50    j = 0
51    while True:
52        x = randrange(0, c) + 2
53        y = randrange(0, r) + 2
54
55        stdscr.addch(y, x, ord('.'))
56
57        stdscr.addch(ypos[j], xpos[j], ord('o'))
58
59        j = next_j(j)
60        stdscr.addch(ypos[j], xpos[j], ord('O'))
61
62        j = next_j(j)
63        stdscr.addch( ypos[j] - 1, xpos[j],     ord('-'))
64        stdscr.addstr(ypos[j],     xpos[j] - 1, "|.|")
65        stdscr.addch( ypos[j] + 1, xpos[j],     ord('-'))
66
67        j = next_j(j)
68        stdscr.addch( ypos[j] - 2, xpos[j],     ord('-'))
69        stdscr.addstr(ypos[j] - 1, xpos[j] - 1, "/ \\")
70        stdscr.addstr(ypos[j],     xpos[j] - 2, "| O |")
71        stdscr.addstr(ypos[j] + 1, xpos[j] - 1, "\\ /")
72        stdscr.addch( ypos[j] + 2, xpos[j],     ord('-'))
73
74        j = next_j(j)
75        stdscr.addch( ypos[j] - 2, xpos[j],     ord(' '))
76        stdscr.addstr(ypos[j] - 1, xpos[j] - 1, "   ")
77        stdscr.addstr(ypos[j],     xpos[j] - 2, "     ")
78        stdscr.addstr(ypos[j] + 1, xpos[j] - 1, "   ")
79        stdscr.addch( ypos[j] + 2, xpos[j],     ord(' '))
80
81        xpos[j] = x
82        ypos[j] = y
83
84        ch = stdscr.getch()
85        if ch == ord('q') or ch == ord('Q'):
86            return
87        elif ch == ord('s'):
88            stdscr.nodelay(0)
89        elif ch == ord(' '):
90            stdscr.nodelay(1)
91
92        curses.napms(50)
93
94curses.wrapper(main)
95