PYTHON   9

LifeDisplay4.py

Guest on 6th June 2021 05:07:07 PM

  1. # LifeDisplay4.py
  2.  
  3. from graphics import GraphWin, Rectangle, Point, Circle, Line
  4. from LifeWidgets import Button, InfoBox
  5.  
  6. _win = None
  7.  
  8. def SetUpLifeDisplay(width, height, orgSize=8, extra=4):
  9.     global _win
  10.     _win = LifeDisplay(width, height, orgSize, extra)
  11.     return
  12.  
  13. def Stopped():
  14.     if _win is None:
  15.         return True
  16.     m = _win.checkMouse()
  17.     if m is None:
  18.         return False
  19.     else:
  20.         return _win.stopButton().clicked(m)
  21.        
  22.  
  23.  
  24. class Organism:
  25.     #   An organism is something that occupies a square on a
  26.     #   grid of the Game of Life board.
  27.     #
  28.     #   An organism knows its own location on the board in x-
  29.     #   and y-coordinates. This will (eventually) allow it to
  30.     #   draw (and "undraw") itself on a display.
  31.  
  32.     #   By this means, the display can be completely decoupled from the
  33.     #   managing of the game board and from questions about recognizing
  34.     #   self-perpetuating clusters (such as gliders)
  35.  
  36.  
  37.     def __init__(self, row, column):
  38.         self._row = row
  39.         self._col = column
  40.         self._displayed = False  # True only when organism is
  41.                                  # displayed on screen
  42.         self._draw()
  43.         return
  44.  
  45.     def getCoords(self):
  46.         return (self._row, self._col)
  47.  
  48.     def removeFromDisplay(self):
  49.         row = self._row
  50.         column = self._col
  51.         if self._displayed:
  52.             self._rect.undraw()
  53.             self._displayed = False
  54.             #   print("Deleting organism at", self._row, ", ", self._col)
  55.         return
  56.  
  57.     def _draw(self):
  58.         #   Remember: rows are in the y-dimension and columns are in the
  59.         #   x-dimension. Negative row numbers are at the top of the display.
  60.         row = self._row     #for debugging
  61.         col = self._col     #for debugging
  62.         upperxMin  = Point(self._col, self._row)
  63.         lowerxMax = Point(self._col+1, self._row+1)
  64.         rect = Rectangle(upperxMin, lowerxMax)
  65.         rect.setFill("black")
  66.         self._rect = rect
  67.  
  68.         if _win is not None:
  69.         #   draw a square representing this organism
  70.             #   Remember, rows are y-axis and columns are x-axis
  71.             if _win.clip(self._col, self._row):
  72.                 self._rect.draw(_win)
  73.                 self._displayed = True
  74.                 displayed = self._displayed
  75.         return
  76.  
  77. class LifeDisplay(GraphWin):
  78.     #   This is a subclass of the graphics windows from
  79.     #   graphics.py. It provices scaling so that each organism
  80.     #   is on integer boundaries and occupied an integer
  81.     #   number of pixels in each dimension. It also remembers
  82.     #   its clipping boundaries so that the draw method
  83.     #   of Organism does not draw outside the array.
  84.     #
  85.     #   Future expansion includes provision for buttons
  86.     #   at text input in the window without impacting the
  87.     #   game board itself
  88.  
  89.     def __init__(self, width, height, orgSize=8, extra=4):
  90.         #   The parameters width and height are measured in
  91.         #   numbers of organisms. The parameter orgSize is
  92.         #   the number of pixels on each edge of an organism.
  93.         #   The parameter extra is the number of additional
  94.         #   additional organism-sized rows reserved at the
  95.         #   yMin of the display for buttons and other graphic
  96.         #   fields.
  97.  
  98.         #   The actual graphics window size
  99.         winWidth  = abs(width * orgSize)
  100.         winHeight = abs(height * orgSize + extra * orgSize)
  101.         title = "Game of Life"
  102.  
  103.         GraphWin.__init__(self, title, winWidth, winHeight)
  104.        
  105.  
  106.         #   Clipping boundaries for the game board so that
  107.         #   the origin is in the center.
  108.         #
  109.         #   Organisms with x, y positions are shown if and
  110.         #   only if xMin <= x < xMax and yMin <= y < height
  111.         xMin   = -width // 2
  112.         xMax  = xMin + width
  113.         yMin    = -height // 2
  114.         yMax = yMin + height
  115.         self._xMin, self._xMax = xMin, xMax
  116.         self._yMin, self._yMax = yMin, yMax
  117.         self._extra = extra
  118.  
  119.         self.setCoords(xMin, yMax + extra, xMax, yMin)
  120.         self.drawAxes(xMin, xMax, yMin, yMax)
  121.  
  122.         self._stopButton = self.setupStopButton(self)
  123. ##        self._generationInfo = self.setupGenInfo(self)
  124. ##        self._generationsToPlay = \
  125. ##                    self.setupGensToPlay(self, 150)
  126.  
  127.         return
  128.  
  129.     def drawAxes(self, xMin, xMax, yMin, yMax):
  130.        
  131.         verticalAxis = self.line(0, yMin, 0, yMax, 'cyan')
  132.         verticalAxis.draw(self)
  133.  
  134.         horizontalAxis = self.line(xMin, 0, xMax, 0, 'cyan')
  135.         horizontalAxis.draw(self)
  136.  
  137.         yMaxBorder = self.line(xMin, yMax, xMax, yMax, 'black')
  138.         yMaxBorder.draw(self)
  139.  
  140.         return
  141.        
  142.  
  143.     def clip(self, x, y):
  144.         xMin, xMax = self._xMin, self._xMax
  145.         yMin, yMax = self._yMin, self._yMax
  146.         return (self._xMin <= x < self._xMax and \
  147.            self._yMin  <= y < self._yMax)
  148.        
  149.     def line(self, x0, y0, x1, y1, color='black'):
  150.         start = Point(x0, y0)
  151.         end   = Point(x1, y1)
  152.         line  = Line(start, end)
  153.         line.setWidth(1)
  154.         line.setOutline(color)
  155.         return line
  156.  
  157.     def setupStopButton(self, win):
  158.         cpx, cpy = self._xMin + 5, \
  159.                    self._yMax + self._extra/2
  160.         center = Point(cpx, cpy)
  161.         width  = 8
  162.         height = self._extra - 1
  163.         activeText = 'Stop'
  164.         inactiveText = 'Stopped'
  165.        
  166.         return Button(win, center, width, height,
  167.                       activeText, inactiveText)
  168.  
  169.     def stopButton(self):
  170.         return self._stopButton
  171.  
  172. ##    def pause(self, win, mouse):
  173. ##        if self._stopButton.clicked(mouse):
  174. ##            self._stopButton.deactivate()
  175. ##        secondClick = win.getMouse()
  176. ##        while not self._stopButton.clicked(secondClick):
  177. ##            secondClick = win.getMouse()
  178. ##        self._stopButton.activate()
  179. ##        return
  180.            
  181.        
  182.     def setupGenInfo(self, win):
  183.         cpx, cpy = self._xMin + 20, \
  184.                    self._yMax + self._extra/2
  185.         center = Point(cpx, cpy)
  186.         labelWidth, infoWidth = 10, 6
  187.         height = self._extra - 1
  188.         label  = 'Current Generation:-:'
  189.  
  190.         return InfoBox(win, center, labelWidth, infoWidth,
  191.                        height, label)
  192.  
  193.     def updateGeneration(self, generation):
  194.         self._generationInfo.updateInfo(generation)
  195.         return
  196.  
  197.     def setupGensToPlay(self, win, defaultNumber):
  198.         cpx, cpy = self._xMin + 40, \
  199.                    self._yMax + self._extra/2
  200.         center = Point(cpx, cpy)
  201.         labelWidth, infoWidth = 10, 6
  202.         height = self._extra - 1
  203.         label  = 'Generations To Play:'
  204.  
  205.         return InfoBox(win, center, labelWidth, infoWidth,
  206.                        height, label, boxType='input',
  207.                        infoValue = defaultNumber)
  208.  
  209.     def GetNumberOfGenerations(self):
  210.         return eval(self._generationsToPlay.getInput())

Raw Paste


Login or Register to edit or fork this paste. It's free.