diff options
Diffstat (limited to 'Tools/demo/life.py')
-rwxr-xr-x | Tools/demo/life.py | 93 |
1 files changed, 47 insertions, 46 deletions
diff --git a/Tools/demo/life.py b/Tools/demo/life.py index 3cbc053..dfb9ab8 100755 --- a/Tools/demo/life.py +++ b/Tools/demo/life.py @@ -1,23 +1,23 @@ #!/usr/bin/env python3 -# life.py -- A curses-based version of Conway's Game of Life. -# Contributed by AMK -# Mouse support and color by Dafydd Crosby -# -# An empty board will be displayed, and the following commands are available: -# E : Erase the board -# R : Fill the board randomly -# S : Step for a single generation -# C : Update continuously until a key is struck -# Q : Quit -# Cursor keys : Move the cursor around the board -# Space or Enter : Toggle the contents of the cursor's position -# -# TODO : -# Make board updates faster -# - -import random, string, traceback + +""" +A curses-based version of Conway's Game of Life. + +An empty board will be displayed, and the following commands are available: + E : Erase the board + R : Fill the board randomly + S : Step for a single generation + C : Update continuously until a key is struck + Q : Quit + Cursor keys : Move the cursor around the board + Space or Enter : Toggle the contents of the cursor's position + +Contributed by Andrew Kuchling, Mouse support and color by Dafydd Crosby. +""" + import curses +import random + class LifeBoard: """Encapsulates a Life board @@ -31,7 +31,7 @@ class LifeBoard: next generation. Then display the state of the board and refresh the screen. erase() -- clear the entire board - makeRandom() -- fill the board randomly + make_random() -- fill the board randomly set(y,x) -- set the given cell to Live; doesn't refresh the screen toggle(y,x) -- change the given cell from live to dead, or vice versa, and refresh the screen display @@ -53,7 +53,7 @@ class LifeBoard: # Draw a border around the board border_line = '+'+(self.X*'-')+'+' self.scr.addstr(0, 0, border_line) - self.scr.addstr(self.Y+1,0, border_line) + self.scr.addstr(self.Y+1, 0, border_line) for y in range(0, self.Y): self.scr.addstr(1+y, 0, '|') self.scr.addstr(1+y, self.X+1, '|') @@ -62,21 +62,21 @@ class LifeBoard: def set(self, y, x): """Set a cell to the live state""" if x<0 or self.X<=x or y<0 or self.Y<=y: - raise ValueError("Coordinates out of range %i,%i"% (y,x)) + raise ValueError("Coordinates out of range %i,%i"% (y, x)) self.state[x,y] = 1 def toggle(self, y, x): """Toggle a cell's state between live and dead""" - if x<0 or self.X<=x or y<0 or self.Y<=y: - raise ValueError("Coordinates out of range %i,%i"% (y,x)) - if (x,y) in self.state: - del self.state[x,y] + if x < 0 or self.X <= x or y < 0 or self.Y <= y: + raise ValueError("Coordinates out of range %i,%i"% (y, x)) + if (x, y) in self.state: + del self.state[x, y] self.scr.addch(y+1, x+1, ' ') else: - self.state[x,y] = 1 + self.state[x, y] = 1 if curses.has_colors(): - #Let's pick a random color! - self.scr.attrset(curses.color_pair(random.randrange(1,7))) + # Let's pick a random color! + self.scr.attrset(curses.color_pair(random.randrange(1, 7))) self.scr.addch(y+1, x+1, self.char) self.scr.attrset(0) self.scr.refresh() @@ -115,8 +115,9 @@ class LifeBoard: # Birth d[i,j] = 1 if curses.has_colors(): - #Let's pick a random color! - self.scr.attrset(curses.color_pair(random.randrange(1,7))) + # Let's pick a random color! + self.scr.attrset(curses.color_pair( + random.randrange(1, 7))) self.scr.addch(j+1, i+1, self.char) self.scr.attrset(0) if not live: self.boring = 0 @@ -128,7 +129,7 @@ class LifeBoard: self.state = d self.scr.refresh() - def makeRandom(self): + def make_random(self): "Fill the board with a random pattern" self.state = {} for i in range(0, self.X): @@ -152,9 +153,9 @@ def display_menu(stdscr, menu_y): if curses.has_colors(): 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.') + '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') + 'E)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uit') stdscr.attrset(0) def keyloop(stdscr): @@ -186,10 +187,10 @@ def keyloop(stdscr): xpos, ypos = board.X//2, board.Y//2 # Main loop: - while (1): + while True: stdscr.move(1+ypos, 1+xpos) # Move the cursor c = stdscr.getch() # Get a keystroke - if 0<c<256: + if 0 < c < 256: c = chr(c) if c in ' \n': board.toggle(ypos, xpos) @@ -201,14 +202,14 @@ def keyloop(stdscr): # Activate nodelay mode; getch() will return -1 # if no keystroke is available, instead of waiting. stdscr.nodelay(1) - while (1): + while True: c = stdscr.getch() if c != -1: break - stdscr.addstr(0,0, '/') + stdscr.addstr(0, 0, '/') stdscr.refresh() board.display() - stdscr.addstr(0,0, '+') + stdscr.addstr(0, 0, '+') stdscr.refresh() stdscr.nodelay(0) # Disable nodelay mode @@ -219,18 +220,19 @@ def keyloop(stdscr): elif c in 'Qq': break elif c in 'Rr': - board.makeRandom() + board.make_random() board.display(update_board=False) elif c in 'Ss': board.display() else: pass # Ignore incorrect keys - elif c == curses.KEY_UP and ypos>0: ypos -= 1 - elif c == curses.KEY_DOWN and ypos<board.Y-1: ypos += 1 - elif c == curses.KEY_LEFT and xpos>0: xpos -= 1 - elif c == curses.KEY_RIGHT and xpos<board.X-1: xpos += 1 + elif c == curses.KEY_UP and ypos > 0: ypos -= 1 + elif c == curses.KEY_DOWN and ypos < board.Y-1: ypos += 1 + elif c == curses.KEY_LEFT and xpos > 0: xpos -= 1 + elif c == curses.KEY_RIGHT and xpos < board.X-1: xpos += 1 elif c == curses.KEY_MOUSE: - (mouse_id, mouse_x, mouse_y, mouse_z, button_state) = curses.getmouse() - if (mouse_x>0 and mouse_x<board.X+1) and (mouse_y>0 and mouse_y<board.Y+1): + mouse_id, mouse_x, mouse_y, mouse_z, button_state = curses.getmouse() + if (mouse_x > 0 and mouse_x < board.X+1 and + mouse_y > 0 and mouse_y < board.Y+1): xpos = mouse_x - 1 ypos = mouse_y - 1 board.toggle(ypos, xpos) @@ -245,6 +247,5 @@ def keyloop(stdscr): def main(stdscr): keyloop(stdscr) # Enter the main loop - if __name__ == '__main__': curses.wrapper(main) |