Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from PyQt5.QtGui import QPainter, QColor, QFont, QPen, QBrush
- from PyQt5.QtCore import Qt
- from PyQt5.QtWidgets import QWidget, QApplication
- #from Score import Score
- # This would be a good place to define some numeric constants for
- # sizes and positions.
- cell_count = 8
- cell_size = 70
- grid_originx = 50
- grid_originy = 50
- player_value = True
- greenPen = QColor(17, 87, 64)
- goldPen = QColor(185, 151, 91)
- class TribeSquares(QWidget):
- def __init__(self):
- super().__init__()
- #TODO add your implementation.
- self.setGeometry(300, 300, 650, 800)
- self.setWindowTitle("TribeSquares")
- self.grid = [[0 for f in range(cell_count)] for j in range(cell_count)]
- self.show()
- def paintEvent(self, event):
- # TODO draw the playing field, including all moves so far and resulting
- # squares. Also draw score information for both players and display
- # whose turn it is.
- qp = QPainter()
- blackPen = QPen(QBrush(Qt.black), 1)
- qp.begin(self)
- qp.fillRect(event.rect(), Qt.white)
- qp.setPen(blackPen)
- for j in range(len(self.grid)):
- for f in range(len(self.grid[j])):
- qp.drawRect(grid_originx + f * cell_size, grid_originy + j * cell_size, cell_size, cell_size)
- if self.grid[j][f] == 0:
- filler = Qt.white
- if player_value:
- if 1 == self.grid[j][f]:
- filler = greenPen
- if not player_value:
- if 2 == self.grid[j][f]:
- filler = goldPen
- qp.fillRect(grid_originx + f * cell_size + 12, grid_originy + j * cell_size + 12, cell_size - 20, cell_size - 20, filler)
- if player_value:
- QFont("Courier", 15)
- qp.setPen(greenPen)
- qp.drawText(300, 750, "Player One's Turn")
- if not player_value:
- qp.setPen(goldPen)
- qp.drawText(80, 80, "Player Two's Turn")
- qp.end()
- # TODO We suggest the following private method, which takes the indices of
- # a player's move and updates that player's score object so that it is
- # displayed correctly on the next update. THis method is not required, and if
- # you choose to use it, you are free to change its parameters.
- def __move(self, x, y):
- pass
- def mousePressEvent(self, event):
- # TODO replace pass with your implementation
- # Here, the player clicked on the window. If she clicked on a valid
- # space, process her score changes, if any. Be sure to update the atr
- # needed to draw the resulting game board.
- row = (event.y() - grid_originy) // cell_size
- col = (event.x() - grid_originx) // cell_size
- if 0 <= row < cell_count and 0 <= col < cell_count:
- while player_value:
- if self.grid[row][col] == 0:
- self.grid[row][col] = 1
- player_value = not player_value
- while not player_value:
- if self.grid[row][col] == 0:
- self.grid[row][col] = 2
- player_value = not player_value
- self.update()
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- ex = TribeSquares()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement