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 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 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 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)
class Intersection: """graphical representation of a vertex""" def __init__(self, vertex): self.id = vertex.id self.x = vertex.x self.y = vertex.y self.radius = 3 self.shape = Circle(Point(self.x, self.y), self.radius) def __repr__(self): return "Intersection {0}: ({1}, {2})".format(self.id, self.x, self.y) def draw(self, canvas): self.shape.draw(canvas) def clicked(self, p): xmin = self.x - self.radius xmax = self.x + self.radius ymin = self.y - self.radius ymax = self.y + self.radius return (xmin <= p.getX() <= xmax and ymin <= p.getY() <= ymax) def get_info(self): info = { "type": "Intersection", "id": self.id, "x": "{0:.1f}".format(self.x), "y": "{0:.1f}".format(self.y), } return info
class Wheel(): def __init__(self, center, wheel_radius, tire_radius): self.tire_circle = Circle(center, tire_radius) self.wheel_circle = Circle(center, wheel_radius) def draw(self, win): self.tire_circle.draw(win) self.wheel_circle.draw(win) def move(self, dx, dy): self.tire_circle.move(dx, dy) self.wheel_circle.move(dx, dy) def set_color(self, wheel_color, tire_color): self.tire_circle.setFill(tire_color) self.wheel_circle.setFill(wheel_color) def undraw(self): self.tire_circle .undraw() self.wheel_circle .undraw() def get_size(self): return self.tire_circle.getRadius() def get_center(self): return self.tire_circle.getCenter() def animate(self, win, dx, dy, n): if n > 0: self.move(dx, dy) win.after(100, self.animate, win, dx, dy, n - 1)
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
class ShotTracker: 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 update(self, dt): """Move the shot dt seconds farther along its flight """ # Update the projectile self.proj.update(dt) # Moves the circle to the new projectile location center = self.marker.getCenter() dx = self.proj.getX() - center.getX() dy = self.proj.getY() - center.getY() self.marker.move(dx, dy) def getX(self): """ return the current x coordinate of the shot's center """ return self.proj.getX() def getY(self): """ return the current y coordinate of the shot's center """ return self.proj.getY() def undraw(self): """ undraw the shot """ self.marker.undraw()
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 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 __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 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 drawLegalMoves(board, win, tile1): legalmoves = board.getLegalMoves(tile1) tileWidth = WIDTH/board.boardWidth tileHeight = HEIGHT/board.boardWidth for move in legalmoves: circ = Circle(tileToCoord(board, move), min(tileWidth, tileHeight)/4) circ.setFill('gray') circ.draw(win)
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(win, colour, centre, radius, current_tile, fill=False): """Helper function for drawing circles.""" current_circle = Circle(Point(*centre), radius) if fill: current_circle.setFill(colour) else: current_circle.setOutline(colour) current_circle.setOutline(colour) current_circle.draw(win) current_tile.append(current_circle)
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)
class Asteroid: def __init__(self, pos, dest, win, vel, type): self.pos = pos self.dest = Point(dest.getX() - pos.getX(), dest.getY() - pos.getY()) self.vel = vel self.type = type self.aliveFlag = True self.outOfBounds = False r = 25 * type self.object = Circle(self.pos, r) self.object.setFill(color_rgb(randrange(100, 255), 240, 90)) self.draw(win) def draw(self, win): self.object.draw(win) def undraw(self): self.object.undraw() def update(self, dt, win): #normalize values dest = norm(self.dest.getX(), self.dest.getY()) #calculate x and y movement x = dest.getX() * self.vel * dt y = dest.getY() * self.vel * dt self.object.move(x, y) self.pos = self.object.getCenter() if self.pos.getX() < -100: self.outOfBounds = True def getAliveFlag(self): return self.aliveFlag def setAliveFlag(self, statement=True): self.aliveFlag = statement def getOutOfBounds(self): return self.outOfBounds def getType(self): return self.type def getObject(self): return self.object
class Bullet: def __init__(self, pos, win, r, dest, vel=1.0, c=color_rgb(255, 255, 255)): self.pos = pos #position self.dest = Point(dest.getX() - pos.getX(), dest.getY() - pos.getY()) #destination self.radius = r #radius self.vel = vel #velocity self.object = Circle(self.pos, r) #Circle self.object.setFill(c) #set colour self.object.setOutline('black') self.draw(win) #draw self.aliveFlag = True def getAliveFlag(self): return self.aliveFlag def update(self, dt, win): #normalize values dest = norm(self.dest.getX(), self.dest.getY()) #calculate x and y movement x = dest.getX() * self.vel * dt y = dest.getY() * self.vel * dt #move object self.object.move(x, y) #set position self.pos = self.object.getCenter() #Split into two if statements for readability if self.pos.getX() > win.getWidth() or self.pos.getY() > win.getHeight( ): self.aliveFlag = False elif self.pos.getX() < 0 or self.pos.getY() < 0: self.aliveFlag = False def getObject(self): return self.object def draw(self, win): self.object.draw(win) #remove object from screen def undraw(self): self.object.undraw()
class Particle: def __init__(self, window, p=Point(0, 0)): self.particle = None self.drawn = False self.color = "RED" self.position = p self.x = p.getX() self.y = p.getY() self.size = 3 self.dX = 0 self.dY = 0 self.win = window self.particleTurnCount = 0 def setCoord(self, x, y): self.x = x self.y = y def setColor(self, color): self.color = color if self.particle: self.particle.setFill(color) def setSize(self, size): self.size = size if self.drawn: self.undraw() self.draw() def draw(self): self.particle = Circle(Point(self.x, self.y), self.size) self.particle.setFill(self.color) self.particle.draw(self.win) self.drawn = True def undraw(self): self.particle.undraw() self.drawn = False def setParticleMovement(self, dx, dy): self.dX = dx self.dY = dy def move(self): self.particle.move(self.dX, self.dY) self.position = Point(self.position.getX() + self.dX, self.position.getY() + self.dY) self.particle.undraw() self.particle.draw(self.win)
def five_click_stick_figure(): """ 9. [harder] Write a five_click_stick_figure() function that allows the user to draw a (symmetric) stick figure in a graphics window using five clicks of the mouse to determine the positions of its features. Each feature should be drawn as the user clicks the points. Hint: the radius of the head is the distance between points 1 and 2 — see the previous practical. Note: only the y-coordinate of point (3) should be used — its x coordinate should be copied from point (1). """ win = GraphWin("Five Click Stick Figure", 800, 600) message = Text(Point(400, 15), "Click to create your stick figure") message.draw(win) head_centre = win.getMouse() head_centreX, head_centreY = head_centre.getX(), head_centre.getY() head_perim = win.getMouse() head_perimX, head_perimY = head_perim.getX(), head_perim.getY() head_radius = math.sqrt((head_perimX - head_centreX)**2 + (head_perimY - head_centreY)**2) head = Circle(head_centre, head_radius) head.draw(win) torso = win.getMouse() torso_line = Line(Point(head_centreX, head_centreY + head_radius), Point(head_centreX, torso.getY())) torso_line.draw(win) arm_reach = win.getMouse() arm_length = head_centreX - arm_reach.getX() arms_line = Line(Point(arm_reach.getX(), arm_reach.getY()), Point(head_centreX + arm_length, arm_reach.getY())) arms_line.draw(win) leg_reach = win.getMouse() left_leg_line = Line(Point(head_centreX, torso.getY()), Point(leg_reach.getX(), leg_reach.getY())) left_leg_line.draw(win) leg_distance = head_centreX - leg_reach.getX() right_leg_line = Line(Point(head_centreX, torso.getY()), Point(head_centreX + leg_distance, leg_reach.getY())) right_leg_line.draw(win) await_user_input(win)
def _setup(self): circle = Circle(Point(250, 250), self.r) # set center and radius circle.setWidth(3) circle.draw(self.win) line = Line(Point(250, 225), Point(250, 285)) line.setWidth(3) line.draw(self.win) line2 = Line(Point(280, 250), Point(250, 225)) line2.setWidth(3) line2.draw(self.win) line3 = Line(Point(220, 250), Point(250, 225)) line3.setWidth(3) line3.draw(self.win)
def peas_in_a_pod(): """ 5. Write a function peas_in_a_pod() that asks the user for a number, and then draws that number of 'peas' (green circles of radius 50) in a 'pod' (graphics window of exactly the right size). E.g. if the user enters 5, a graphics window of size 500 × 100 should appear. """ peas = int(input("Enter a number of peas: ")) win = GraphWin("Peas in a pod", peas * 100, 100) for pea in range(peas): circle = Circle(Point(50 + pea * 100, 50), 50) circle.setFill("green") circle.draw(win) win.getMouse() win.close()
def test(points, number_of_clusters, solution=None, max_iterations=100, method=Methods.method_heuristic_initial_clusters_max_dist): win = GraphWin("Results", 800, 600) displacement = 20 time_start = time() for point in points: p = GPoint(point.x * displacement, point.y * displacement) p.draw(win) iteration_result = Methods.clustering(points, number_of_clusters, max_iterations, method) clusters = iteration_result.clusters for c in clusters: c = Circle( GPoint(c.center.x * displacement, c.center.y * displacement), c.radius * displacement) c.draw(win) time_end = time() time_range = time_end - time_start print("-----------------------------") print("Method used: " + get_method_name(method)) print("Total time: " + str(time_range) + " s") print("Results after " + str(max_iterations) + " iterations:") print(str(clusters)) if solution is not None: errors = Methods.compare_results(clusters, solution) print("Max error: " + str(errors[1])) print("Min error: " + str(errors[2])) print("Median error: " + str(errors[0])) try: win.getMouse() win.close() except: return 0
def _visualise_gap_id(self, alpha, id): 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) pos = Point(x + 250, y + 250) gap = Circle(pos, 8) # set center and radius gap.setFill("red") gap.draw(self.win) text = Text(pos, str(id)) text.setTextColor("black") text.setSize(8) text.setStyle('bold') text.draw(self.win) return (id, gap, text)
class Goal: def __init__(self, pos, win): #self.vel_x, self.vel_y = vel[0], vel[1] #self.vel = np.array([self.vel_x, self.vel_y]) self.pos_x = pos[0] self.pos_y = pos[1] self.win = win 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 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 main(): parser = argparse.ArgumentParser() parser.add_argument("--size", type=int, default=3, help="Size of k") args = parser.parse_args() win = GraphWin("My Circle", WIN_SIZE, WIN_SIZE) k = args.size m = k * (k - 1) + 1 r = min(pi * R / m * 0.50, 20) ds = DiffState(k) ds.search() points = [] for i in range(m): ang = 2 * pi * i / m center = (CENTER[0] + R * sin(ang), CENTER[1] - R * cos(ang)) points.append(Point(center[0], center[1])) if m < 20: for i in range(m): for j in range(i, m): if not (i in ds.current and j in ds.current): l = Line(points[i], points[j]) l.draw(win) l.setOutline(all_color) for i in range(m): for j in range(i, m): if i in ds.current and j in ds.current: l = Line(points[i], points[j]) l.setWidth(3) l.draw(win) l.setOutline(set_color) for i in range(m): c = Circle(points[i], r) c.setFill('red') c.draw(win) win.getMouse() win.close()
def main(): parser = argparse.ArgumentParser() parser.add_argument("--size", type=int, default=3, help="Size of k") args = parser.parse_args() win = GraphWin("My Circle", WIN_SIZE, WIN_SIZE) k = args.size m = k * (k - 1) + 1 r = min(pi * R / m * 0.50, 20) ds = DiffState(k) ds.search() points = [] for i in range(m): ang = 2 * pi * i / m center = (CENTER[0] + R * sin(ang), CENTER[1] - R * cos(ang)) points.append(Point(center[0], center[1])) if m < 20: for i in range(m): for j in range(i, m): if not (i in ds.current and j in ds.current): l = Line(points[i], points[j]) l.draw(win) l.setOutline(all_color) for i in range(m): for j in range(i, m): if i in ds.current and j in ds.current: l = Line(points[i], points[j]) l.setWidth(3) l.draw(win) l.setOutline(set_color) for i in range(m): c = Circle(points[i], r) c.setFill("red") c.draw(win) win.getMouse() win.close()
def draw(self, from_update=False): radius = 10 circle1 = Circle(center=self.location, radius=radius) if self.firing: circle1.setFill('red') else: if self.was_fired > 0: circle1.setFill('yellow') else: circle1.setFill(color_rgb(240, 240, 240)) # if self.prev_firing and from_update: # circle1.setFill('yellow') circle1.draw(self.brain.win) message = Text(self.location, self.presentation) message.setTextColor('red') # message.setStyle('italic') message.setSize(10) message.draw(self.brain.win)
def __init__(self,window,P1,level): from graphics import GraphWin,Circle import random from time import time self.p1=P1 self.window=window self.cir=Circle(self.p1,0.4) self.level=level self.cir.setWidth(0) i=random.randrange(6,9+self.level) self.color=i if self.color==6: #颜色对应数字 self.cir.setFill("red") if self.color==7: self.cir.setFill("green") if self.color==8: self.cir.setFill("black") if self.color==9: self.cir.setFill("blue") if self.color==10: self.cir.setFill("orange") for s in range(100): cirs=Circle(self.p1,0.0+s*0.004) if self.color==6: cirs.setFill("red") if self.color==7: cirs.setFill("green") if self.color==8: cirs.setFill("black") if self.color==9: cirs.setFill("blue") if self.color==10: cirs.setFill("orange") cirs.draw(self.window) empty=0 #空循环控制时间(time间隔太大) while empty<=30000: empty=empty+1 cirs.undraw() self.cir.draw(self.window) self.activate=True
def set_graphicals(self): draw_x = scale(self.pos_x) draw_y = scale(self.pos_y) # Draw the new path if self.sling_path_calculated is not None: for action in self.sling_path_calculated: cir = Circle(action[0].get_scaled_point(), self.body_radius) cir.setFill('yellow') cir.draw(self.win) if self.circle is not None: dubinc = Circle(self.circle.c.get_scaled_point(), scale_vectors(self.circle.r)) dubinc.setOutline('Green') dubinc.draw(self.win) if self.body: 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] * 5), scale(self.pos_y + self.current_vel[1] * 5))) 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) '''
class Dado(object): def __init__(self, v, centro, ancho, alto): x, y = centro.getX(), centro.getY() w, h = ancho /2, alto /2 self.ventana = v self.xmax, self.xmin = x+w, x-w self.ymax, self.ymin = y+h, y-h p1 = Point(self.xmin, self.ymin) p2 = Point(self.xmax, self.ymax) self.dado = Rectangle(p1, p2) self.dado.draw(v) self.dado.setFill('#FF0000') '''pintamos los circulos del dado con las posiciones reescalables de los puntos pos1 pos5 pos2 pos4 pos6 pos3 pos7 ''' self.pos1 = Point(self.xmin + w /2, self.ymin + h /2) self.pos2 = Point(self.xmin + w / 2, self.ymin + h) self.pos3 = Point(self.xmin + w / 2, self.ymin + h * 1.5) self.pos4 = Point(self.xmin + w, self.ymin + h) self.pos5 = Point(self.xmin + w * 1.5, self.ymin + h /2) self.pos6 = Point(self.xmin + w * 1.5, self.ymin + h) self.pos7 = Point(self.xmin + w * 1.5, self.ymin + h * 1.5) self.c1 = Circle(self.pos1, 4) self.c1.setOutline('#fff') self.c1.setFill('#fff') self.c1.draw(self.ventana) self.c2 = Circle(self.pos2, 4) self.c2.setOutline('#fff') self.c2.setFill('#fff') self.c2.draw(self.ventana) self.c3 = Circle(self.pos3, 4) self.c3.setOutline('#fff') self.c3.setFill('#fff') self.c3.draw(self.ventana) self.c4 = Circle(self.pos4, 4) self.c4.setOutline('#fff') self.c4.setFill('#fff') self.c4.draw(self.ventana) self.c5 = Circle(self.pos5, 4) self.c5.setOutline('#fff') self.c5.setFill('#fff') self.c5.draw(self.ventana) self.c6 = Circle(self.pos6, 4) self.c6.setOutline('#fff') self.c6.setFill('#fff') self.c6.draw(self.ventana) self.c7 = Circle(self.pos7, 4) self.c7.setOutline('#fff') self.c7.setFill('#fff') self.c7.draw(self.ventana) self.limpiar() def colorDado(self, c):#personalizar el dado self.dado.setFill(c) def colorPunto(self, c):#personalizar los puntos del dado self.c1.setOutline(c) self.c1.setFill(c) self.c2.setOutline(c) self.c2.setFill(c) self.c3.setOutline(c) self.c3.setFill(c) self.c4.setOutline(c) self.c4.setFill(c) self.c5.setOutline(c) self.c5.setFill(c) self.c6.setOutline(c) self.c6.setFill(c) self.c7.setOutline(c) self.c7.setFill(c) def pulsado(self, p):#funcion para saber si se ha pulsado if p.getX() in range(self.xmin, self.xmax) and p.getY() in range(self.ymin, self.ymax): return True else: return False def ponValor(self, valor=1):#funcion para establecer la cara visible del dado self.limpiar() if valor == 1: self.c4.draw(self.ventana) elif valor == 2: self.c1.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 3: self.c1.draw(self.ventana) self.c4.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 4: self.c1.draw(self.ventana) self.c3.draw(self.ventana) self.c5.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 5: self.c1.draw(self.ventana) self.c3.draw(self.ventana) self.c4.draw(self.ventana) self.c5.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 6: self.c1.draw(self.ventana) self.c2.draw(self.ventana) self.c3.draw(self.ventana) self.c5.draw(self.ventana) self.c6.draw(self.ventana) self.c7.draw(self.ventana) def limpiar(self):#limpiar el dado antes de pintar self.c1.undraw() self.c2.undraw() self.c3.undraw() self.c4.undraw() self.c5.undraw() self.c6.undraw() self.c7.undraw() def tirarDado(self):#funcion para visualizar aleatoriamente una cara posibles = [1, 2, 3, 4, 5, 6] num = choice(posibles) self.ponValor(num) return num
def draw_patch(patch, win): if win is not None: c = Circle(Point(patch.x_pos, patch.y_pos), patch.radius) c.setOutline("red") c.draw(win)
class circle: def __init__(self,window,P1,level): from graphics import GraphWin,Circle import random from time import time self.p1=P1 self.window=window self.cir=Circle(self.p1,0.4) self.level=level self.cir.setWidth(0) i=random.randrange(6,9+self.level) self.color=i if self.color==6: #颜色对应数字 self.cir.setFill("red") if self.color==7: self.cir.setFill("green") if self.color==8: self.cir.setFill("black") if self.color==9: self.cir.setFill("blue") if self.color==10: self.cir.setFill("orange") for s in range(100): cirs=Circle(self.p1,0.0+s*0.004) if self.color==6: cirs.setFill("red") if self.color==7: cirs.setFill("green") if self.color==8: cirs.setFill("black") if self.color==9: cirs.setFill("blue") if self.color==10: cirs.setFill("orange") cirs.draw(self.window) empty=0 #空循环控制时间(time间隔太大) while empty<=30000: empty=empty+1 cirs.undraw() self.cir.draw(self.window) self.activate=True def click(self,pr): from graphics import Circle import time pd=False if self.activate==True and (pr.getX()-self.p1.getX())**2+(pr.getY()-self.p1.getY())**2<=0.24: for i in range(3): #点击动画 self.cir.move(0,-0.12/3.0) time.sleep(0.01) for i in range(3): self.cir.move(0,0.12/3.0) time.sleep(0.01) for i in range(3): self.cir.move(0,-0.12/3.0) time.sleep(0.01) for i in range(3): self.cir.move(0,0.12/3.0) time.sleep(0.01) pd=True return pd def undraw(self): self.cir.undraw() def close(self): self.cir.undraw() self.activate=False def move(self,pr): self.cir.move(pr.getX()-self.p1.getX(),pr.getY()-self.p1.getY()) self.p1.move(pr.getX()-self.p1.getX(),pr.getY()-self.p1.getY())