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 __init__(self, radius, av, st, num=1): self.av = av self.st = st self.num = num self.layer = _g.Layer() self.circle = _g.Circle(radius=radius) self.text = _g.Text("%d" % num, 11, _g.Point(0, 0)) self.layer.add(self.circle) self.layer.add(self.text) self.circle.setFillColor("yellow") self.circle.setBorderColor("orange") self.circle.setDepth(10) self.text.setDepth(5) self.layer.setDepth(5)
def init_hardware(): global _canvas, _cabin, _ldoor, _rdoor _canvas = _g.Canvas(200, 600) _canvas.setBackgroundColor("lightblue") _canvas.setTitle("Elevator") r = _g.Rectangle(90, 160) r.setFillColor("yellow") r.setBorderColor("red") _ldoor = _g.Rectangle(40, 150) _rdoor = _g.Rectangle(40, 150) _ldoor.move(-20, 0) _rdoor.move(20, 0) _ldoor.adjustReference(-20, 0) _rdoor.adjustReference(20, 0) _ldoor.setFillColor("blue") _rdoor.setFillColor("blue") r.setDepth(60) _cabin = _g.Layer() _cabin.add(r) _cabin.add(_ldoor) _cabin.add(_rdoor) _cabin.moveTo(70, 600 - 90) _canvas.add(_cabin) for i in range(3): floor = _g.Rectangle(70, 6) floor.setFillColor("blue") floor.moveTo(150, 600 - i * 200 - 13) _canvas.add(floor) for i in range(4): floor = (i + 1) // 2 button = Button(i, ((i % 2) == 0)) button.moveTo(150, 600 - floor * 200 - 50) light = _g.Circle(10) light.moveTo(150, 600 - floor * 200 - 90) light.setFillColor("darkblue") if i == 1: button.move(15, 0) light.move(15, 0) elif i == 2: button.move(-15, 0) light.move(-15, 0) _canvas.add(button) _canvas.add(light) _buttons[i] = button _lights[i] = light