Fix checkerboard with bounding box and positional fixes.

This commit is contained in:
Dorian 2014-09-25 08:54:18 -04:00
parent 7fecb40f7d
commit 185b226f3f
1 changed files with 72 additions and 50 deletions

View File

@ -23,6 +23,7 @@ from PySide import QtGui
from PySide import QtCore from PySide import QtCore
from justcheckers.game import board from justcheckers.game import board
from justcheckers.game.rules import Rules
Coordinates = namedtuple('Coordinates', ['x', 'y']) Coordinates = namedtuple('Coordinates', ['x', 'y'])
@ -41,41 +42,13 @@ class BoardSquare(QtGui.QGraphicsItem):
:return: A graphical item representing a specific square on a checkerboard. :return: A graphical item representing a specific square on a checkerboard.
""" """
super(BoardSquare, self).__init__(parent) super(BoardSquare, self).__init__(parent)
self.color = QtGui.QColor(QtCore.Qt.blue)
self.pixmap = None
# self.dragOver = False
# TODO Add validation that coordinates match a real active game. # TODO Add validation that coordinates match a real active game.
self.coordinates = coordinates self.coordinates = coordinates
self.game_board = game_board self.game_board = game_board
# self.setAcceptDrops(True)
# def dragEnterEvent(self, event):
# if event.mimeData().hasColor() or \
# (isinstance(self, RobotHead) and event.mimeData().hasImage()):
# event.setAccepted(True)
# self.dragOver = True
# self.update()
# else:
# event.setAccepted(False)
#
# def dragLeaveEvent(self, event):
# self.dragOver = False
# self.update()
#
# def dropEvent(self, event):
# self.dragOver = False
# if event.mimeData().hasColor():
# self.color = QtGui.QColor(event.mimeData().colorData())
# elif event.mimeData().hasImage():
# self.pixmap = QtGui.QPixmap(event.mimeData().imageData())
#
# self.update()
def boundingRect(self): def boundingRect(self):
# return QtCore.QRectF(-15, -50, 30, 50) return QtCore.QRectF(-15, -50, 30, 50)
return QtCore.QRectF()
def paint(self, painter, option, widget=None): def paint(self, painter, option, widget=None):
@ -84,9 +57,8 @@ class BoardSquare(QtGui.QGraphicsItem):
if self.game_board.is_illegal_space(self.coordinates.x, self.coordinates.y): if self.game_board.is_illegal_space(self.coordinates.x, self.coordinates.y):
self.color = QtGui.QColor(QtCore.Qt.white) self.color = QtGui.QColor(QtCore.Qt.white)
painter.setPen(QtGui.QPen(QtCore.Qt.darkYellow, 2))
painter.setBrush(self.color) painter.setBrush(self.color)
painter.drawRoundedRect(0, 0, 50, 50, 25, 25, QtCore.Qt.RelativeSize) painter.drawRect(0, 0, 50, 50)
# Draw the underlying piece. # Draw the underlying piece.
if (self.game_board.is_empty(self.coordinates.x, self.coordinates.y) if (self.game_board.is_empty(self.coordinates.x, self.coordinates.y)
@ -98,41 +70,80 @@ class BoardSquare(QtGui.QGraphicsItem):
if self.game_board.is_light(self.coordinates.x, self.coordinates.y): if self.game_board.is_light(self.coordinates.x, self.coordinates.y):
self.token_colour = QtGui.QColor(QtCore.Qt.white) self.token_colour = QtGui.QColor(QtCore.Qt.white)
painter.setPen(QtGui.QPen(QtCore.Qt.darkYellow, 2))
painter.setBrush(self.token_colour) painter.setBrush(self.token_colour)
painter.drawEllipse(5, 5, 40, 40) painter.drawEllipse(5, 5, 40, 40)
if self.game_board.is_king(self.coordinates.x, self.coordinates.y): if self.game_board.is_king(self.coordinates.x, self.coordinates.y):
painter.drawText(15, 15, 'King') painter.drawText(15, 15, 'King')
class BoardBackground(QtGui.QGraphicsItem):
"""A graphical unit for the background of the checkerboard."""
def __init__(self, board_size=Rules.STANDARD_BOARD_SIZE, parent=None):
"""
Create a board square.
:param board_size: The size of the board in terms of rows or columns.
:param parent: The parent widget for this graphical item.
:return: A graphical item representing the background of a checkerboard.
"""
super(BoardBackground, self).__init__(parent)
self.board_size = board_size
def boundingRect(self):
return QtCore.QRectF(-15, -50, 30, 50)
def paint(self, painter, option, widget=None):
pixel_dimensions = 55 * self.board_size
painter.setPen(QtGui.QPen(QtCore.Qt.black, 2))
# FFC200
painter.setBrush(QtGui.QColor(255, 194, 0))
painter.drawRoundedRect(
-1 * pixel_dimensions,
-1 * pixel_dimensions,
pixel_dimensions + 10,
pixel_dimensions + 10,
10, 10, QtCore.Qt.RelativeSize)
class GameBoardWidget(QtGui.QGraphicsView): class GameBoardWidget(QtGui.QGraphicsView):
mouse_move = QtCore.Signal(int, int)
def __init__(self, scene): def __init__(self, scene):
super(GameBoardWidget, self).__init__(scene) super(GameBoardWidget, self).__init__(scene)
self.scene = scene self.scene = scene
def create_checker_board(self): def create_checker_board(self):
# TODO Cleaner implementation of passing in game board. # TODO Cleaner implementation of passing in game board.
game_board = board.Board() game_board = board.Board()
game_board.setup_new_game() game_board.setup_new_game()
redness = QtGui.QBrush(color=QtCore.Qt.darkRed) board_background = BoardBackground()
redness_pen = QtGui.QPen(color=QtCore.Qt.darkRed) self.scene.addItem(board_background)
# TODO Create a nicer board behind the "floating" squares for row in xrange(8):
self.scene.addRect(-5, -5, 55 * 8 + 5, 55 * 8 + 5, pen=redness_pen, brush=redness) for column in xrange(8):
board_coords = Coordinates(x=column, y=row)
for x in xrange(8):
for y in xrange(8):
board_coords = Coordinates(x=x, y=y)
current_coords = Coordinates(x=55 * board_coords.x, y=55 * board_coords.y) current_coords = Coordinates(x=55 * board_coords.x, y=55 * board_coords.y)
item = BoardSquare(board_coords, game_board)
item.setPos(current_coords.x, current_coords.y)
self.scene.addItem(item)
print("{}x{} = board, {}, {} = real".format(board_coords.x, board_coords.y, current_coords.x, current_coords.y))
item = BoardSquare(board_coords, game_board, parent=board_background)
item.setPos(-1 * current_coords.x, -1 * current_coords.y)
# TODO Add in number of captured pieces and whose turn it is. # TODO Add in number of captured pieces and whose turn it is.
def mouseMoveEvent(self, event):
# msg = '({}, {})'.format(event.x(), event.y())
# self.mouse_move.emit(msg)
self.mouse_move.emit(event.x(), event.y())
def mousePressEvent(self, event):
self.mouse_move.emit(event.x(), event.y())
class GameView(QtGui.QWidget): class GameView(QtGui.QWidget):
"""Game viewer for the game's license, etc.""" """Game viewer for the game's license, etc."""
@ -141,24 +152,28 @@ class GameView(QtGui.QWidget):
def __init__(self): def __init__(self):
super(GameView, self).__init__() super(GameView, self).__init__()
self.spaces_moved = 0
self.setup_components() self.setup_components()
def setup_components(self): def setup_components(self):
self.game_board_scene = QtGui.QGraphicsScene() self.game_board_scene = QtGui.QGraphicsScene()
self.game_board = GameBoardWidget(self.game_board_scene) self.game_board_widget = GameBoardWidget(self.game_board_scene)
self.game_board_widget.mouse_move.connect(self.update_coordinates)
self.game_board.create_checker_board()
self.game_board.show()
self.game_board_widget.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(QtCore.Qt.green)))
self.game_board_widget.create_checker_board()
exit_button = QtGui.QPushButton('Back to Menu', self) exit_button = QtGui.QPushButton('Back to Menu', self)
exit_button.setFixedHeight(50) exit_button.setFixedHeight(50)
exit_button.clicked.connect(self.switch_to_menu_view) exit_button.clicked.connect(self.switch_to_menu_view)
widget_layout = QtGui.QVBoxLayout(self) widget_layout = QtGui.QVBoxLayout(self)
widget_layout.addWidget(self.game_board) widget_layout.addWidget(self.game_board_widget)
self.coordinate_label = QtGui.QLabel('Coords: ')
button_row = QtGui.QHBoxLayout(self) button_row = QtGui.QHBoxLayout(self)
button_row.addWidget(self.coordinate_label)
button_row.addWidget(exit_button) button_row.addWidget(exit_button)
widget_layout.addLayout(button_row) widget_layout.addLayout(button_row)
@ -167,3 +182,10 @@ class GameView(QtGui.QWidget):
def switch_to_menu_view(self): def switch_to_menu_view(self):
self.parentWidget().setCurrentIndex(0) self.parentWidget().setCurrentIndex(0)
@QtCore.Slot(int, int)
def update_coordinates(self, x, y):
self.spaces_moved += 1
text = 'Coords: {} {} - Moved: {}'.format(x, y, self.spaces_moved)
self.coordinate_label.setText(text)
self.update()