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 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_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 __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 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 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_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 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 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_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 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()
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)
#This program illustrates the correct way of drawing two eyes using the clone method from graphics import Circle, Point, GraphWin leftEye = Circle (Point (80 , 50) , 5) leftEye.setFill ('yellow') leftEye.setOutline ('red') rightEye = leftEye.clone ( ) # rightEye is an exact copy of the left rightEye.move ( 20 , 0 ) win = GraphWin('Eyes') leftEye.draw(win) rightEye.draw(win)
def __makePip(self, x, y): pip = Circle(Point(x, y), self.pip_size) pip.setFill(self.background) pip.setOutline(self.background) pip.draw(self.win) return pip
from graphics import GraphWin, Point, Circle, Polygon, Text win = GraphWin("Valentine", 600, 600) left_circle = Circle(Point(200, 200), 100) left_circle.setFill("red") left_circle.setOutline("red") left_circle.draw(win) right_circle = left_circle.clone() right_circle.move(200, 0) right_circle.draw(win) p1 = Point(110, 245) p2 = Point(300, 500) p3 = Point(490, 245) p4 = Point(300, 200) bottom = Polygon(p1, p2, p3, p4) bottom.setFill("red") bottom.setOutline("red") bottom.draw(win) happy = Text(Point(300, 240), "Happy") happy.setFill("white") happy.setSize(32) happy.draw(win) valentines = Text(Point(300, 280), "Valentine's") valentines.setFill("white") valentines.setSize(32)
def endGame(field, game_panel, player_name, score): # Let the user know the game is finished end_game_text = Text(Point(200, 200), "Finished! Click to close") end_game_text.draw(field) # Draw graphic objects at different places that represent balloons with a # string connected to it. balloon_1 = Circle(Point(145, 110), 18) balloon_1.setFill("red") balloon_1.setOutline("red") balloon_1.draw(field) triangle_1 = Polygon(Point(137, 135), Point(145, 128), Point(153, 135)) triangle_1.setFill("red") triangle_1.setOutline('red') triangle_1.draw(field) string_1 = Line(Point(145, 135), Point(145, 180)) string_1.draw(field) balloon_2 = Circle(Point(340, 300), 18) balloon_2.setFill("red") balloon_2.setOutline("red") balloon_2.draw(field) triangle_2 = Polygon(Point(332, 325), Point(340, 318), Point(348, 325)) triangle_2.setFill("red") triangle_2.setOutline('red') triangle_2.draw(field) string_2 = Line(Point(340, 325), Point(340, 370)) string_2.draw(field) balloon_3 = Circle(Point(75, 275), 18) balloon_3.setFill("red") balloon_3.setOutline("red") balloon_3.draw(field) triangle_3 = Polygon(Point(67, 300), Point(75, 293), Point(83, 300)) triangle_3.setFill("red") triangle_3.setOutline('red') triangle_3.draw(field) string_3 = Line(Point(75, 300), Point(75, 345)) string_3.draw(field) # Create a while loop that moves the objets every 0.05 seconds upwards to # make it appear as if they are floating while True: sleep(0.05) balloon_1.move(0, -10) triangle_1.move(0, -10) string_1.move(0, -10) balloon_2.move(0, -10) triangle_2.move(0, -10) string_2.move(0, -10) balloon_3.move(0, -10) triangle_3.move(0, -10) string_3.move(0, -10) # If a click is detetced in the field, the window will close even if the # balloons are still moving in the while loop click = field.checkMouse() if click != None: break # Add player score to top_scores.txt writeScores(player_name, score) # Set close condition to True close = True # Return close condition return close
class Player: def __init__(self, pos, r, vel=1.0, c=color_rgb(255, 255, 255)): self.pos = pos #position self.radius = r #radius self.vel = vel #velocity self.colour = c self.object = Circle(self.pos, r) self.object.setFill(c) self.object.setOutline('black') self.bullets = [] #bullet list self.score = 0 print(self.object.getRadius()) def modifyScore(self, value): self.score += value #print("Score:"+str(self.score)) def getScore(self): return self.score def update(self, dt, win): x = 0.0 y = 0.0 if k.kUp(): #If key up is pressed y -= self.vel * dt if k.kDown(): #If key down is pressed y += self.vel * dt if k.kLeft(): #If key left is pressed x -= self.vel * dt if k.kRight(): #If key right is pressed x += self.vel * dt #Check if mouse has been clicked mouse = k.kMouseLeft(win) if mouse != None: #print(mouse) self.bullets.append( Bullet(self.pos, win, 10, Point(mouse.getX(), mouse.getY()), 1000, self.colour)) #Update bullets and check if out of bounds for bullet in self.bullets: bullet.update(dt, win) if bullet.getAliveFlag() != True: bullet.undraw() self.bullets.remove(bullet) self.object.move(x, y) self.pos = self.object.getCenter() def getBullets(self): return self.bullets def getObject(self): return self.object def getRadius(self): return self.object.getRadius() def getPos(self): return self.pos def draw(self, win): self.object.draw(win) #undraw object def undraw(self): self.object.undraw()