def _populate_grid_at_position(self, grid_position, mapper, layout, coordinate_pair): # creates a button and positions it on the grid x, y = coordinate_pair[0], coordinate_pair[1] textual_coordinates = "{0}-{1}".format(x, y) button = QCustomPushButton( self, grid_position, Coordinates.parse_coordinates(textual_coordinates, self._grid_size) ) Helpers.paint_grid_button(button, style.FIELD_BLUE) button.setObjectName("GridButton") button.setFixedSize(style.FIELD_ICON_SIZE + 10, style.FIELD_ICON_SIZE + 10) button.setIconSize(QSize(style.FIELD_ICON_SIZE, style.FIELD_ICON_SIZE)) # set the QSignalMapper's mapping to work with strings mapper.setMapping(button, textual_coordinates) # connecting the button's clicked signal to the QSignalMappers # mapped slot button.clicked.connect(mapper.map) # finally, add the button to the QGridLayout layout.addWidget(button, x, y)
def _mark_buttons(self, field_color): # marks the buttons with regards to the current chosen orientation x, y = self._coordinates.x, self._coordinates.y ship_dimensions = self._ship.ship_type.value left_grid = self._parent.ui.gridLayoutLeft if self._ship_orientation == ShipOrientation.horizontal: for i in xrange(y, (ship_dimensions + y)): target_button = self._parent.resolve_grid_button( left_grid, Coordinates(x, i) ) Helpers.paint_grid_button(target_button, field_color) elif self._ship_orientation == ShipOrientation.vertical: for i in xrange(x, (ship_dimensions + x)): target_button = self._parent.resolve_grid_button( left_grid, Coordinates(i, y) ) Helpers.paint_grid_button(target_button, field_color)
def _paint_player_grid(self, grid_layout, player, reveal=False): # marks the grid's squares according to their state grid = player.grid dimensions = grid.grid_size.value if player.player_type == PlayerType.human: reveal = True for i in xrange(0, dimensions): for j in xrange(0, dimensions): current_square = grid.squares.item((i, j)) owner = current_square.owner square_state = current_square.square_state button = self.resolve_grid_button(grid_layout, Coordinates(i, j)) if ( square_state == SquareState.vacant or square_state == SquareState.unoccupiable ): Helpers.paint_grid_button(button, style.FIELD_BLUE) elif square_state == SquareState.populated: if reveal: Helpers.paint_grid_button(button, style.FIELD_GRAY) else: Helpers.paint_grid_button(button, style.FIELD_BLUE) elif square_state == SquareState.hit: if owner is not None: if owner.ship_state == ShipState.damaged: Helpers.paint_grid_button(button, style.FIELD_RED) elif owner.ship_state == ShipState.sunk: Helpers.paint_grid_button(button, style.FIELD_BLACK) else: Helpers.paint_grid_button(button, style.FIELD_LIGHT_BLUE)