def _draw_board(self): self.canvas_width = self._canvas.winfo_width() self.canvas_height = self._canvas.winfo_height() self.column_spacing = self.canvas_width / self._othello_state.columns self.row_spacing = self.canvas_height / self._othello_state.rows self.point_row_spacing = point.from_pixel(0, self.row_spacing, self.canvas_width, self.canvas_height) self.point_col_spacing = point.from_pixel(self.column_spacing, 0, self.canvas_width, self.canvas_height) each_row = 0 each_col = 0 for row in range(1, self._othello_state.rows): each_row += self.point_row_spacing._frac_y self._canvas.create_line(0, each_row * self.canvas_height, self.canvas_width, each_row * self.canvas_height) for column in range(1, self._othello_state.columns): each_col += self.point_col_spacing._frac_x self._canvas.create_line(each_col * self.canvas_width, 0, each_col * self.canvas_width, self.canvas_height)
def point_list(self): '''Returns a list of top left and bottom right points''' canvas_width = self._canvas.winfo_width() canvas_height = self._canvas.winfo_height() pointlist = [] for pc in self.paired_coordinates(): tl = point.from_pixel(pc[0][0],pc[0][1],canvas_width, canvas_height) br = point.from_pixel(pc[1][0],pc[1][1],canvas_width, canvas_height) pointlist.append([tl,br]) return pointlist
def _debug_mode_clicked(self, event: tkinter.Event): self._update_canvas_size() click_location = point.from_pixel(event.x, event.y, self._canvas_width, self._canvas_height) click_was_in = None, None for y in range(self._state.board.ROWS): for x in range(self._state.board.COLS): if self._is_in_quadrant(self._board_point_index[y][x], click_location): click_was_in = y, x if None in click_was_in: return else: occupier = self._state.board[click_was_in[0]][click_was_in[1]] if occupier <= 1: player = occupier + 1 else: player = 0 self._state.board.modify_board( { 'y': click_was_in[1], 'x': click_was_in[0] }, player) self._redraw_all()
def move(self, event: tkinter.Event): width = self._canvas.winfo_width() height = self._canvas.winfo_height() click_point = point.from_pixel(event.x, event.y, width, height) row = 0 row_unit = 1 / (self._row) col = 0 col_unit = 1 / (self._col) for i in range(self._row): if click_point.frac()[1] > i * row_unit and click_point.frac( )[1] < (i + 1) * row_unit: row = i for j in range(self._col): if click_point.frac()[0] > j * col_unit and click_point.frac( )[0] < (j + 1) * col_unit: col = j if self._gamestate.move(row, col) == True: self.draw_circle(self._board) self._white_text.set('White:' + str(self._gamestate.wnumber)) self._black_text.set('Black:' + str(self._gamestate.bnumber)) self._turn_text.set('Turn:' + self._gamestate.turn) if self._gamestate.wnumber + self._gamestate.bnumber == self._row * self._col \ or self._gamestate.wnumber == 0 \ or self._gamestate.bnumber == 0: self._init_text.set('Winner is' + self._gamestate.winner(self._wincondition))
def _on_canvas_clicked(self, event: tkinter.Event) -> None: # When the canvas is clicked, tkinter generates an event. Since # we've bound to this method to that event, this method will be # called whenever the canvas is clicked. The event object passed # to this method will have two useful attributes: # # * event.x, which specifies the x-coordinate where the click # occurred # * event.y, which specifies the y-coordinate where the click # occurred # # tkinter is not aware of the concept of fractional coordinates. # It always returns pixel coordinates. But that's okay, # because we can simply create a Point object and let it # do the appropriate conversion for us. width = self._canvas.winfo_width() height = self._canvas.winfo_height() click_point = point.from_pixel( event.x, event.y, width, height) # Ask the SpotsState object to handle the click, by either # adding or removing a spot. self._state.handle_click(click_point) # Now that a spot has either been added or removed, redraw # the dots. self._redraw_all_spots()
def _on_canvas_clicked(self, event: tkinter.Event) -> None: # When the canvas is clicked, tkinter generates an event. Since # we've bound to this method to that event, this method will be # called whenever the canvas is clicked. The event object passed # to this method will have two useful attributes: # # * event.x, which specifies the x-coordinate where the click # occurred # * event.y, which specifies the y-coordinate where the click # occurred # # tkinter is not aware of the concept of fractional coordinates. # It always returns pixel coordinates. But that's okay, # because we can simply create a Point object and let it # do the appropriate conversion for us. width = self._canvas.winfo_width() height = self._canvas.winfo_height() click_point = point.from_pixel(event.x, event.y, width, height) # Ask the SpotsState object to handle the click, by either # adding or removing a spot. self._state.handle_click(click_point) # Now that a spot has either been added or removed, redraw # the dots. self._redraw_all_spots()
def _on_canvas_clicked(self, event: tkinter.Event) -> None: 'Makes the move depending on where the board was clicked' width = self._game_canvas.winfo_width() height = self._game_canvas.winfo_height() click_point = point.from_pixel(event.x, event.y, width, height) move = self.pixelToIndex(click_point) row, col = move moves = self._game.iter_flipped(self._count, row, col) if (len(moves) > 0): self._game.make_move(self._count, row, col) self._game.flip_pieces(moves, self._count) self.drawCircles() if self._game.game_over(): self.end_scenario() self._score.set('Black: {} White: {}'.format( self._game.getBlacks(), self._game.getWhites())) self._valid_text.set('') if self._game.has_move(self._count + 1): self.add() self._turn_text.set('Turn: {}'.format(self.turn())) self._score.set('Black: {} White: {}'.format( self._game.getBlacks(), self._game.getWhites())) self._valid_text.set('') else: if not self._game.won: self._valid_text.set('INVALID MOVE')
def _on_canvas_clicked(self, event: tkinter.Event) -> None: '''Handles clicks and redraws the canvas''' width = self._canvas.winfo_width() height = self._canvas.winfo_height() click_point = point.from_pixel( event.x, event.y, width, height) self.handle_click(click_point) if self._gamestate != None: self._gamestate.scoreboard() self._score.set('Black: ' + str(self._gamestate._blackscore) + ' White: ' + str(self._gamestate._whitescore)) if not self._gamestate.check_no_valid_moves_one_player(self._gamestate.get_turn()): self._gamestate.no_valid_moves_one_player(self._gamestate.get_turn()) if self._gamestate.gameover(): if self._gamestate.determine_winner() == 'B': self._game_text.set("WINNER: BLACK") elif self._gamestate.determine_winner() == 'W': self._game_text.set("WINNER: WHITE") elif self._gamestate.determine_winner() == 'NONE': self._game_text.set("NO WINNER") else: if self._gamestate.get_turn() == 1: self._game_text.set("Black's turn") elif self._gamestate.get_turn() == 2: self._game_text.set("White's turn") self._redraw()
def _on_canvas_clicked(self, event: tkinter.Event) -> None: ''' when the board click, it takes in the coordinates ''' width = self.canvas.winfo_width() height = self.canvas.winfo_height() click_point = point.from_pixel(event.x, event.y, width, height) self.board_setup(event)
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 _on_canvas_clicked(self, event: tkinter.Event) -> None: '''deal with click event''' width = self._canvas.winfo_width() height = self._canvas.winfo_height() click_point = point.from_pixel(event.x, event.y, width, height) self._state.handle_click(click_point) self._redraw_all_spots()
def _on_canvas_clicked(self, event): ''' manages click events ''' width = self._canvas.winfo_width() height = self._canvas.winfo_height() click_point = point.from_pixel( event.x, event.y, width, height) self._play(click_point)
def _mouse_location(self, event: tkinter.Event) -> 'tuple of row & column': '''returns a tuple of the location of the mouse click''' canvas_width = self._canvas.winfo_width() canvas_height = self._canvas.winfo_height() click_point = point.from_pixel(event.x, event.y, canvas_width, canvas_height) frac_x, frac_y = click_point.frac() for row in range(self._total_rows + 1): for column in range(self._total_columns + 1): if frac_x < column / self._total_columns and frac_y < row / self._total_rows: return [row - 1, column - 1]
def _on_canvas_clicked(self, event: tkinter.Event) -> None: '''generates a event.x and event.y coordinate to determine where the click occured and then calls the appropriate functions to redraw the board with the letter created by the click if the move is legal''' width, height = self._get_canvas_dimensions() click_point = point.from_pixel((event.x, event.y), (width, height)) self._handle_click(click_point) self._turn_text.set(self._turn_or_win()) self._draw_board()
def get_row_height(self) -> int: """ This function returns the proportionated height of an individual row. """ row_height = self._game_board_canvas.winfo_height( ) / self._user_input_dict["row"] row_height_frac = point.from_pixel( 0, row_height, self._game_board_canvas.winfo_width(), self._game_board_canvas.winfo_height()) row_height_pixel = row_height_frac.frac( )[1] * self._game_board_canvas.winfo_height() return row_height_pixel
def _on_tile_clicked(self, event: tk.Event) -> None: '''Makes a move, if possible, on the tile clicked''' click_point = point.from_pixel(event.x, event.y, self._boardGUI.width, self._boardGUI.height) for row in range(len(self._boardGUI._tiles)): for col in range(len(self._boardGUI._tiles[row])): if self._boardGUI._tiles[row][col].contains(click_point): self._board.make_move(row, col) self._boardGUI._draw_board() self.update_score() self.update_turn()
def get_column_width(self) -> int: """ This function returns the proportionated width of an individual column. """ column_width = self._game_board_canvas.winfo_width( ) / self._user_input_dict["column"] column_width_frac = point.from_pixel( column_width, 0, self._game_board_canvas.winfo_width(), self._game_board_canvas.winfo_height()) column_width_pixel = column_width_frac.frac( )[0] * self._game_board_canvas.winfo_width() return column_width_pixel
def _on_canvas_clicked(self, event: tk.Event): '''when the canvas is clicked it is determined whether that move is valid, and if so changes the board, updates the labels and switches turns, otherwise, it displays the winner''' if self._gamestate != None: width = self._canvas.winfo_width() height = self._canvas.winfo_height() list_of_cells = self._gamestate._board click_point = point.from_pixel(event.x, event.y, width, height) for cell in list_of_cells: if cell._frac_x1 < click_point._frac_x < cell._frac_x2 and cell._frac_y1 < click_point._frac_y < cell._frac_y2 and cell._status == None: if self._board_is_setup == False: cell._set_grid_cell_status(self._gamestate._turn) self._gamestate._remove_empty_spot(cell) self._draw_spot(cell) self._update_scoreboard() else: try: if self._gamestate._try_empty_spots_for_valid_move( ): self._gamestate.is_move_valid(cell) self._gamestate.execute_move(cell) self._gamestate._remove_empty_spot(cell) self._draw_all_spots() self._gamestate._opposite_turn() self._update_turn_label() self._update_scoreboard() if not self._gamestate._try_empty_spots_for_valid_move( ): self._gamestate._opposite_turn() self._update_turn_label() if self._gamestate.is_there_a_winner(): winner = self._gamestate.determine_winner() self._make_winner_label(winner) except othello_model.InvalidMoveError: continue
def _on_canvas_clicked(self, event: tkinter.Event) -> None: """When the canvas is clicked, check if there is a valid move""" width = self._canvas.winfo_width() height = self._canvas.winfo_height() click_point = point.from_pixel(event.x, event.y, width, height) move = self._model_state.handle_click(click_point) if move: try: self._game_state.move(move) self._redraw() except InvalidMoveError: self._error_dialog() self._winner_dialog()
def _on_canvas_clicked(self, event: tkinter.Event): if self._debug: self._debug_mode_clicked(event) return self._board_canvas.config(background='#00802b') self._update_canvas_size() click_location = point.from_pixel(event.x, event.y, self._canvas_width, self._canvas_height) previous_turn = self._state.turn click_was_in = None, None for y in range(self._state.board.ROWS): for x in range(self._state.board.COLS): if self._is_in_quadrant(self._board_point_index[y][x], click_location): click_was_in = y, x if None in click_was_in: return else: try: self._state.make_move({ 'x': click_was_in[0], 'y': click_was_in[1] }) except othello_logic.IllegalMoveError: return else: self._redraw_all() try: self._state.change_to_next_turn() except othello_logic.GameOver: if self._state.win == '>': win_state = 'Most' else: win_state = 'Least' game_over = GameOverPopup(self._decide_winner(), win_state) game_over.show() self._root_window.destroy() else: self._update_turn_label() if self._state.turn == previous_turn: warning = NoMovePopup(previous_turn) warning.show()
def _on_canvas_clicked(self, event:Event) -> None: '''generates a event.x and event.y coordinate to determine where the click occured and then calls the appropriate functions to redraw the board with the letter created by the click if the move is legal''' width, height = self._get_canvas_dimensions() click_point = point.from_pixel( (event.x, event.y), (width, height)) self._handle_click(click_point) self._game.count_disc() black = self._game.return_black_num() white = self._game.return_white_num() text3 = ('FULL | TURN: {} | B: {} W: {}'.format(self._game.whose_turn(),black, white)) Label(self._f1, text=text3).grid(row=0, column=0) self._draw_board()
def _clicked_cell(self, event: tkinter.Event): start_row = 1 start_col = 1 click_point = point.from_pixel(event.x, event.y, self.canvas_width, self.canvas_height) for row in range(1, self._othello_state.rows + 1): if start_row * self.point_row_spacing._frac_y < click_point._frac_y: start_row += 1 else: player_move_row = row break for column in range(1, self._othello_state.columns + 1): if start_col * self.point_col_spacing._frac_x < click_point._frac_x: start_col += 1 else: player_move_col = column break return (player_move_row, player_move_col)
def _click_move(self, event: tkinter.Event) -> None: '''Makes a move based on valid click location''' width = self._canvas.winfo_width() + 4 height = self._canvas.winfo_height() + 4 click_pix = point.from_pixel(event.x, event.y, width, height).pixel(width, height) click_x = click_pix[0] click_y = click_pix[1] for cols in range(self._column): for rows in range(self._row): x_up_left = (width / self._column ) * cols + 2 #+2 to make up for positioning x_down_right = (width / self._column) * (cols + 1) + 2 y_up_left = (height / self._row) * rows + 2 y_down_right = (height / self._row) * (rows + 1) + 2 if (click_x >= x_up_left) and (click_x <= x_down_right) and ( click_y >= y_up_left) and (click_y <= y_down_right): input_list = self._game.make_turn(rows, cols) self._execute_move(input_list)
def click_black(self, event: tkinter.Event) -> str: width = self._canvas.winfo_width() height = self._canvas.winfo_height() click_point = point.from_pixel(event.x, event.y, width, height) row = 0 row_unit = 1 / (self._row) col = 0 col_unit = 1 / (self._col) for i in range(self._row): if click_point.frac()[1] > i * row_unit and click_point.frac( )[1] < (i + 1) * row_unit: row = i for j in range(self._col): if click_point.frac()[0] > j * col_unit and click_point.frac( )[0] < (j + 1) * col_unit: col = j if self._board[row][col] == '.': self._board[row][col] = 'B' self.draw_circle(self._board)
def _on_button_down(self, event: tkinter.Event): ''' Event handler for left mouse button release event ''' if self._game_over == False: canvas = event.widget x = canvas.canvasx(event.x) y = canvas.canvasy(event.y) #print ((event.x, event.y), (x,y)) player_input = self._board.cell_location( point.from_pixel(x, y, self._board_width, self._board_height)) try: if self._othello._validateInput(player_input, self._next_player): self._othello.updateBoard(player_input, self._next_player) self._process_next_move() except InvalidMoveError: pass # nothing to do if invalid move
def _create_notes(self): # Input: {time: (pitch, duration)} # Output: [NoteSketch] OFFSET = .05 beats_per_measure, beat_value = self._time_signature measure_remaining_value = beats_per_measure*beat_value next_x_position = self._x1 for played_at, (pitch, duration) in self._raw_notes.items(): if duration > measure_remaining_value: raise TimeError() measure_remaining_value -= duration note_center_point = point.from_pixel(round(next_x_position + (OFFSET*self._measure_width)), self._pitch_y_coords[pitch], self._measure_width, self._measure_height) if(self._current_note_time == played_at): self._note_sketches.append(NoteSketch(note_center_point, (self._measure_width, self._measure_height), self._measure_height//8, duration, line_up = self._is_note_line_up(pitch), color = RED)) else: self._note_sketches.append(NoteSketch(note_center_point, (self._measure_width, self._measure_height), self._measure_height//8, duration, line_up = self._is_note_line_up(pitch))) used_space = (duration/beat_value/beats_per_measure)*self._measure_width next_x_position += used_space
def _on_canvas_clicked(self, event: tkinter.Event) -> None: 'When the canvas is clicked, handle the click' width = self._canvas.winfo_width() height = self._canvas.winfo_height() row = dialog._get_rows() col = dialog._get_columns() mode = dialog._get_mode() click_point = point.from_pixel(event.x, event.y, width, height) self._state.handle_click(click_point, width, height, row, col, mode) white_count = str(self._state._logic._white_counter) black_count = str(self._state._logic._black_counter) self._turn_text.set(f'Turn: {self._state._logic._turn}') self._state._logic.all_move_list() self._state._logic.viable_move_list() self._white_score.set(f'White: {white_count}') self._black_score.set(f'Black: {black_count}') self._redraw_all_spots_and_lines()
def _on_line_clicked(self, event: tk.Event) -> None: '''Determines whether or not a move has been made through clicks on the GUI''' click_point = point.from_pixel(event.x, event.y, self._boardGUI._canvas.winfo_width(), self._boardGUI._canvas.winfo_height()) for row in range(len(self._board.boxes())): for col in range(len(self._board.boxes()[row])): pairs = self._board.boxes()[row][col].return_possible_pairs() for pair in pairs: dot_0 = pair[0] dot_1 = pair[1] if dot.contains_point( click_point, self._boardGUI._dots[dot_0.row()][dot_0.col()], self._boardGUI._dots[dot_1.row()][dot_1.col()]): self._board.make_move(dot_0.row(), dot_0.col(), dot_1.row(), dot_1.col()) self._boardGUI._draw_board() self.update_score() self.update_turn() return
def _on_button_clicked(self, event: tkinter.Event) -> None: '''This method is called whenever there is a click on the game board. Basically this method is the key stone of the GUI. This method appropriately determines in what row and column the click was made and then interacts with the game logic, checking the validity of the move made as well as handling all the errors raised by the game logic including game over, invalid move, or no valid moves left''' canvas_width = self._canvas.winfo_width() canvas_height = self._canvas.winfo_height() click_point = point.from_pixel(event.x, event.y, canvas_width, canvas_height) x, y = click_point.frac() row_click = int(y * self._state._board_row) col_click = int(x * self._state._board_col) try: self._state.drop_piece(row_click + 1, col_click + 1) rand_num = random.randrange(3) if rand_num == 0: self._move.set('GOOD JOB') elif rand_num == 1: self._move.set('EXCELLENT MOVE') else: self._move.set('NICE WORK') except othello.InvalidOthelloGameMove: self._move.set('INVALID MOVE. NO POSSIBLE FLIPS') except othello.GameBoardLocationUnEmpty: self._move.set('INVALID MOVE. LOCATION UNEMPTY') self._draw_othello_board() self._player.set('PLAYER ' + self._state._player) self._black_tiles.set(' BLACK : \n' + \ str(self._state.count_tiles()[0])) self._white_tiles.set(' WHITE : \n' + \ str(self._state.count_tiles()[1])) try: self._state.check_all_board() except othello.OthelloGameOver: self._move.set('GAME OVER. NO VALID MOVES. PRESS QUIT') winner = self._state.winner_othello() if winner == 'W': self._player.set('CONGRATS. WINNER IS PLAYER WHITE') elif winner == 'B': self._player.set('CONGRATS. WINNER IS PLAYER BLACK') else: self._player.set('SORRY. NO ONE WON. GAME DRAW') except othello.NoValidMovesLeft: self._player.set('PLAYER ' + self._state._player) self._move.set('TURN REVERT. NO VALID MOVES FOR PLAYER ' \ + self._state._opposite_player())
import point