def draw_hold_button(): # hold button points hold_a = Point(WIN.getWidth() * 2 / 11 - 100, WIN.getHeight() * 3 / 12) hold_b = Point(hold_a.getX() + 200, hold_a.getY() + 100) hold_button = Rectangle(hold_a, hold_b) hold_button.setFill("green") hold_button.setOutline("black") hold_button.setWidth(5) # Obtains the center point of the hold button center_hold_button = Point(0.5 * (hold_a.getX() + hold_b.getX()), 0.5 * (hold_a.getY() + hold_b.getY())) # Text class hold_text = Text(center_hold_button, "Hold") # Style the text hold_text.setFace("helvetica") hold_text.setSize(26) hold_text.setStyle("bold") hold_text.setTextColor("black") hold_button.draw(WIN) hold_text.draw(WIN) return hold_a, hold_b
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_rectangle(win, colour, point1, point2, current_tile, fill=False): """Helper function for drawing rectangles.""" current_rectangle = Rectangle(Point(*point1), Point(*point2)) if fill: current_rectangle.setFill(colour) current_rectangle.setOutline(colour) current_rectangle.draw(win) current_tile.append(current_rectangle)
def partial(i, j, color): print(n, size, i, j, color) one = Point(i * size, j * size) two = Point(size * (i + 1), size * (j + 1)) rect = Rectangle(one, two) rect.setFill(color) rect.setOutline(color) rect.draw(win) return rect
def draw_rec(board_column, board_row, color, dot_size): rtl = (board_column * (size * 2)) + offsetC rtr = (board_row * (size * 2)) + offsetR rbl = (board_column * (size * 2) + offsetC + (dot_size * 2)) rbr = (board_row * (size * 2) + offsetR + (dot_size * 2)) head = Rectangle(Point(rtl, rtr), Point(rbl, rbr)) head.setFill(color) head.setOutline(color) head.draw(win)
def drawFieldPanel(): # Create Field panel field = GraphWin("The Field", 400, 400) # Create vertical and horizontal lines on Field panel for i in range(9): v_field_line = Line(Point(40 * (i + 1), 0), Point(40 * (i + 1), 400)) v_field_line.setOutline("light gray") v_field_line.draw(field) h_field_line = Line(Point(0, 40 * (i + 1)), Point(400, 40 * (i + 1))) h_field_line.setOutline("light gray") h_field_line.draw(field) # Color start and end squares start = Rectangle(Point(0, 0), Point(40, 40)) start.setFill("green") start.setOutline("light gray") start.draw(field) end = Rectangle(Point(360, 360), Point(400, 400)) end.setFill("red") end.setOutline("light gray") end.draw(field) # Draw black holes blackcenter1, blackcenter2 = blackHoles(field) # Create spin square Rectangle while True: # Make sure spin square is not drawn on top of black holes, and repeat # location process if it is on top of a black hole spin_x = randint(2, 9) * 40 - 20 spin_y = randint(2, 9) * 40 - 20 spin_square = Rectangle(Point(spin_x - 17, spin_y - 17), Point(spin_x + 17, spin_y + 17)) if spin_x != blackcenter1.getX() and spin_y != blackcenter1.getY( ) or spin_x != blackcenter2.getX() and spin_y != blackcenter2.getY(): break spin_square.setFill("blue") spin_square.draw(field) spin_text = Text(Point(spin_x, spin_y), "SPIN") spin_text.setTextColor("white") spin_text.draw(field) # Create initial Pete Rectangle pete = Rectangle(Point(4, 4), Point(36, 36)) pete.setFill("gold") pete.draw(field) # Draw and return sensors sensors, goodsensors = drawSensors(field) # Return objects return field, pete, sensors, spin_square, blackcenter1, blackcenter2, goodsensors
def displaygraphicalgrid(win, grid, scale): for i in range(0, len(grid)): for j in range(0, len(grid[i])): Rect = Rectangle(Point(i * scale, j * scale), Point((i + 1) * scale - 1, (j + 1) * scale - 1)) if grid[i][j].solid: Rect.setFill("white") Rect.setOutline("white") else: Rect.setFill("black") Rect.setOutline("black") Rect.draw(win)
class Button: def __init__(self, pos, width, height, type=None, bc=color_rgb(255, 255, 255), text="", size=18, txtc=color_rgb(0, 0, 0), style="normal", font="arial"): oWidth = width / 2 #offset width oHeight = height / 2 #offset height pos1 = Point(pos.getX() - oWidth, pos.getY() - oHeight) pos2 = Point(pos.getX() + oWidth, pos.getY() + oHeight) self.object = Rectangle(pos1, pos2) self.object.setFill(bc) self.object.setOutline(color_rgb(255, 255, 255)) self.pos = pos self.pos1 = pos1 #Left-Up Corner self.pos2 = pos2 #Right-Down Corner self.type = type self.width = width self.height = height self.text = Text(self.object.getCenter(), text, size, txtc, style, font) def draw(self, win): self.object.draw(win) self.text.draw(win) def undraw(self): self.object.undraw() self.text.undraw() def getPos(self): return self.pos def getPos1(self): return self.pos1 def getPos2(self): return self.pos2 def getType(self): return self.type
class Block: coordinates = None rectangle = None def __init__(self, x, y, window, fill=SNAKE_COLOR): self.coordinates = (x, y) self.rectangle = Rectangle( Point(x * TILE_SIZE, y * TILE_SIZE), Point((x + 1) * TILE_SIZE, (y + 1) * TILE_SIZE)) self.rectangle.setFill(fill) self.rectangle.setOutline(BACKGROUND_COLOR) self.rectangle.draw(window) def destroy(self): self.rectangle.undraw() return self.rectangle
def update_status_message(self, text): center_x = self.win.getWidth() / 2 center_y = 20 back_color = color_rgb(240, 240, 240) msg_width = 300 msg_height = 20 rect = Rectangle(Point(center_x - msg_width, center_y - msg_height), Point(center_x + msg_width, center_y + msg_height)) rect.setFill(back_color) rect.setOutline(back_color) rect.setWidth(1) # width of boundary line rect.draw(self.win) message = Text(Point(center_x, center_y), text) message.setTextColor('red') message.setStyle('bold') message.setSize(20) message.draw(self.win)
def main(): win = graphics.GraphWin() shape = Rectangle(Point(75,75),Point(125,125)) shape.setOutline('Red') shape.setFill('Red') shape.draw(win) for i in range(10): p = win.getMouse() tx = p.getX()-25 ty = p.getY()-25 bx = p.getX()+25 by = p.getY()+25 Rectangle(Point(tx,ty),Point(bx,by)).draw(win) Text(Point(100,180),'Click again to quit!').draw(win) win.getMouse() win.close()
def draw_deck_border(deck): # Abritatry value to gives a nice spacing. deck_border = 10 deck_width = deck.getWidth() deck_height = deck.getHeight() # Goes to middle of screen and minus' deck width minus border border_top_left = Point( WIN.getWidth() / 2 - deck_width / 2 - deck_border, WIN.getHeight() * 5 / 19 - deck_height / 2 - deck_border) border_bottom_right = Point( WIN.getWidth() / 2 + deck_width / 2 + deck_border, WIN.getHeight() * 5 / 19 + deck_height / 2 + deck_border) # Draws and styles it! deck_border = Rectangle(border_top_left, border_bottom_right) deck_border.setOutline("black") deck_border.setWidth(5) deck_border.draw(WIN)
def main(): win = GraphWin("Five Click House", 500, 500) win.setBackground(color_rgb(43, 43, 43)) p = win.getMouse() p1 = Point(p.getX(), p.getY()) p = win.getMouse() p2 = Point(p.getX(), p.getY()) base = Rectangle(p1, p2) # Base of the house, initial rectangle. base.setOutline(color_rgb(180, 180, 180)) base.draw(win) dist = sqrt((p2.getX() - p1.getX())**2 + (p2.getY() - p1.getY())**2) # Distance between p1 and p2. baseHeight = p1.getY() - p2.getY() # Height of the 'base' object. baseWidth = p2.getX() - p1.getX() # Width of the 'base' object. doorWidth = (baseWidth / 5) p = win.getMouse() p3 = Point(p.getX(), p.getY()) # Third Mouse Click. p4 = Point((p3.getX() - doorWidth / 2), p3.getY()) # Top left of door. p5 = Point(p3.getX() + doorWidth / 2, p1.getY()) # Bottom right of the door. door = Rectangle(p4, p5) door.setOutline(color_rgb(180, 180, 180)) door.draw(win) p = win.getMouse() p6 = Point(p.getX(), p.getY()) # Fourth Click. p7 = Point(p6.getX() - doorWidth / 4, p6.getY() - doorWidth / 4) # Top left of the window. p8 = Point(p6.getX() + doorWidth / 4, p6.getY() + doorWidth / 4) # Bottom right of the window window = Rectangle(p7, p8) window.setOutline(color_rgb(180, 180, 180)) window.draw(win) p = win.getMouse() p9 = Point(p.getX(), p.getY()) # Fifth Mouse Click, also tip of roof. p10 = Point(p1.getX(), p1.getY() - baseHeight) # Left corner of roof. p11 = Point(p2.getX(), p2.getY()) # Right corner of roof. roof = Polygon(p9, p10, p11) roof.setOutline(color_rgb(180, 180, 180)) roof.draw(win) print("Distance between p1 and p2:", dist) print("p1 x:", p1.getX(), "\np1 y:", p1.getY(), "\np2 x:", p2.getX(), "\np2 y:", p2.getY()) print("Base Height:", baseHeight, "\nBase Width:", baseWidth) win.getMouse() win.close()
class View: """ Will handle all functions necessary to provide a graphical representation of a World. """ def __init__(self, world: World, title: str): """ Creates a new View with a given World to represent. """ self.world = world self.window = GraphWin(title, world.width, world.height, autoflush=False) self.rectangles = [] self.vehicle = None self.camera = None def update(self, delta_t: float): """ Redraws this View (call upon update in coordinates). """ if self.vehicle: self.vehicle.move(self.world.vehicle.velocity[0] * delta_t, self.world.vehicle.velocity[1] * delta_t) if self.world.camera: #self.camera = Rectangle(self.world.camera.get_upper_left_corner(), self.world.camera.get_lower_right_corner()) if self.camera: self.camera.undraw() upper_left_corner = self.world.camera.get_upper_left_corner() lower_right_corner = self.world.camera.get_lower_right_corner() self.camera = Rectangle(Point(upper_left_corner[0], upper_left_corner[1]), Point(lower_right_corner[0], lower_right_corner[1])) self.camera.setFill("green") self.camera.setOutline("green") self.camera.draw(self.window) else: if self.camera: self.camera.undraw() self.camera = None self.window.update() def add_feature(self, upper_left_corner: tuple, lower_right_corner: tuple, color: str): """ Adds a feature to be drawn in this view. """ self.rectangles.append(Rectangle(Point(upper_left_corner[0], upper_left_corner[1]), Point(lower_right_corner[0], lower_right_corner[1]))) self.rectangles[len(self.rectangles) - 1].setFill(color) self.rectangles[len(self.rectangles) - 1].setOutline(color) self.rectangles[len(self.rectangles) - 1].draw(self.window) def set_vehicle(self, upper_left_corner: tuple, lower_right_corner: tuple, color: str): """ Sets the location of the Vehicle. """ self.vehicle = Rectangle(Point(upper_left_corner[0], upper_left_corner[1]), Point(lower_right_corner[0], lower_right_corner[1])) self.vehicle.setFill(color) self.vehicle.draw(self.window) def check_mouse(self): """ Checks if a mouse button has been clicked during the previous frame. Returns the specific button if a button was clicked, or None if not button was pressed. """ return self.window.checkMouse() def close_window(self): """ Closes the window associated with this View. """ self.window.close() def capture_png(self, name: str): """ Records a postscript (similar to PDF) of the current graphical representation to the given file name. Converts this postscript to a png. """ file_name = name + ".eps" self.window.postscript(file=file_name, colormode='color') img = Image.open(file_name) img.save(name + ".png", "png")
def draw_rectangle(win, point1, point2, colour): rectangle = Rectangle(point1, point2) rectangle.setFill(colour) rectangle.setOutline(colour) rectangle.draw(win)
closedSet_score.draw(win) pathLength_text = Text(Point(100, 455), "Path Length Score: ") pathLength_text.setTextColor(color="white") pathLength_text.setSize(10) pathLength_text.setStyle("bold") pathLength_text.draw(win) pathLength_text = Text(Point(175, 455), '') pathLength_text.setTextColor(color="white") pathLength_text.setSize(10) pathLength_text.setStyle("bold") pathLength_text.draw(win) a_rect = Rectangle(Point(900, 400), Point(950, 450)) a_rect.setOutline(color="white") a_rect.setWidth(3) a_rect.draw(win) Astar_text = Text(Point(925, 425), "A*") Astar_text.setSize(25) # Astar_text = Text(Point(925,425), "BFS") # Astar_text.setSize(15) # Astar_text = Text(Point(925,425), "DiJ") # Astar_text.setSize(15) Astar_text.setTextColor(color="white") # Astar_text.setTextColor(color_rgb(66, 134, 244)) Astar_text.setStyle("bold") Astar_text.draw(win) Description_text = Text(
def drawSensors(field): # Set initial coordinates to potentially draw sensors and empty list # of sensor locations x = 40 y = 40 sensors = [] goodsensors = [] # Initialize loop to create horizontal sensors for column in range(10): for row in range(10): # Create random number between 0 and 1 h_chance_border = random() v_chance_border = random() # Creates 40% chance horizontal sensor will be drawn if h_chance_border >= 0.6: # Define, draw, and append location of sensor horizontal_sensor = Rectangle(Point(x - 35.5, y), Point(x - 4.5, y)) horizontal_sensor.setWidth(2.5) horizontal_sensor.setOutline("orange") horizontal_sensor.draw(field) h_x = horizontal_sensor.getCenter().getX() h_y = horizontal_sensor.getCenter().getY() sensors.append([h_x, h_y]) # Creates 40% chance horizontal sensor will be drawn if v_chance_border >= 0.6: # Define, draw, and append location of sensor vertical_sensor = Rectangle(Point(x, y - 35.5), Point(x, y - 4.5)) vertical_sensor.setWidth(2.5) vertical_sensor.setOutline("orange") vertical_sensor.draw(field) v_x = vertical_sensor.getCenter().getX() v_y = vertical_sensor.getCenter().getY() sensors.append([v_x, v_y]) # Move to next row y += 40 # Set back to first row after finishing final row y = 40 # Move to the next column x += 40 x = 40 y = 40 # Initialize loop to create good horizontal sensors for column in range(9): for row in range(9): # Create random number between 0 and 1 h_chance_border = random() v_chance_border = random() # Creates 40% chance horizontal sensor will be drawn if h_chance_border <= .25: # Define, draw, and append location of sensor horizontal_sensorgood = Rectangle(Point(x - 35.5, y), Point(x - 4.5, y)) horizontal_sensorgood.setWidth(2.5) horizontal_sensorgood.setOutline("green") h_xg = horizontal_sensorgood.getCenter().getX() h_yg = horizontal_sensorgood.getCenter().getY() if [h_xg, h_yg] not in sensors: horizontal_sensorgood.draw(field) goodsensors.append([h_xg, h_yg]) # Creates 40% chance horizontal sensor will be drawn if v_chance_border <= 0.25: # Define, draw, and append location of sensor vertical_sensorgood = Rectangle(Point(x, y - 35.5), Point(x, y - 4.5)) vertical_sensorgood.setWidth(2.5) vertical_sensorgood.setOutline("green") v_xg = vertical_sensorgood.getCenter().getX() v_yg = vertical_sensorgood.getCenter().getY() if [v_xg, v_yg] not in sensors: vertical_sensorgood.draw(field) goodsensors.append([v_xg, v_yg]) # Move to next row y += 40 # Set back to first row after finishing final row y = 40 # Move to the next column x += 40 # Draw vertical sensors return sensors, goodsensors