Example #1
0
class Country:
    def __init__(self, states, width=950):
        ratio = 0.5263
        self._canvas = Canvas(width, ratio * width)
        self._canvas.setTitle('United States')
        self._states = {}  # map from abbrev to RenderedState
        bounds = None
        self._canvas.setAutoRefresh(False)
        for s in states:
            rendered = _RenderedState(s)
            self._canvas.add(rendered)
            self._states[s.abbrev()] = rendered
            bounds = _mergeBounds(rendered.getBounds(), bounds)
        self._canvas.zoomView(width / 950.0, Point(0, 0))
        self._canvas.setAutoRefresh(True)


#    self._canvas.setView(Point(bounds[0],bounds[3]), Point(bounds[1],bounds[2]))

    def setTitle(self, title):
        """Set the Canvas title to the given string."""
        self._canvas.setTitle(title)

    def setFillColor(self, stateCode, color):
        """Set the fill color of the state with given abbreviation to the indicated color."""
        if not isinstance(stateCode, str):
            raise TypeError('state code must be a string')
        if stateCode not in self._states:
            raise ValueError('unknown state code: ' + stateCode)
        self._states[stateCode].setFillColor(color)
Example #2
0
class Frame:
    def __init__(self, width, height, bgColor, title):
        self.frame = Canvas(width, height, bgColor, title)
        # constructs a new Frame object

    def getWidth(self):
        return self.frame.getWidth()
        # returns the width of the Frame

    def getHeight(self):
        return self.frame.getHeight()
        # returns the height of the Frame

    def setWidth(self, width):
        self.frame.setWidth(width)
        # sets a new width for the Frame

    def setHeight(self, height):
        self.frame.setHeight(height)
        # sets a new height for the Frame

    def add(self, obj):
        self.frame.add(obj)
        # adds a new object to the Frame

    def getContents(self):
        return self.frame.getContents()

    def close(self):
        self.frame.close()

    def wait(self):
        self.frame.wait()

    def remove(self, obj):
        self.frame.remove(obj)

    def setTitle(self, title):
        self.frame.setTitle(title)
class World:

    def __init__(self,width=500,height=400,color="white",title="Simulated World"):
        self._canvas = Canvas(width,height,color,title)
        self._canvas.setAutoRefresh(False)
        self._star = None
        self._controller = None
        self._timestamp = 0

    def getWidth(self):
        return self._canvas.getWidth()

    def getHeight(self):
        return self._canvas.getHeight()

    def setController(self,controller):
        self._controller = controller
        
    def getGravity(self):
        if self._controller:
            return self._controller.getGravity()
        else:
            return 0.0

    def getStar(self):
        return self._star

    def _setStar(self,star):
        if (self._star):
            self._canvas.remove(star)
        self._star = star
        star.setDepth(0)
        self._canvas.add(star)
        self._canvas.refresh()

    def add(self,updatable):
        try:
            updatable.advance        # note this is not a call, just a check to see if advance exists
        except:
            raise StandardError, "You may only add objects which support an appropriate 'advance' method"
        self._timestamp += 1
        updatable.setDepth(self._timestamp)
        self._canvas.add(updatable)
        self._canvas.refresh()

    def remove(self, obj):
        self._canvas.remove(obj)
               
    def numObj(self):
        return len(self._canvas.getContents())
    
    def _mainloop(self):

        more = True
        while more:
#            global debug
#            if debug:
#                print "within world's mainloop"
            objects = self._canvas.getContents()   # latest list of objects
            for obj in objects:
                obj.advance(self)
            self._canvas.refresh()
            if self._controller:
                more = self._controller.processDelay(self)