PYTHON   13

Organism.py

Guest on 6th June 2021 05:08:49 PM

  1. # Organism.py
  2.  
  3. from graphics import Rectangle, Point, Circle, Line
  4. from LifeDisplay4 import _win
  5.  
  6.  
  7.  
  8. class Organism:
  9.     #   An organism is something that occupies a square on a
  10.     #   grid of the Game of Life board.
  11.     #
  12.     #   An organism knows its own location on the board in x-
  13.     #   and y-coordinates. This will (eventually) allow it to
  14.     #   draw (and "undraw") itself on a display.
  15.  
  16.     #   By this means, the display can be completely decoupled from the
  17.     #   managing of the game board and from questions about recognizing
  18.     #   self-perpetuating clusters (such as gliders)
  19.  
  20.  
  21.     def __init__(self, row, column):
  22.         self._row = row
  23.         self._col = column
  24.         self._draw()
  25.         return
  26.  
  27.     def getCoords(self):
  28.         return (self._row, self._col)
  29.  
  30.     def __del__(self):
  31.         self._undraw()
  32.         #   print("Deleting organism at", self._row, ", ", self._col)
  33.         return
  34.  
  35.     def _draw(self):
  36.         upperxMin  = Point(self._row, self._col)
  37.         lowerxMax = Point(self._row+1, self._col+1)
  38.         rect = Rectangle(upperxMin, lowerxMax)
  39.         rect.setFill("black")
  40.         self._rect = rect
  41.  
  42.         if _win is not None:
  43.         #   draw a square representing this organism
  44.             if _win.clip(self.__x, self.__y):
  45.                 self.__rect.draw(_win)
  46.         return
  47.  
  48.     def _undraw(self):
  49.         if _win is not None:
  50.             self._rect.undraw()
  51.         return

Raw Paste


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