def main(): fileInput = "C:\Programming\CS138\Week 8\canoe.gif" fileOutput = "C:\Programming\CS138\Week 8\grayCanoe.gif" image = Image(Point(400, 300), fileInput) # Getting our image. width = image.getWidth() height = image.getHeight() win = GraphWin("Monochromatic Conversion", width, height) # Creating our window. image.draw(win) row = 0 # The starting pixel for our x-axis column = 0 # The starting pixel for our y-axis win.getMouse() for row in range(image.getWidth()): for column in range(image.getHeight()): r, g, b = image.getPixel(row, column) brightness = int(round(0.299 * r + 0.587 * g + 0.114 * b)) image.setPixel(row, column, color_rgb(brightness, brightness, brightness)) win.update() image.save(fileOutput) win.getMouse() win.close()
class GraphicsDisplay(Display): def __init__(self, cols=None, rows=None, window=None, scale=None): super().__init__() if scale is not None: self.scale = scale else: self.scale = 0.036 if window is not None: self.window = window else: windowWidth = self.scale * ((self.cols * characterDiameter) + ((self.cols + 1) * characterRadius)) windowHeight = self.scale * ((self.rows * characterDiameter) + ((self.rows + 1) * characterRadius)) self.window = GraphWin("Retrotextual", windowWidth, windowHeight, autoflush=False) self.window.setBackground("black") center = arr([characterDiameter, characterDiameter]) for row in range(self.rows): for col in range(self.cols): self.characters.append(GraphicsCharacter(self, center)) center[0] += characterDiameter + characterGap center[0] = characterDiameter center[1] = characterRadius * 5 def show(self): self.window.update()
def main(): gui = False if len(sys.argv) > 1: if str(sys.argv[1]) == 'cli': gui = False if(gui): myboard = Board(8) comp = BotRandom(myboard, 'b') print(myboard.outputFEN()) win = GraphWin('Chess', WIDTH, HEIGHT) while not win.isClosed(): drawBoard(myboard, win) if myboard.gamestate != 0: print("Checkmate!") drawCheckmate(myboard, win) if myboard.active == 'w': getNextMove(myboard, win) else: comp.getNextMove() # print(myboard.allLegalMoves(True)) win.update() time.sleep(.1) win.close() else: boardSize = 8 if len(sys.argv) > 2: boardSize = int(sys.argv[2]) myboard = Board(boardSize) comp = BotHeuristic(myboard, 'b') print(myboard.outputFEN()) while myboard.gamestate == 0: print(myboard.asciiBoard()) if myboard.active == 'w': cliNextMove(myboard) else: comp.getNextMove() print(myboard.asciiBoard()) if myboard.gamestate == 1: print("Game Over! Black Wins!") if myboard.gamestate == 2: print("Game Over! White Wins!") if myboard.gamestate == 3: print("Game Over! Draw!")
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")
class VScreen: def __init__(self, name, size): """Size is a Tuple(x,y) Win is a GraphWin from graphics.py""" self.__matrix = [["#000000" for y in range(size[1] + 1)] for x in range(size[0] + 1)] # self.__matrix_anti_alising = [["#000000" for y in range(size[1] + 1)] for x in range(size[0] + 1)] self.win = GraphWin(name, size[0], size[1], autoflush=False) self.win.setCoords(0, 0, size[0], size[1]) self.win.setBackground("black") self.size = size scenario(self) # def alising(self): def refresh(self): self.win.update() def get_point_color(self, p): try: point_color = self.__matrix[p[0]][p[1]] return point_color except IndexError: print(f"Trying to place point: {p[0]},{p[1]}") def __str__(self): vs = '' for x in range(0, self.size[0]): for y in range(0, self.size[1]): vs += f'({x}, {y}: {self.__matrix[x][y]}), ' print(vs) vs = '' return 'Objeto printado!' def point_1_screen(self, p, color='#000000'): try: if (color != None): self.win.plot(p[0], p[1], color) except IndexError: print(f"Trying to place point: {p[0]},{p[1]}") def point_1(self, p, color='#000000'): try: self.__matrix[p[0]][p[1]] = color self.win.plot(p[0], p[1], color) except IndexError: print(f"Trying to place point: {p[0]},{p[1]}") def point_2(self, p, color='#000000'): """ # - ponto passado para a funcao #* ** """ self.point_1((p[0] + 1, p[1]), color) self.point_1((p[0], p[1] + 1), color) self.point_1((p[0], p[1]), color) self.point_1((p[0] + 1, p[1] + 1), color) def point_3(self, p, color="#000000"): ''' # - ponto passado para a funcao * *#* * ''' self.point_1((p[0], p[1]), color) self.point_1((p[0], p[1] - 1), color) self.point_1((p[0], p[1] + 1), color) self.point_1((p[0] - 1, p[1]), color) self.point_1((p[0] + 1, p[1]), color) def point_4(self, p, color="#000000"): """ # - ponto passado para a funcao ** *#** **** ** """ self.point_1((p[0], p[1]), color) self.point_1((p[0] + 1, p[1]), color) self.point_1((p[0] + 2, p[1]), color) self.point_1((p[0] - 1, p[1]), color) self.point_1((p[0], p[1] - 1), color) self.point_1((p[0] + 1, p[1] - 1), color) self.point_1((p[0] - 1, p[1] + 1), color) self.point_1((p[0], p[1] + 1), color) self.point_1((p[0] + 1, p[1] + 1), color) self.point_1((p[0] + 2, p[1] + 1), color) self.point_1((p[0], p[1] + 2), color) self.point_1((p[0] + 1, p[1] + 2), color) def fill(self, p, color='#000000'): print("In Fill") lista = list() x = p lista.append(x) while (len(lista) != 0): high = (x[0] > 0 and x[1] > 0) low = (x[0] < self.size[0] and x[1] < self.size[1]) if color != self.__matrix[x[0]][x[1]] and high and low: self.point_1(x, color) lista.append((x[0] + 1, x[1])) lista.append((x[0], x[1] + 1)) lista.append((x[0] - 1, x[1])) lista.append((x[0], x[1] - 1)) x = lista.pop(0) print("Out Fill") # Linhas def line(self, p1, p2, size, color='#000000'): if (size == 1): x1 = p1[0] y1 = p1[1] x2 = p2[0] y2 = p2[1] p = 0 dX = x2 - x1 dY = y2 - y1 xInc = 1 yInc = 1 if (dX < 0): xInc = -1 dX = -dX if (dY < 0): yInc = -1 dY = -dY if (dY <= dX): p = dX / 2 while (x1 != x2): self.point_1((x1, y1), color) p = p - dY if (p < 0): y1 = y1 + yInc p = p + dX x1 = x1 + xInc else: p = dY / 2 while (y1 != y2): self.point_1((x1, y1), color) p = p - dX if (p < 0): x1 = x1 + xInc p = p + dY y1 = y1 + yInc self.point_1((x1, y1), color) elif (size == 2): x1 = p1[0] y1 = p1[1] x2 = p2[0] y2 = p2[1] p = 0 dX = x2 - x1 dY = y2 - y1 xInc = 1 yInc = 1 if (dX < 0): xInc = -1 dX = -dX if (dY < 0): yInc = -1 dY = -dY if (dY <= dX): p = dX / 2 while (x1 != x2): self.point_2((x1, y1), color) p = p - dY if (p < 0): y1 = y1 + yInc p = p + dX x1 = x1 + xInc else: p = dY / 2 while (y1 != y2): self.point_2((x1, y1), color) p = p - dX if (p < 0): x1 = x1 + xInc p = p + dY y1 = y1 + yInc self.point_2((x1, y1), color) elif (size == 3): x1 = p1[0] y1 = p1[1] x2 = p2[0] y2 = p2[1] p = 0 dX = x2 - x1 dY = y2 - y1 xInc = 1 yInc = 1 if (dX < 0): xInc = -1 dX = -dX if (dY < 0): yInc = -1 dY = -dY if (dY <= dX): p = dX / 2 while (x1 != x2): self.point_3((x1, y1), color) p = p - dY if (p < 0): y1 = y1 + yInc p = p + dX x1 = x1 + xInc else: p = dY / 2 while (y1 != y2): self.point_3((x1, y1), color) p = p - dX if (p < 0): x1 = x1 + xInc p = p + dY y1 = y1 + yInc self.point_3((x1, y1), color) elif (size == 4): x1 = p1[0] y1 = p1[1] x2 = p2[0] y2 = p2[1] p = 0 dX = x2 - x1 dY = y2 - y1 xInc = 1 yInc = 1 if (dX < 0): xInc = -1 dX = -dX if (dY < 0): yInc = -1 dY = -dY if (dY <= dX): p = dX / 2 while (x1 != x2): self.point_4((x1, y1), color) p = p - dY if (p < 0): y1 = y1 + yInc p = p + dX x1 = x1 + xInc else: p = dY / 2 while (y1 != y2): self.point_4((x1, y1), color) p = p - dX if (p < 0): x1 = x1 + xInc p = p + dY y1 = y1 + yInc self.point_4((x1, y1), color) # Circulo def circle(self, c, r, size, color="#000000"): print("In Circle") if (size == 1): # c eh o PONTO DO CENTRO DO CIRCULO # r eh o RAIO do circulo x = 0 y = r p = 5 / 4 - r self.point_1((x + c[0], y + c[1]), color) self.point_1((-x + c[0], y + c[1]), color) self.point_1((-x + c[0], -y + c[1]), color) self.point_1((x + c[0], -y + c[1]), color) self.point_1((y + c[0], x + c[1]), color) self.point_1((-y + c[0], x + c[1]), color) self.point_1((-y + c[0], -x + c[1]), color) self.point_1((y + c[0], -x + c[1]), color) while (x < y): x += 1 if (p < 0): p += 2 * x + 1 else: y = y - 1 p += 2 * x + 1 - 2 * y self.point_1((x + c[0], y + c[1]), color) self.point_1((-x + c[0], y + c[1]), color) self.point_1((-x + c[0], -y + c[1]), color) self.point_1((x + c[0], -y + c[1]), color) self.point_1((y + c[0], x + c[1]), color) self.point_1((-y + c[0], x + c[1]), color) self.point_1((-y + c[0], -x + c[1]), color) self.point_1((y + c[0], -x + c[1]), color) elif (size == 2): # c eh o PONTO DO CENTRO DO CIRCULO # r eh o RAIO do circulo x = 0 y = r p = 5 / 4 - r self.point_2((x + c[0], y + c[1]), color) self.point_2((-x + c[0], y + c[1]), color) self.point_2((-x + c[0], -y + c[1]), color) self.point_2((x + c[0], -y + c[1]), color) self.point_2((y + c[0], x + c[1]), color) self.point_2((-y + c[0], x + c[1]), color) self.point_2((-y + c[0], -x + c[1]), color) self.point_2((y + c[0], -x + c[1]), color) while (x < y): x += 1 if (p < 0): p += 2 * x + 1 else: y = y - 1 p += 2 * x + 1 - 2 * y self.point_2((x + c[0], y + c[1]), color) self.point_2((-x + c[0], y + c[1]), color) self.point_2((-x + c[0], -y + c[1]), color) self.point_2((x + c[0], -y + c[1]), color) self.point_2((y + c[0], x + c[1]), color) self.point_2((-y + c[0], x + c[1]), color) self.point_2((-y + c[0], -x + c[1]), color) self.point_2((y + c[0], -x + c[1]), color) elif (size == 3): # c eh o PONTO DO CENTRO DO CIRCULO # r eh o RAIO do circulo x = 0 y = r p = 5 / 4 - r self.point_3((x + c[0], y + c[1]), color) self.point_3((-x + c[0], y + c[1]), color) self.point_3((-x + c[0], -y + c[1]), color) self.point_3((x + c[0], -y + c[1]), color) self.point_3((y + c[0], x + c[1]), color) self.point_3((-y + c[0], x + c[1]), color) self.point_3((-y + c[0], -x + c[1]), color) self.point_3((y + c[0], -x + c[1]), color) while (x < y): x += 1 if (p < 0): p += 2 * x + 1 else: y = y - 1 p += 2 * x + 1 - 2 * y self.point_3((x + c[0], y + c[1]), color) self.point_3((-x + c[0], y + c[1]), color) self.point_3((-x + c[0], -y + c[1]), color) self.point_3((x + c[0], -y + c[1]), color) self.point_3((y + c[0], x + c[1]), color) self.point_3((-y + c[0], x + c[1]), color) self.point_3((-y + c[0], -x + c[1]), color) self.point_3((y + c[0], -x + c[1]), color) elif (size == 4): # c eh o PONTO DO CENTRO DO CIRCULO # r eh o RAIO do circulo x = 0 y = r p = 5 / 4 - r self.point_4((x + c[0], y + c[1]), color) self.point_4((-x + c[0], y + c[1]), color) self.point_4((-x + c[0], -y + c[1]), color) self.point_4((x + c[0], -y + c[1]), color) self.point_4((y + c[0], x + c[1]), color) self.point_4((-y + c[0], x + c[1]), color) self.point_4((-y + c[0], -x + c[1]), color) self.point_4((y + c[0], -x + c[1]), color) while (x < y): x += 1 if (p < 0): p += 2 * x + 1 else: y = y - 1 p += 2 * x + 1 - 2 * y self.point_4((x + c[0], y + c[1]), color) self.point_4((-x + c[0], y + c[1]), color) self.point_4((-x + c[0], -y + c[1]), color) self.point_4((x + c[0], -y + c[1]), color) self.point_4((y + c[0], x + c[1]), color) self.point_4((-y + c[0], x + c[1]), color) self.point_4((-y + c[0], -x + c[1]), color) self.point_4((y + c[0], -x + c[1]), color) print("Out Circle")