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, QPushButton
- # 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
- 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()
- self.player_value = False
- self.current_scoreone = 0
- self.current_scoretwo = 0
- self.current_multiplierone = 1
- self.current_multipliertwo = 1
- def paintEvent(self, event):
- qp = QPainter()
- blackPen = QPen(QBrush(Qt.black), 1)
- qp.begin(self)
- qp.fillRect(event.rect(), Qt.white)
- 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 1 == self.grid[j][f]:
- filler = greenPen
- elif 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 not self.player_value:
- qp.setFont(QFont("Courier", 100, 100))
- qp.setPen(greenPen)
- qp.drawText(350, 690, "Player One's Turn")
- else:
- qp.setFont(QFont("Courier", 100, 100))
- qp.setPen(goldPen)
- qp.drawText(350, 730, "Player Two's Turn")
- for i in range (1,8):
- for row in range(cell_count):
- for column in range(cell_count):
- if j + 1 <= 7 and f + i <= 7:
- if (self.grid[row][column]) == (self.grid[row][column + 1]) == 0 and (self.grid[row + 1][column]) == 0 and (self.grid[row + i][column + i]):
- if 0 == self.grid[j][f]:
- qp.setPen(greenPen)
- qp.drawRect(grid_originx + 35 + column * 70, grid_originy + 35 + row * 70, i * 70, i * 70)
- elif (self.grid[row][column]) == (self.grid[row][column + 1]) == 1 and (self.grid[row + 1][column]) == 1 and (self.grid[row + i][column + i]):
- if 1 == self.grid[j][f]:
- qp.setPen(goldPen)
- qp.drawRect(grid_originx + 35 + column * 70, grid_originy + 35 + row * 70, i * 70, i * 70)
- qp.setPen(blackPen)
- qp.setFont(QFont("Courier", 100, 100))
- qp.drawText(70, 650, "Score")
- qp.drawText(175, 650, "Multiplier")
- qp.drawText(440, 650, "Turn")
- qp.setPen(greenPen)
- qp.drawText(100, 690, str(self.current_scoreone))
- qp.drawText(230, 690, "x" + str(self.current_multiplierone))
- qp.setPen(goldPen)
- qp.drawText(100, 730, str(self.current_scoretwo))
- qp.drawText(230, 730, "x" + str(self.current_multipliertwo))
- qp.setPen(blackPen)
- 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 mousePressEvent(self, event):
- row = (event.y() - grid_originy) // cell_size
- col = (event.x() - grid_originx) // cell_size
- if 0 <= row < cell_count and 0 <= col < cell_count:
- if self.grid[row][col] == 0:
- self.grid[row][col] = self.player_value + 1
- if not self.player_value:
- squarecheck = self.count_newsquare(1, row, col)
- new_score = squarecheck[0]
- new_multiplier = squarecheck[1]
- self.current_scoreone += new_score
- self.current_multiplierone = new_multiplier
- else:
- squarecheck = self.count_newsquare(2, row, col)
- new_score = squarecheck[0]
- new_multiplier = squarecheck[1]
- self.current_scoretwo += new_score
- self.current_multipliertwo = new_multiplier
- self.player_value = not self.player_value
- self.update()
- def count_newsquare(self, check, row, col):
- area_sum = 0
- multiplier = 0
- for row2 in range(cell_count):
- for col2 in range(cell_count):
- if not row2 == row and not col2 == col and self.grid[row2][col2] == check:
- if self.grid[row][col2] == check and self.grid[row2][col] == check:
- area_sum += (abs(col2 - col)+1) * (abs(row2 - row) + 1)
- multiplier += 1
- return area_sum * multiplier, multiplier
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- ex = TribeSquares()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement