def __init__(self, game: GameView, grid_size: int): """Square grid, with a little space around the tiles. Args: game: The surrounding GameView object """ self.game = game self.win = game.win self.background = graphics.Rectangle( graphics.Point(0, 0), graphics.Point(game.width, game.height)) self.background.setFill("wheat") self.background.draw(self.win) self.cell_width = (game.width - MARGIN) / grid_size self.tile_width = self.cell_width - MARGIN self.cell_height = (game.height - MARGIN) / grid_size self.tile_height = self.cell_height - MARGIN self.tiles = [] # Initially empty tile spaces for row in range(grid_size): row_tiles = [] for col in range(grid_size): ul, lr = self.tile_corners(row, col) tile_background = graphics.Rectangle(ul, lr) tile_background.setFill("grey") tile_background.setOutline("white") tile_background.draw(self.win) row_tiles.append(tile_background) self.tiles.append(row_tiles)
def is_in(point, is_grid, is_tile): left, right = is_grid.tile_corners(is_tile.r, is_tile.c) white = g.Rectangle(left, right) ll = white.getP1() ur = white.getP2() return ll.getX() < point.getX() < ur.getX() and ll.getY() < point.getY( ) < ur.getY()
def __init__(self, grid: GridView, tile: model.Tile): """Display the tile on the grid. Internally there are actually two graphics objects: A background rectangle and text within it. The background rectangle has a visible outline until the first time it moves. """ self.grid = grid self.win = grid.win self.row = tile.row self.col = tile.col self.value = tile.value ul, lr = grid.tile_corners(self.row, self.col) background = graphics.Rectangle(ul, lr) background.setFill(RAMP[self.value]) background.setOutline(TILE_OUTLINE_NEW) self.background = background cx = (ul.getX() + lr.getX()) / 2.0 cy = (ul.getY() + lr.getY()) / 2.0 center = graphics.Point(cx, cy) label = graphics.Text(center, str(self.value)) label.setSize(36) self.label = label background.draw(self.win) label.draw(self.win)
def __init__(self, text, p1, p2): self.text = text self.rect = graphics.Rectangle(p1, p2) self.text = graphics.Text(self.rect.getCenter(), text) self.clicked = False self.visible = False
def __init__(self, game, grid_size): self.game = game self.win = game.win self.bg = g.Rectangle(g.Point(0, 0), g.Point(game.h_w, game.h_w)) self.bg.setFill(black) self.bg.draw(self.win) self.cell_h_w = (game.h_w - m) / grid_size self.tile_h_w = self.cell_h_w - m self.tiles = [] for r in range(grid_size): row_tiles = [] for c in range(grid_size): left, right = self.tile_corners(r, c) tile_bg = g.Rectangle(left, right) tile_bg.setFill(white) tile_bg.draw(self.win) row_tiles.append(tile_bg) self.tiles.append(row_tiles)
def __init__(self, window, p1, p2): self.window = window position1 = p1 position2 = p2 self.rect = graphics.Rectangle(position1, position2) self.rect.setOutline("black") self.rect.setFill("grey") self.text = None
def bar(self, col: int, height: int, color, frac_width=1.0): if col > self.ncols: log.debug(f"Column {col} off chart") return thin_by = (1.0 - frac_width) * self.col_width right = col * self.col_width - thin_by left = (col - 1) * self.col_width + thin_by bottom = self.height top = self.height - (height * self.unit_height ) # Measuring from top of window r = graphics.Rectangle(graphics.Point(left, bottom), graphics.Point(right, top)) r.setFill(color) r.draw(self.win)
def __init__(self, window: graphics.GraphWin): self.nation = None self.window = window position1 = graphics.Point(0, 400) position2 = graphics.Point(200, 500) self.rect = graphics.Rectangle(position1, position2) self.rect.setOutline("black") self.rect.setFill("grey") self.text = None self.attackButton = Button('Attack', graphics.Point(0, 400), graphics.Point(50, 425)) self.recruitButton = Button('Recruit', graphics.Point(50,400), graphics.Point(100, 425)) self.recruit_amount = graphics.Entry(graphics.Point(125, 412.5), 4)
def __init__(self, grid, t): self.grid = grid self.win = grid.win self.r = t.r self.c = t.c self.val = t.val left, right = grid.tile_corners(self.r, self.c) white = g.Rectangle(left, right) white.setFill(xo_colors[self.val]) self.bg = white x = (left.getX() + right.getX()) / 2 y = (left.getY() + right.getY()) / 2 center = g.Point(x, y) label = g.Text(center, self.val) label.setSize(36) if self.val in nums: label.setFill("white") self.label = label white.draw(self.win) label.draw(self.win) self.x = x self.y = y
def __init__(self, pxwidth: int, pxheight: int, ncols: int, v_min: int, v_max: int, autoflush=True, title="Chart", background=graphics.color_rgb(255, 255, 255)): self.width = pxwidth self.height = pxheight self.ncols = ncols self.win = graphics.GraphWin("Chart", pxwidth, pxheight, autoflush=autoflush) bkgrnd = graphics.Rectangle(graphics.Point(0, 0), graphics.Point(pxwidth, pxheight)) bkgrnd.setFill(background) self.col_width = pxwidth / ncols self.unit_height = pxheight / (v_max - v_min) self._last_update = time.time()
from graphics import graphics from shapely import geometry from nation import Nation size = (1000, 1000) window = graphics.GraphWin("Map", size[0], size[1]) window.setCoords(0, 0, 500, 500) nations = [] points = [] graphics_points = [] rect = graphics.Rectangle(graphics.Point(0, 400), graphics.Point(200, 500)) rect.setFill("grey") rect.draw(window) previous_point = None name_box = graphics.Entry(rect.getCenter(), 10) name_box.draw(window) color_box = graphics.Entry(rect.getCenter(), 10) color_box.move(0, -30) color_box.draw(window) while True: clickedPoint = window.getMouse() if 0 < clickedPoint.getX() < 200 and 400 < clickedPoint.getY() < 500: tempNation = Nation(name_box.getText(), color_box.getText(), 1000, points)