def create_layer(self): # compute tilesize and border coordinates w, h = _scene.getWidth(), _scene.getHeight() tsx = w / (self.num_cols + 2) tsy = h / (self.num_rows + 2) self.ts = min(tsx, tsy) self.left = 2 * self.ts self.right = self.left + 2 * self.ts * self.av self.bottom = h - 2 * self.ts self.top = self.bottom - 2 * self.ts * self.st self.layer = _g.Layer() # Create avenues for av in range(self.av): x = self.left + self.ts * (2 * av + 1) l = _g.Path([_g.Point(x, self.top), _g.Point(x, self.bottom)]) l.setBorderColor("light gray") self.layer.add(l) self.layer.add( _g.Text("%d" % (av + 1), 10, _g.Point(x, self.bottom + self.ts))) # Create streets for st in range(self.st): y = self.bottom - self.ts * (2 * st + 1) l = _g.Path([_g.Point(self.left, y), _g.Point(self.right, y)]) l.setBorderColor("light gray") self.layer.add(l) self.layer.add( _g.Text("%d" % (st + 1), 10, _g.Point(self.left - self.ts, y))) # Create border border = _g.Polygon(_g.Point(self.left, self.bottom), _g.Point(self.right, self.bottom), _g.Point(self.right, self.top), _g.Point(self.left, self.top)) border.setBorderWidth(5) border.setBorderColor("dark red") border.setDepth(10) self.layer.add(border) # Create walls for (col, row) in self.walls: if col % 2 == 0: # vertical wall x1, y1 = self.cr2xy(col, row - 1) x2, y2 = self.cr2xy(col, row + 1) else: # horizontal wall x1, y1 = self.cr2xy(col - 1, row) x2, y2 = self.cr2xy(col + 1, row) w = _g.Path([_g.Point(x1, y1), _g.Point(x2, y2)]) w.setBorderWidth(5) w.setBorderColor("dark red") w.setDepth(10) self.layer.add(w) # Create beepers for (av, st) in self.beepers: self._create_beeper(av, st) # Layer finished _scene.add(self.layer)
def set_trace(self, color=None): """Without color argument, turn off tracing. With color argument, start a new trace in that color.""" if not color: if self._trace: _scene.remove(self._trace) self._trace = None else: x, y = self._trace_pos() self._trace = _g.Path([_g.Point(x, y)]) self._trace.setBorderColor(color) _scene.add(self._trace)
def make_cat_appear(self): cat = cg.Circle(100, cg.Point(self.center_x, self.center_y)) cat.setFillColor(self.color) eye1 = cg.Circle(10, cg.Point(self.center_x - 50, self.center_y - 25)) eye1.setFillColor("black") eye2 = cg.Circle(10, cg.Point(self.center_x + 50, self.center_y - 25)) eye2.setFillColor("black") ear1 = cg.Polygon(cg.Point(self.center_x + 90, self.center_y - 25), cg.Point(self.center_x + 125, self.center_y - 125), cg.Point(self.center_x + 30, self.center_y - 90)) ear2 = cg.Polygon(cg.Point(self.center_x - 90, self.center_y - 25), cg.Point(self.center_x - 125, self.center_y - 125), cg.Point(self.center_x - 30, self.center_y - 90)) nose = cg.Polygon(cg.Point(self.center_x - 15, self.center_y), cg.Point(self.center_x, self.center_y + 15), cg.Point(self.center_x + 15, self.center_y)) nose_line1 = cg.Path(cg.Point(self.center_x + 15, self.center_y + 30), cg.Point(self.center_x, self.center_y + 15)) nose_line2 = cg.Path(cg.Point(self.center_x - 15, self.center_y + 30), cg.Point(self.center_x, self.center_y + 15)) nose.setFillColor("pink") ear1.setFillColor(self.color) ear1.setDepth(70) ear2.setFillColor(self.color) ear2.setDepth(70) nose.setDepth(25) nose_line1.setBorderWidth(2.5) nose_line2.setBorderWidth(2.5) nose_line1.setDepth(25) nose_line2.setDepth(25) paper.add(nose) paper.add(ear1) paper.add(ear2) paper.add(cat) paper.add(eye1) paper.add(eye2) paper.add(nose_line1) paper.add(nose_line2)
sun.setRadius(50) sun.moveTo(700, 100) paper.add(sun) facade = cg.Square(200, cg.Point(400, 350)) facade.setFillColor('white') paper.add(facade) chimney = cg.Rectangle(50, 70, cg.Point(450, 215)) chimney.setFillColor('red') paper.add(chimney) tree = cg.Polygon(cg.Point(150, 220), cg.Point(120, 380), cg.Point(180, 380)) tree.setFillColor('darkGreen') paper.add(tree) sunraySW = cg.Path(cg.Point(660, 140), cg.Point(635, 165)) sunraySW.setBorderColor('yellow') sunraySW.setBorderWidth(6) paper.add(sunraySW) sunraySE = cg.Path(cg.Point(740, 140), cg.Point(765, 165)) sunraySE.setBorderColor('yellow') sunraySE.setBorderWidth(6) paper.add(sunraySE) sunrayNE = cg.Path(cg.Point(740, 60), cg.Point(765, 35)) sunrayNE.setBorderColor('yellow') sunrayNE.setBorderWidth(6) paper.add(sunrayNE) sunrayNW = cg.Path(cg.Point(660, 60), cg.Point(635, 35))
def draw_landscape(): """Creates two suns, sunrays, house, tree, and grass""" paper = cg.Canvas() paper.setBackgroundColor('skyBlue') paper.setWidth(800) paper.setHeight(600) paper.setTitle('Welcome to Tattoine') sun = cg.Circle() paper.add(sun) sun.setFillColor('yellow') sun.setRadius(50) sun.moveTo(700, 100) sunCenter = cg.Point(670, 110) sun2 = cg.Circle(50, sunCenter) sun2.setDepth(60) sun2.setFillColor('orange') paper.add(sun2) facade = cg.Square(200, cg.Point(400, 350)) facade.setFillColor('white') paper.add(facade) chimney = cg.Rectangle(50, 70, cg.Point(450, 215)) chimney.setFillColor('red') paper.add(chimney) tree = cg.Polygon(cg.Point(150, 220), cg.Point(120, 380), cg.Point(180, 380)) tree.setFillColor('darkGreen') paper.add(tree) sunraySW = cg.Path(cg.Point(660, 140), cg.Point(635, 165)) sunraySW.setBorderColor('yellow') sunraySW.setBorderWidth(6) paper.add(sunraySW) sunraySE = cg.Path(cg.Point(740, 140), cg.Point(765, 165)) sunraySE.setBorderColor('yellow') sunraySE.setBorderWidth(6) paper.add(sunraySE) sunrayNE = cg.Path(cg.Point(740, 60), cg.Point(765, 35)) sunrayNE.setBorderColor('yellow') sunrayNE.setBorderWidth(6) paper.add(sunrayNE) sunrayNW = cg.Path(cg.Point(660, 60), cg.Point(635, 35)) sunrayNW.setBorderColor('yellow') sunrayNW.setBorderWidth(6) paper.add(sunrayNW) grass = cg.Rectangle(800, 300, cg.Point(400, 450)) grass.setFillColor('green') grass.setBorderColor('green') grass.setDepth(75) paper.add(grass) landscape_objects = {'paper': paper, 'sun1': sun, 'sun2': sun2, 'facade': facade, 'sunrays': [sunrayNE, sunrayNW, sunraySW, sunraySE], 'chimney': chimney, 'tree': tree, 'grass': grass} return landscape_objects
paper.add('chimney') paper.add(chimney) tree.cg.Polygon(cg.Point(150, 220), (cg.Point(120, 380), (cg.Point(180, 380)) tree.setFillColor('darkGreen') tree = cg.Polygon(cg.Point(150, 220), (cg.Point(120, 380), (cg.Point(180, 380)) tree.setFillColor('darkGreen') tree = cg.Polygon(cg.Point(150, 220), cg.Point(120, 380), cg.Point(180, 380)) tree.setFillColor('darkGreen') paper.add(tree) sunraySW = cg.Path(cg.Point(660, 140), cg.Point(635, 165)) sunray.SW.setBorderColor('yellow') sunraySW.setBorderColor('yellow') sunraySW.setBorderWidth(6) paper.add(sunraySW) sunraySE = cg.Path(cg.Point(740, 140), cg.Poit(764, 165)) sunraySE = cg.Path(cg.Point(740, 140), cg.Point(764, 165)) sunraySE.setBorderColor('yellow') sunraySE.setBorderWidth(6) paper.add(sunraySE) sunrayNE = cg.Path(cg.Point(740, 60), cg.Point(764, 35)) sunrayNE.setBorderColor('yellow') sunrayNE.setBorderWidth(6) paper.add(sunrayNE) sunrayNW = cg.Path(cg.Point(660, 60), cg.Point(635, 35)) sunrayNW.setBorderColor('yellow')
sun.moveTo(700, 100) sunCenter = cg.Point(500, 150) sun2 = cg.Circle(50, sunCenter) paper.add(sun2) sun2.setFillColor('Yellow') facade = cg.Square(200, cg.Point(400, 350)) facade.setFillColor('brown') paper.add(facade) chimney = cg.Rectangle(50, 70, cg.Point(450, 215)) chimney.setFillColor('red') paper.add(chimney) tree = cg.Polygon(cg.Point(150, 220)), tree = cg.Polygon(cg.Point(150, 220), cg.Point(120, 380), cg.Point(180, 380)) tree.setFillColor("limeGreen") paper.add(tree) sunraySW = cg.Path(cg.Point(660, 140), cg.Point(635, 165)) sunraySW.setBorderColor("white") sunraySW.setBorderWidth(6) paper.add(sunraySW) sunraySE = cg.Path(cg.Point(740, 140), cg.Point(765, 165)) sunraySE.setBorderColor('white') sunraySE.setBorderWidth(6) paper.add(sunraySE) sunrayNE = cg.Path(cg.Point(740, 60), cg.Point(765, 35)) sunrayNE.setBorderColor('white') sunrayNE.setBorderWidth(6) paper.add(sunrayNE) sunrayNW = cg.Path(cg.Point(660, 60), cg.Point(635, 35)) sunrayNW.setBorderColor('white') sunrayNW.setBorderWidth(6) paper.add(sunrayNW)