def _draw_all_tiles(self, canvas_width, canvas_height) -> None: '''This method interacts with the game logic and draws all the tiles, black or white, wherever the current game state has them''' for row in range(self._state._board_row): for col in range(self._state._board_col): top_left_point = point.from_frac( (col * self._width_tile) + 0.01, (row * self._height_tile) + 0.01) bottom_right_point = point.from_frac( ((col + 1) * self._width_tile) - 0.01, ((row + 1) * self._height_tile) - 0.01) x1, y1 = top_left_point.pixel(canvas_width, canvas_height) x2, y2 = bottom_right_point.pixel( canvas_width, canvas_height) if self._state.get_tile_position(row, col).strip() == 'B': self._canvas.create_oval( x1, y1, x2, y2, fill = 'black') elif self._state.get_tile_position(row, col).strip() == 'W': self._canvas.create_oval( x1, y1, x2, y2, fill = 'white', outline = 'white')
def create_empty_board(): all_tiles = [] for row in range(8): for col in range(8): topleft_frac = point.from_frac(col / 8, row / 8) bottomright_frac = point.from_frac((col + 1) / 8, (row + 1) / 8) all_tiles.append(Tile(row, col, topleft_frac, bottomright_frac)) return all_tiles
def __init__(self): self._width = ORIGINAL_PADDLE_SIZE self._height = .025 self._topleft_corner = point.from_frac(0, 0) self._bottomright_corner = point.from_frac(0, 0) self._x_position = .5 self._lengthened = False self._shortened = False
def create_tile_points(row, col, total_row, total_col) -> (point.Point): '''Returns the top-left and bottom-right points of a box with the given row and column''' x = (.9 / total_col) y = (.9 / total_row) pad = 0.05 return(point.from_frac( (pad + (x*col)), (pad + (y*row))), point.from_frac( (pad + (x*(col+1))), (pad + (y*(row+1)))))
def create_dot_points(row: int, col: int, total_row: int, total_col: int) -> (point.Point): '''Given a dot's position in a board, returns bounding box points for the dot''' x = (.8 / total_col) y = (.8 / total_row) pad = 0.1 radius = 0.015 return (point.from_frac( (pad + (x*col) - radius), (pad + (y*row) - radius)), point.from_frac( (pad + (x*col) + radius), (pad + (y*row) + radius)))
def __init__(self): self._topleft_corner = point.from_frac(0, 0) self._bottomright_corner = point.from_frac(0, 0) self._height = 0 self._color = None self._hit_status = False self._lengthen_paddle_power_up = False self._shortened_paddle_power_up = False self._multi_ball_power_up = False
def _draw_vertical_lines(self, canvas_width, canvas_height) -> None: '''This method draws the vertical lines on the canvas''' for j in range(self._state._board_col + 1): point_top = point.from_frac(j * self._width_tile, 0) point_bottom = point.from_frac(j * self._width_tile, canvas_height) x1, y1 = point_top.pixel(canvas_width, canvas_height) x2, y2 = point_bottom.pixel(canvas_width, canvas_height) self._canvas.create_line(x1, y1, x2, y2)
def _draw_horizontal_lines(self, canvas_width, canvas_height) -> None: '''This method draws the horizontal lines on the canvas''' for i in range(self._state._board_row + 1): point_left = point.from_frac(0, i * self._height_tile) point_right = point.from_frac(canvas_width, i * self._height_tile) x1, y1 = point_left.pixel(canvas_width, canvas_height) x2, y2 = point_right.pixel(canvas_width, canvas_height) self._canvas.create_line(x1, y1, x2, y2)
def create_dot_points(row: int, col: int, total_row: int, total_col: int) -> (point.Point): '''Given a dot's position in a board, returns bounding box points for the dot''' x = (.8 / total_col) y = (.8 / total_row) pad = 0.1 radius = 0.015 return (point.from_frac((pad + (x * col) - radius), (pad + (y * row) - radius)), point.from_frac((pad + (x * col) + radius), (pad + (y * row) + radius)))
def disc_coords(self) -> (point.Point, point.Point): ''' returns the fractional coordinates of the disc that will be drawn within this cell We don't draw the disc here since it will depend on actual pixel coordinates ''' origin_coord = self._origin.frac() bottom_right_coord = self._bottom_right.frac() x_spacing = ( (bottom_right_coord[0] - origin_coord[0]) * _DISC_SPACE_FRAC) / 2 y_spacing = ( (bottom_right_coord[1] - origin_coord[1]) * _DISC_SPACE_FRAC) / 2 top_left_coord = point.from_frac(origin_coord[0] + x_spacing, origin_coord[1] + y_spacing) bottom_right_coord = point.from_frac(bottom_right_coord[0] - x_spacing, bottom_right_coord[1] - y_spacing) return (top_left_coord, bottom_right_coord)
def move(self) -> tuple: new_top_left_x = self._x_position - (.5 * self._width) new_top_left_y = 1 - self._height new_bottom_right_x = self._x_position + (.5 * self._width) new_bottom_right_y = 1 if new_top_left_x < 0: new_top_left_x = 0 new_bottom_right_x = self._width if new_bottom_right_x > 1: new_bottom_right_x = 1 new_top_left_x = 1 - self._width self._topleft_corner = point.from_frac(new_top_left_x, new_top_left_y) self._bottomright_corner = point.from_frac(new_bottom_right_x, new_bottom_right_y)
def _load_discs(self) -> None: """Load disc into the canvas""" board = self._game_state.board for piece in board.list_of_pieces(): row, col = piece.location row_frac = float(1 + row * 2) / float(board.rows * 2) col_frac = float(1 + col * 2) / float(board.columns * 2) center_point = point.from_frac(col_frac, row_frac) self._model_state.add_disc(center_point, piece)
def contains(self, click_x, click_y, board_width, board_height) -> bool: """ Returns whether or not the given pixel coordinates are within the radius of the center of the cell/disc. """ click = point.from_pixel(click_x, click_y, board_width, board_height) center = point.from_frac(self._frac_x, self._frac_y) distance = click.frac_distance_from(center) return distance <= self._frac_radius
def __init__(self, top_left: 'Point', bottom_right: 'Point', dimensions: '(width,height)', raw_dict_notes = [dict()], color = BLACK, time_signature = (4, .25), current_note_time = -1 ,number_of_measures = 4): if len(raw_dict_notes) != number_of_measures: raise TimeError() # Coordinates self._window_width, self._window_height = dimensions #need the window size to go from fractional coordinates to actual pixels self._x1, self._y1 = top_left.pixel(self._window_width, self._window_height) #(x1, y1) self._x2, self._y2 = bottom_right.pixel(self._window_width,self._window_height) self._staff_width = self._x2 - self._x1 self._staff_height = self._y2 - self._y1 # Measures self._current_note_time = current_note_time self._measure_frac_width = (bottom_right.frac()[0] - top_left.frac()[0]) / number_of_measures self._measures = [] for i, measure_notes in enumerate(raw_dict_notes): temp_top_left_point = point.from_frac(top_left.frac()[0]+ (self._measure_frac_width * i), top_left.frac()[1]) temp_bottom_right_point = point.from_frac(top_left.frac()[0]+ (self._measure_frac_width * (i+1)), bottom_right.frac()[1]) self._measures.append(MeasureSketch(temp_top_left_point, temp_bottom_right_point, dimensions, measure_notes, color, time_signature, current_note_time))
def __init__(self, num_rows: int, num_cols: int): ''' A Board instance represents the layout of the Othello board composed of Cell instances for each location the discs can be placed. These Cell instances contain the fractional coordinates that will help with drawing the Othello grid and the discs ''' self._num_rows = num_rows self._num_cols = num_cols # create the board with cells defined as Cell objects self._board = [] for row in range(self._num_rows): cell_row = [] for col in range(self._num_cols): origin = point.from_frac(col * (1 / self._num_cols), row * (1 / self._num_rows)) bottom_right = point.from_frac( ((col + 1) * (1 / self._num_cols)), ((row + 1) * (1 / self._num_rows))) cell_row.append(Cell(origin, bottom_right)) self._board.append(cell_row)
def _create_list_of_bricks(self) -> list: import random top_gap = .05 percentage_covered_by_bricks = .45 list_of_bricks = [] for row in range(self._num_rows): for col in range(self._num_columns): B = Brick() B.set_color(self._list_of_colors[row]) B.set_height(percentage_covered_by_bricks / self._num_rows) top_left_x = col / self._num_columns top_left_y = top_gap + row * B._height bottom_right_x = (col + 1) / self._num_columns bottom_right_y = top_left_y + B._height B._topleft_corner = point.from_frac(top_left_x, top_left_y) B._bottomright_corner = point.from_frac( bottom_right_x, bottom_right_y) random_power_up_number = random.randint(1, 100) if 1 < random_power_up_number < 7: B._lengthen_paddle_power_up = True if 7 < random_power_up_number < 14: B._shortened_paddle_power_up = True if 14 < random_power_up_number < 20: B._multi_ball_power_up = True list_of_bricks.append(B) return list_of_bricks
def draw_board(self): self._canvas.delete(tkinter.ALL) self.move_list = [] self.game.track_score() self._wscore.set(self.game._wscore) self._bscore.set(self.game._bscore) self._first.set(self.game._first) for row in range (self.game._rows): for column in range (self.game._columns): top_left = point.from_frac(column/self.game._columns, row/self.game._rows) bottom_right = point.from_frac((column+1)/self.game._columns, (row+1)/self.game._rows) width = self._canvas.winfo_width() height = self._canvas.winfo_height() top_left_x,top_left_y = top_left.pixel(width,height) bottom_right_x,bottom_right_y = bottom_right.pixel(width,height) x1, y1 = top_left.frac() x2, y2 = bottom_right.frac() self.move_list.append([x1,y1,x2,y2,width,height,row,column]) color = '' if self.game.board[row][column] == 'Black': color = 'black' elif self.game.board[row][column] == 'White': color = 'white' self._canvas.create_rectangle(top_left_x,top_left_y,bottom_right_x,bottom_right_y,fill = 'green') self._canvas.create_oval(top_left_x,top_left_y,bottom_right_x,bottom_right_y,outline="",fill = color) if self.game.end_game(): self._winner.set(self.game.determine_winner())
def _draw_spot(self, spot: spots_model.Spot) -> None: self._canvas.delete(tkinter.ALL) center_frac_x, center_frac_y = spot.center_point().frac() radius_frac = spot.radius_frac() topleft_frac_x = center_frac_x - radius_frac topleft_frac_x = center_frac_x - radius_frac topleft = Point.from_frac(topleft_frac_x, topleft_frac_y) bottomleft_frac_x = center_frac_x + radius_frac bottomleft_frac_x = center_frac_x + radius_frac bottomleft = point.from_frac(bottomright_frac_x, bottomright_frac_y) topleft_pixel_x, topleft_pixel_y = topleft.pixel(width, height) bottomright_pixel_x, bottomright_pixel_y = bottomright.pixel( width, height) self._canvas.create_oval(topleft_pixel_x, topleft_pixel_y, bottomright_pixel_x, bottom_right_pixel_y, fill='yellow')
def move(self): current_x_center, current_y_center = self._center.as_frac() new_center_x = current_x_center + self._xdelta new_center_y = current_y_center + self._ydelta if new_center_x + self._frac_radius > 1.0: too_far_x = new_center_x + self._frac_radius - 1 new_center_x = new_center_x - too_far_x self.bounce('x') if new_center_x - self._frac_radius < 0: too_far_x = -(new_center_x - self._frac_radius) new_center_x = new_center_x + too_far_x self.bounce('x') if new_center_y - self._frac_radius < 0: too_far_y = -(new_center_y - self._frac_radius) new_center_y = new_center_y + too_far_y self.bounce('y') self._center = point.from_frac(new_center_x, new_center_y)
def __init__(self): self._spots = [ # x y radius Spot(point.from_frac(0.5, 0.5), 0.05), Spot(point.from_frac(0.1, 0.8), 0.1), Spot(point.from_frac(0.3, 0.2), 0.02) ]
def __init__(self): self._center = point.from_frac(0, 0) self._frac_radius = .015 self._xdelta = ORIGINAL_X_DELTA self._ydelta = ORIGINAL_Y_DELTA
def center(self) -> point.Point: '''Returns the center point of the dot''' return point.from_frac( (self._tl_x + self._br_x)/2, (self._tl_y + self._br_y)/2 )
def set_center(self, x: float, y: float): '''takes an x and y fractional coordinate and sets the center of the ball to those coordinates''' self._center = point.from_frac(x, y)
def center(self) -> point.Point: '''Returns the center point of the dot''' return point.from_frac((self._tl_x + self._br_x) / 2, (self._tl_y + self._br_y) / 2)