Fixed issue-number mistake in NEWS update.
[python.git] / Demo / curses / rain.py
blob9d46e6eb600a1b52972063ccea9bee5cf59d9f29
1 #!/usr/bin/env python
3 # $Id$
5 # somebody should probably check the randrange()s...
7 import curses
8 from random import randrange
10 def next_j(j):
11 if j == 0:
12 j = 4
13 else:
14 j -= 1
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)
23 return j
25 def 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
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)
36 curses.nl()
37 curses.noecho()
38 # XXX curs_set() always returns ERR
39 # curses.curs_set(0)
40 stdscr.timeout(0)
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
50 j = 0
51 while True:
52 x = randrange(0, c) + 2
53 y = randrange(0, r) + 2
55 stdscr.addch(y, x, ord('.'))
57 stdscr.addch(ypos[j], xpos[j], ord('o'))
59 j = next_j(j)
60 stdscr.addch(ypos[j], xpos[j], ord('O'))
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('-'))
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('-'))
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(' '))
81 xpos[j] = x
82 ypos[j] = y
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)
92 curses.napms(50)
94 curses.wrapper(main)