Back to index

python3.2  3.2.2
Classes | Functions
life Namespace Reference

Classes

class  LifeBoard

Functions

def erase_menu
def display_menu
def keyloop
def main

Function Documentation

def life.display_menu (   stdscr,
  menu_y 
)

Definition at line 148 of file life.py.

00148 
00149 def display_menu(stdscr, menu_y):
00150     "Display the menu of possible keystroke commands"
00151     erase_menu(stdscr, menu_y)
00152 
00153     # If color, then light the menu up :-)
00154     if curses.has_colors():
00155         stdscr.attrset(curses.color_pair(1))
00156     stdscr.addstr(menu_y, 4,
00157         'Use the cursor keys to move, and space or Enter to toggle a cell.')
00158     stdscr.addstr(menu_y+1, 4,
00159         'E)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uit')
00160     stdscr.attrset(0)

Here is the call graph for this function:

Here is the caller graph for this function:

def life.erase_menu (   stdscr,
  menu_y 
)

Definition at line 141 of file life.py.

00141 
00142 def erase_menu(stdscr, menu_y):
00143     "Clear the space where the menu resides"
00144     stdscr.move(menu_y, 0)
00145     stdscr.clrtoeol()
00146     stdscr.move(menu_y+1, 0)
00147     stdscr.clrtoeol()

Here is the caller graph for this function:

def life.keyloop (   stdscr)

Definition at line 161 of file life.py.

00161 
00162 def keyloop(stdscr):
00163     # Clear the screen and display the menu of keys
00164     stdscr.clear()
00165     stdscr_y, stdscr_x = stdscr.getmaxyx()
00166     menu_y = (stdscr_y-3)-1
00167     display_menu(stdscr, menu_y)
00168 
00169     # If color, then initialize the color pairs
00170     if curses.has_colors():
00171         curses.init_pair(1, curses.COLOR_BLUE, 0)
00172         curses.init_pair(2, curses.COLOR_CYAN, 0)
00173         curses.init_pair(3, curses.COLOR_GREEN, 0)
00174         curses.init_pair(4, curses.COLOR_MAGENTA, 0)
00175         curses.init_pair(5, curses.COLOR_RED, 0)
00176         curses.init_pair(6, curses.COLOR_YELLOW, 0)
00177         curses.init_pair(7, curses.COLOR_WHITE, 0)
00178 
00179     # Set up the mask to listen for mouse events
00180     curses.mousemask(curses.BUTTON1_CLICKED)
00181 
00182     # Allocate a subwindow for the Life board and create the board object
00183     subwin = stdscr.subwin(stdscr_y-3, stdscr_x, 0, 0)
00184     board = LifeBoard(subwin, char=ord('*'))
00185     board.display(update_board=False)
00186 
00187     # xpos, ypos are the cursor's position
00188     xpos, ypos = board.X//2, board.Y//2
00189 
00190     # Main loop:
00191     while True:
00192         stdscr.move(1+ypos, 1+xpos)     # Move the cursor
00193         c = stdscr.getch()                # Get a keystroke
00194         if 0 < c < 256:
00195             c = chr(c)
00196             if c in ' \n':
00197                 board.toggle(ypos, xpos)
00198             elif c in 'Cc':
00199                 erase_menu(stdscr, menu_y)
00200                 stdscr.addstr(menu_y, 6, ' Hit any key to stop continuously '
00201                               'updating the screen.')
00202                 stdscr.refresh()
00203                 # Activate nodelay mode; getch() will return -1
00204                 # if no keystroke is available, instead of waiting.
00205                 stdscr.nodelay(1)
00206                 while True:
00207                     c = stdscr.getch()
00208                     if c != -1:
00209                         break
00210                     stdscr.addstr(0, 0, '/')
00211                     stdscr.refresh()
00212                     board.display()
00213                     stdscr.addstr(0, 0, '+')
00214                     stdscr.refresh()
00215 
00216                 stdscr.nodelay(0)       # Disable nodelay mode
00217                 display_menu(stdscr, menu_y)
00218 
00219             elif c in 'Ee':
00220                 board.erase()
00221             elif c in 'Qq':
00222                 break
00223             elif c in 'Rr':
00224                 board.make_random()
00225                 board.display(update_board=False)
00226             elif c in 'Ss':
00227                 board.display()
00228             else: pass                  # Ignore incorrect keys
00229         elif c == curses.KEY_UP and ypos > 0:            ypos -= 1
00230         elif c == curses.KEY_DOWN and ypos < board.Y-1:  ypos += 1
00231         elif c == curses.KEY_LEFT and xpos > 0:          xpos -= 1
00232         elif c == curses.KEY_RIGHT and xpos < board.X-1: xpos += 1
00233         elif c == curses.KEY_MOUSE:
00234             mouse_id, mouse_x, mouse_y, mouse_z, button_state = curses.getmouse()
00235             if (mouse_x > 0 and mouse_x < board.X+1 and
00236                 mouse_y > 0 and mouse_y < board.Y+1):
00237                 xpos = mouse_x - 1
00238                 ypos = mouse_y - 1
00239                 board.toggle(ypos, xpos)
00240             else:
00241                 # They've clicked outside the board
00242                 curses.flash()
00243         else:
00244             # Ignore incorrect keys
00245             pass
00246 

Here is the call graph for this function:

Here is the caller graph for this function:

def life.main (   stdscr)

Definition at line 247 of file life.py.

00247 
00248 def main(stdscr):
00249     keyloop(stdscr)                    # Enter the main loop

Here is the call graph for this function: