def draw_archery_target(): """ 3. Write a function, draw_archery_target(), that draws a coloured target consisting of concentric circles of yellow (innermost), red and blue. The sizes of the circles should be in correct proportion i.e. the red circle should have a radius twice that of the yellow circle, and the blue circle should have a radius three times that of the yellow circle. Hint: objects drawn later will appear on top of objects drawn earlier. """ win = GraphWin("Target") win.setCoords(0, 0, 1, 1) centre = Point(0.5, 0.5) yellow_circle = Circle(centre, 0.1) yellow_circle.setFill("yellow") red_circle = Circle(centre, yellow_circle.getRadius() * 2) red_circle.setFill("red") blue_circle = Circle(centre, yellow_circle.getRadius() * 3) blue_circle.setFill("blue") blue_circle.draw(win) red_circle.draw(win) yellow_circle.draw(win) await_user_input(win)
def blackHoles(field): # Create a list of numbers that could be X and Y coordinates numbers = [] for i in range(60, 360, 40): numbers.append(i) # From that list, ceate first blackhole coordinates blackX1 = int(choice(numbers)) blackY1 = int(choice(numbers)) # Create second blackhole coordinates blackX2 = int(choice(numbers)) blackY2 = int(choice(numbers)) # Draw the black holes black1 = Circle(Point(blackX1, blackY1), 5) black1.setFill('black') black1.draw(field) black2 = Circle(Point(blackX2, blackY2), 5) black2.setFill('black') black2.draw(field) # Get the centers of the circles blackcenter1 = black1.getCenter() blackcenter2 = black2.getCenter() # Return the center locations for the black holes return blackcenter1, blackcenter2
def set_graphicals(self): draw_x = scale(self.pos_x) draw_y = scale(self.pos_y) if self.circle is not None and self.get_trace is False: dubinc = Circle(self.circle.c.get_scaled_point(), scale_vectors(self.circle.r)) dubinc.setOutline('Green') dubinc.draw(self.win) if self.body is not None and self.get_trace is False: self.body.undraw() self.body = Circle(Point(draw_x, draw_y), self.body_radius) self.body.setFill('yellow') self.body.draw(self.win) if self.vel_arrow: self.vel_arrow.undraw() self.vel_arrow = Line( Point(draw_x, draw_y), Point(scale(self.pos_x + self.current_vel[0]), scale(self.pos_y + self.current_vel[1]))) self.vel_arrow.setFill('black') self.vel_arrow.setArrow("last") self.vel_arrow.draw(self.win) if self.acc_arrow: self.acc_arrow.undraw() self.acc_arrow = Line( Point(draw_x, draw_y), Point(scale(self.pos_x + self.current_acc[0] * 5), scale(self.pos_y + self.current_acc[1] * 5))) self.acc_arrow.setFill('blue') self.acc_arrow.setArrow('last') self.acc_arrow.draw(self.win)
def update_territories(self): """Update the black and white territories on the board. Attributes updated by this function: self.batch_territory """ # Display the territory an the regular batch # Display the stones on the regular batch self.batch_territory = self.batch rad = 5 # Iterate trough all territory indicators and place the corresponding # black or white circle on the grid or above stones for i in range(0, self.data['size']): for j in range(0, self.data['size']): if self.data['territory'][j][i] != None: x_coord, y_coord = self.grid.get_coords(i, j) if self.data['territory'][j][i] == BLACK: Circle(x_coord, y_coord, color=BLACK_TERRITORY, r=rad, batch=self.batch_territory, group=self.grp_territory) elif self.data['territory'][j][i] == WHITE: Circle(x_coord, y_coord, color=WHITE_TERRITORY, r=rad, batch=self.batch_territory, group=self.grp_territory)
def draw_coin(window, x, y, score): coinCoord = Point(OFFSET + y * SCALE, OFFSET + x * VSCALE) if score == 2: coin = Circle(coinCoord, GOLD_SIZE) coin.setFill(GOLD_COIN) else: coin = Circle(coinCoord, SILVER_SIZE) coin.setFill(SILVER_COIN) coin.draw(window) return coin
def draw_circle(board_column, board_row, color, dot_size): if color != "white": head = Circle( Point((board_column * (size * 2) + (offsetC + dot_size)), (board_row * (size * 2) + (offsetR + dot_size))), dot_size) head.setFill(color) else: head = Circle( Point((board_column * (size * 2) + (offsetC + dot_size)), (board_row * (size * 2) + (offsetR + dot_size))), dot_size / 3) head.setFill(color) head.draw(win)
def O_make(p1): # Left column bottom Box if (p1.getX() < 2 and p1.getY() < 3): Circ = Circle(Point(.5, 1.5), 1) board[2][0] = 'O' Circ.setFill('red') Circ.draw(win) # left column second Box elif (p1.getX() < 2 and p1.getY() < 6): Circ = Circle(Point(.5, 4.5), 1) board[1][0] = 'O' Circ.setFill('red') Circ.draw(win) # Left column top box elif (p1.getX() < 2 and p1.getY() < 9): Circ = Circle(Point(.5, 7.5), 1) board[0][0] = 'O' Circ.setFill('red') Circ.draw(win) # Middle column bottom box elif (p1.getX() < 5 and p1.getY() < 3): Circ = Circle(Point(3.5, 1.5), 1) Circ.setFill('red') Circ.draw(win) board[2][1] = 'O' # middle column middle box elif (p1.getX() < 5 and p1.getY() < 6): Circ = Circle(Point(3.5, 4.5), 1) Circ.setFill('red') Circ.draw(win) board[1][1] = 'O' # middle column top box elif (p1.getX() < 5 and p1.getY() < 9): Circ = Circle(Point(3.5, 7.5), 1) Circ.setFill('red') Circ.draw(win) board[0][1] = 'O' # right column bottom box elif (p1.getX() < 8 and p1.getY() < 3): Circ = Circle(Point(6.5, 1.5), 1) Circ.setFill('red') Circ.draw(win) board[2][2] = 'O' # right column middle box elif (p1.getX() < 8 and p1.getY() < 6): Circ = Circle(Point(6.5, 4.5), 1) Circ.setFill('red') Circ.draw(win) board[1][2] = 'O' # right column top box elif (p1.getX() < 8 and p1.getY() < 9): Circ = Circle(Point(6.5, 7.5), 1) Circ.setFill('red') Circ.draw(win) board[0][2] = 'O'
def make_button(msg, color): poster = Poster() circle = Circle(100, color, filled=True) poster.pin(circle, 0, 0) textbox = TextBox(msg, "Helvetica", "40") poster.pin(textbox, 0, 0) return poster
def draw_classroom(): win = GraphWin("Stick figure", 300, 200) head = Circle(Point(100, 60), 20) body = Line(Point(100, 80), Point(100, 120)) arms = Line(Point(70, 90), Point(130, 90)) leg_l = Line(Point(100, 120), Point(80, 170)) leg_r = Line(Point(100, 120), Point(120, 170)) for body_part in [head, body, arms, leg_l, leg_r]: body_part.draw(win) # Draw the Whiteboard whiteboard = Rectangle(Point(140, 50), Point(290, 150)) whiteboard.setFill('white') whiteboard.draw(win) # Draw the blue marker pen base marker_pen = Rectangle(Point(120, 80), Point(128, 100)) marker_pen.setFill('blue') marker_pen.draw(win) # Draw the blue marker pen tip marker_pen_tip = Rectangle(Point(122, 72), Point(126, 80)) marker_pen_tip.setFill('blue') marker_pen_tip.draw(win) write_letters(win) marker_pen_tip.setFill('white')
def draw_tour(path_d, all_nodes): from graphics import Point, Circle, Line, GraphWin maxX = max([x for x, y in all_nodes]) minX = min([x for x, y in all_nodes]) maxY = max([y for x, y in all_nodes]) minY = min([y for x, y in all_nodes]) N = 750 norm_x = N/(maxX - minX) norm_y = N/(maxY - minY) win = GraphWin("TSP", N, N) for n in all_nodes: c = Point(*norm(n, norm_x, norm_y, minX, minY)) c = Circle(c, 3) c.draw(win) for (k, subdict) in path_d.iteritems(): #t = Text(Point(*subdict['center']*N), k) #t.draw(win) if not subdict['hasBeenSplit']: p = Point(*norm(subdict['center'], norm_x, norm_y, minX, minY)) c1i, c2i = subdict['connections'] c1 = Point(*norm(path_d[str(c1i)]['center'], norm_x, norm_y, minX, minY)) c2 = Point(*norm(path_d[str(c2i)]['center'], norm_x, norm_y, minX, minY)) l1 = Line(p, c1) l2 = Line(p, c2) l1.draw(win) l2.draw(win) win.getMouse() win.close()
def __makePip(self, x, y): "Internal helper method to draw a pip at (x,y)" pip = Circle(Point(x, y), self.psize) pip.setFill(self.background) pip.setOutline(self.background) pip.draw(self.win) return pip
def createWindow(self, N): """ Create the graphics window. Arguments: self - the SkewerUI instance N - the capacity of the skewer """ self.win = GraphWin("Shish Kebab", 800, 200) self.win.setCoords( \ WIN_LOW_LEFT_X, \ WIN_LOW_LEFT_Y - 0.1, \ WIN_LOW_LEFT_X+(N+1)*FOOD_WIDTH, \ WIN_UP_RIGHT_Y + 0.1 \ ) # draw skewer line = Line( \ Point(WIN_LOW_LEFT_X, WIN_LOW_LEFT_Y+WIN_HEIGHT/2.0), \ Point(N, WIN_LOW_LEFT_Y+WIN_HEIGHT/2.0) \ ) line.setWidth(LINE_THICKNESS) line.draw(self.win) handle = Circle( \ Point(N-.1, WIN_LOW_LEFT_Y+WIN_HEIGHT/2.0), \ SKEWER_HANDLE_RADIUS \ ) handle.setFill(BKGD_COLOR) handle.setWidth(LINE_THICKNESS) handle.draw(self.win) self.items = []
def draw_grid(size): squares = size - 1 win = GraphWin("Graphical traced walk", 50 * size, 50 * size) win.setCoords(0, size, size, 0) border_rectangle = Rectangle(Point(0.5, 0.5), Point(size - 0.5, size - 0.5)).draw(win) border_rectangle.setFill("gray") border_rectangle.setWidth(2) centre_square = Rectangle( Point(size / 2 - 0.5, size / 2 - 0.5), Point(size / 2 + 0.5, size / 2 + 0.5), ).draw(win) centre_square.setFill("cyan") centre_square.setOutline("") person = Circle(Point(size / 2, size / 2), 0.25).draw(win) person.setFill("red") square_texts = [[""] * squares for _ in range(squares)] for i in range(squares): for j in range(squares): # grid lines Line(Point(1.5 + j, 0.5), Point(1.5 + j, size - 0.5)).draw(win) Line(Point(0.5, 1.5 + j), Point(size - 0.5, 1.5 + j)).draw(win) # text within each square square_text = Text(Point(1 + j, 1 + i), "").draw(win) square_texts[i][j] = square_text return win, person, square_texts
def draw_path(final_path): for i in final_path: dot = Circle( Point((i.j * square_size) + square_size / 2, (i.i * square_size) + square_size / 2), 10) dot.setFill(color_rgb(66, 134, 244)) dot.draw(win)
def update_board(second_per_update): # A* current = min(openSet, key=lambda o: o.f) # BFS # current = openSet[0] # DIJKSTRA # current = min(openSet, key=lambda o: o.g) for i in openSet: if i not in open_update: rect = i.rectangle(i.i, i.j) rect.setFill(color_rgb(167, 239, 180)) rect.draw(win) open_update.append(i) text = i.hScoreText(i.i, i.j) text.setOutline(color_rgb(19, 10, 200)) text.setStyle("bold") text.draw(win) dot = Circle( Point(current.j * square_size + square_size / 2, current.i * square_size + square_size / 2), 10) dot.setOutline(color_rgb(19, 10, 200)) dot.draw(win) for i in closeSet: if i not in closed_update: rect = i.rectangle(i.i, i.j) rect.setFill(color_rgb(239, 167, 167)) rect.draw(win) closed_update.append(i) text = i.hScoreText(i.i, i.j) text.setOutline(color_rgb(19, 10, 200)) text.setStyle("bold") text.draw(win) openSet_score.setText(openSet.__len__()) closedSet_score.setText(closeSet.__len__()) time.sleep(second_per_update)
def spawn_disk() -> Circle: x = random.randint( int(-disk_size / 2), # nosec int(win_size + disk_size / 2)) y = random.randint(-win_size, -disk_size) # nosec c = Circle(Point(x, y), disk_size) return c
def draw_mark(self, move: tuple) -> None: """ Draw a mark as specified by a move :param move: a legal move: (selected_tile.x_pos, selected_tile.y_pos, player.mark) :return: none """ if self.window is None: raise ValueError('Board has no open window!') tile_x, tile_y, mark = move grid_x, grid_y = self.coord_tile_to_grid(tile_x, tile_y) rad = self.tile_size * 0.3 if mark == 'O': cir = Circle(Point(grid_x, grid_y), rad) cir.setOutline('blue') cir.setWidth(3) cir.draw(self.window) else: downstroke = Line(Point(grid_x - rad, grid_y - rad), Point(grid_x + rad, grid_y + rad)) upstroke = Line(Point(grid_x - rad, grid_y + rad), Point(grid_x + rad, grid_y - rad)) downstroke.setOutline('red') downstroke.setWidth(3) upstroke.setOutline('red') upstroke.setWidth(3) upstroke.draw(self.window) downstroke.draw(self.window)
def move(self, x, y): self.col = x self.row = y self.circle.undraw() self.face.undraw() self.circle = Circle(Point(x * 50 + 25, y * 50 + 50 + 25), 20) self.face = self._getFace()
def draw_tour(tour): from graphics import Point, Circle, Line, GraphWin maxX = max([x for x, y in tour]) minX = min([x for x, y in tour]) maxY = max([y for x, y in tour]) minY = min([y for x, y in tour]) N = 750 norm_x = N / (maxX - minX) norm_y = N / (maxY - minY) win = GraphWin("TSP", N, N) for n in tour: c = Point(*norm(n, norm_x, norm_y, minX, minY)) c = Circle(c, 2) c.draw(win) for i, _ in enumerate(tour[:-1]): p1 = norm(tour[i], norm_x, norm_y, minX, minY) p2 = norm(tour[i + 1], norm_x, norm_y, minX, minY) l = Line(Point(*p1), Point(*p2)) l.draw(win) win.getMouse() win.close()
def __moveBall(self, pos): """Move a graphic to a particular position on the window, rather than move by an amount""" old = self.graphic graphic = Circle(pos, self.rad) graphic.draw(self.window) self.graphic = graphic old.undraw()
def __init__(self, x, y): self.col = x self.row = y self.circle = Circle(Point(x * 50 + 25, y * 50 + 50 + 25), 20) self.facing = self.NORTE self.face = self._getFace() self.attached = False self.s = [False, False, False, False, False, False, False, False] self.x = [False, False, False, False]
def draw_path(final_path): for i in final_path: dot = Circle( Point((i.j * square_size) + square_size / 2, (i.i * square_size) + square_size / 2), 10) dot.setFill(color_rgb(206, 141, 14)) dot.setOutline(color="white") dot.setWidth(3) dot.draw(win)
def _visualise_gap(self, alpha): alpha_rad = (alpha * 2 * math.pi / 360) - math.pi / 2 y = self.r * math.sin(alpha_rad) x = -1 * self.r * math.cos(alpha_rad) gap = Circle(Point(x + 250, y + 250), 5) # set center and radius gap.setFill("red") gap.draw(self.win) return gap
def set_graphicals(self, quick_draw): """Draws the graph""" self.drawables = [] self.drawable_path = [] t = time.time() for node in self.graph: curr_loc = node.get_scaled_point() draw_node = Circle(curr_loc, 1) draw_node.setFill('red') draw_node.draw(self.win) self.drawables.append(draw_node) if not quick_draw: for neighbor in self.graph[node]: if neighbor: line = Line(curr_loc, neighbor.get_scaled_point()) line.draw(self.win) self.drawables.append(line) if self.path is not None: for i in range(0, len(self.path) - 1): node_1 = self.path[i] node_2 = self.path[i + 1] cir = Circle(node_1.get_scaled_point(), 2) cir.setFill('Red') cir.setOutline('Red') self.drawable_path.append(cir) lin = Line(node_1.get_scaled_point(), node_2.get_scaled_point()) lin.setOutline('Red') lin.draw(self.win) self.drawable_path.append(lin) for i in range(0, len(self.optimal_path) - 1): node_1 = self.optimal_path[i] node_2 = self.optimal_path[i + 1] cir = Circle(node_1.get_scaled_point(), 5) cir.setFill('Blue') cir.setOutline('Blue') cir.draw(self.win) self.drawable_path.append(cir) lin = Line(node_1.get_scaled_point(), node_2.get_scaled_point()) lin.setOutline('Blue') lin.draw(self.win) self.drawable_path.append(lin) emit_verbose("Drawing RRT took", self.verbose, var=time.time() - t)
def set_graphicals(self): # draw player self.body = Circle(Point(scale(self.pos_x), scale(self.pos_y)), 7) self.body.setFill('red') # Note: downwards in Y is the positive direction for this graphics lib #self.arrow = Line(Point(scale(self.pos_x), scale(self.pos_y)), # Point(scale(self.pos_x + self.vel_x), scale(self.pos_y + self.vel_y))) #self.arrow.setFill('black') #self.arrow.setArrow('last') self.body.draw(self.win)
def __init__(self, win, angle, velocity, height): """win is the GraphWin to display the shot. angle, velocity, and height are initial projectile parameters. """ self.proj = Projectile(angle, velocity, height) self.marker = Circle(Point(0, height), 3) self.marker.setFill("red") self.marker.setOutline("red") self.marker.draw(win)
def __init__(self, window, center, size): eyeSize = 0.15 * size eyeOff = size / 3.0 mouthSize = 0.8 * size mouthOff = size / 2.0 self.head = Circle(center, size) self.head.draw(window) self.leftEye = Circle(center, eyeSize) self.leftEye.move(-eyeOff, -eyeOff) self.rightEye = Circle(center, eyeSize) self.rightEye.move(eyeOff, -eyeOff) self.leftEye.draw(window) self.rightEye.draw(window) p1 = center.clone() p1.move(-mouthSize / 2, mouthOff) p2 = center.clone() p2.move(mouthSize / 2, mouthOff) self.mouth = Line(p1, p2) self.mouth.draw(window)
def draw_positioned_circles(): win = GraphWin("Circles", 800, 200) radius = 20 for _ in range(15): click = win.getMouse() click_x = click.getX() circle = Circle(click, radius) colour_circles(click_x, circle, win)
def Flinch(self, center, size, window): eyesize = 0.15 * size eyeOff = size / 3.0 self.leftEye = Line(Point(center.x + eyesize / 2, center.y), Point(center.x - eyesize / 2, center.y)) self.leftEye.move(-eyeOff, -eyeOff) self.rightEye = Circle(center, eyesize) self.rightEye.move(eyeOff, -eyeOff) self.leftEye.draw(window) self.rightEye.draw(window)
def draw_arrow(win, arrow_x, arrow_y): """Draw an arrow onto the given graphics window.""" arrow_shaft = Circle(Point(arrow_x, arrow_y), 0.008).draw(win) arrow_shaft.setFill("brown") for x in (1, -1): fletching = Line(Point(arrow_x + 0.02, arrow_y + 0.02 * x), Point(arrow_x - 0.02, arrow_y - 0.02 * x)).draw(win) fletching.setWidth(2) fletching.setFill("gray")