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 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
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
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)
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 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 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_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")
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_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)
def draw_vecs(ctx, vec, color='red'): map_objs, vec_objs, w, h, win = ctx while vec_objs: vec_objs.pop().undraw() for v in vec: c = Circle(Point(v[0] * w, v[1] * h), 2) c.setOutline(color) c.setFill(color) vec_objs.append(c) for o in vec_objs: o.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 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)
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()
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 _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 draw_blue_circle(): """ 5. Write a function, draw_blue_circle(), that allows the user to draw a blue circle of radius 50 by clicking the location of its centre on the window. """ win = GraphWin("Blue Circle") message = Text(Point(100, 100), "Click anywhere to place a blue circle").draw(win) message.setSize(8) centre = win.getMouse() circle = Circle(centre, 50).draw(win) circle.setFill("blue") message.setText("") await_user_input(win)
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 __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) '''
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 draw_2d_map(ctx, m): map_objs, vec_objs, w, h, win = ctx while map_objs: map_objs.pop().undraw() for i in range(m.shape[0]): for j in range(m.shape[1]): c = Circle(Point(m[i, j, 0] * w, m[i, j, 1] * h), 5) c.setFill('black') map_objs.append(c) if i + 1 < m.shape[0]: map_objs.append( Line(Point(m[i, j, 0] * w, m[i, j, 1] * h), Point(m[i + 1, j, 0] * w, m[i + 1, j, 1] * h))) if j + 1 < m.shape[1]: map_objs.append( Line(Point(m[i, j, 0] * w, m[i, j, 1] * h), Point(m[i, j + 1, 0] * w, m[i, j + 1, 1] * h))) for o in map_objs: o.draw(win)
def draw_patch_one(win, x, y, colour): patch_shapes = [] even_positions, odd_positions = [20, 60], [0, 40, 80] # Loop through the rows and columns of the patch # to define the square, tilted square, and inner circle for row in range(0, 100, 20): for column in range(0, 100, 20): square = Rectangle(Point(x + column, y + row), Point(x + column + 20, y + row + 20)) # Set the square fill colour if # it is in rows 2 or 4 or columns 1, 3, or 5 if row in even_positions or column in odd_positions: square.setFill(colour) # Set the tilted square if # it is in rows 1, 3, or 5 and columns 2 or 4 if row in odd_positions and column in even_positions: tilted_square = Polygon(Point(x + column, y + row + 10), Point(x + column + 10, y + row), Point(x + column + 20, y + row + 10), Point(x + column + 10, y + row + 20)) tilted_square.setFill(colour) patch_shapes.append(tilted_square) circle = Circle(Point(x + column + 10, y + row + 10), 5) circle.setFill("white") for shape in [square, circle]: patch_shapes.append(shape) patch_box = draw_patch_box(x, y) for shape in patch_shapes: shape.draw(win).setOutline("") drawn, design, elements = True, "1", [patch_box, patch_shapes] return [colour, drawn, design, x, y, elements]
def drawPiece(pieceCode, x, y): if pieceCode == 1: color, mirror, shape, xEye = pieceColors[0], -1, horseShape, 60 elif pieceCode == 2: color, mirror, shape, xEye = pieceColors[0], 1, appleShape, 0 elif pieceCode == -1: color, mirror, shape, xEye = pieceColors[1], 1, horseShape, 40 else: color, mirror, shape, xEye = pieceColors[1], 1, appleShape, 0 poly = Polygon([Point(x + 50 + mirror * (xPoint - 50) * squareSize / 100, y + yPoint * squareSize / 100) for (xPoint, yPoint) in shape]) poly.setFill(color) poly.setOutline("black") poly.setWidth(2) poly.draw(win) if xEye > 0: eye = Circle(Point(x + xEye * squareSize / 100, y + 30), 3) eye.setFill("black") eye.setOutline("black") eye.setWidth(1) eye.draw(win)
class ShotTracker: def __init__(self, win, angle, velocity, height): 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): self.proj.update(dt) 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 self.proj.getX() def getY(self): return self.proj.getY() def undraw(self): self.marker.undraw()
def create_window(self, capacity): """ Create the graphics window. :param capacity: the capacity of the skewer (for the window size) :return: None """ self.win = GraphWin("Shish Kebab", 800, 200) self.win.setCoords(WIN_LOW_LEFT_X, WIN_LOW_LEFT_Y - 0.1, WIN_LOW_LEFT_X + (capacity + 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(capacity, WIN_LOW_LEFT_Y + WIN_HEIGHT / 2.0)) line.setWidth(LINE_THICKNESS) line.draw(self.win) handle = Circle( Point(capacity - .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 = []
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
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())