Index: Demo/curses/life.py =================================================================== --- Demo/curses/life.py (revision 86744) +++ Demo/curses/life.py (working copy) @@ -1,6 +1,7 @@ #!/usr/bin/env python # life.py -- A curses-based version of Conway's Game of Life. # Contributed by AMK +# Mouse support and colour by Dafydd Crosby # # An empty board will be displayed, and the following commands are available: # E : Erase the board @@ -12,8 +13,6 @@ # Space or Enter : Toggle the contents of the cursor's position # # TODO : -# Support the mouse -# Use colour if available # Make board updates faster # @@ -140,10 +139,17 @@ def display_menu(stdscr, menu_y): "Display the menu of possible keystroke commands" erase_menu(stdscr, menu_y) + + # If colour, then light the menu up :-) + if curses.has_colors(): + curses.init_pair(1, curses.COLOR_CYAN, 0) + + stdscr.attrset(curses.color_pair(1)) stdscr.addstr(menu_y, 4, 'Use the cursor keys to move, and space or Enter to toggle a cell.') stdscr.addstr(menu_y+1, 4, 'E)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uit') + stdscr.attrset(0) def keyloop(stdscr): # Clear the screen and display the menu of keys @@ -152,6 +158,9 @@ menu_y = (stdscr_y-3)-1 display_menu(stdscr, menu_y) + # Set up the mask to listen for mouse events + curses.mousemask(curses.BUTTON1_CLICKED) + # Allocate a subwindow for the Life board and create the board object subwin = stdscr.subwin(stdscr_y-3, stdscr_x, 0, 0) board = LifeBoard(subwin, char=ord('*')) @@ -203,6 +212,15 @@ elif c == curses.KEY_DOWN and ypos0: xpos -= 1 elif c == curses.KEY_RIGHT and xpos0 and mouse_x0 and mouse_y