Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.53 KB | None | 0 0
  1. import sys
  2. from PyQt5.QtGui import QPainter, QColor, QFont, QPen, QBrush
  3. from PyQt5.QtCore import Qt
  4. from PyQt5.QtWidgets import QWidget, QApplication, QPushButton
  5.  
  6.  
  7. # This would be a good place to define some numeric constants for
  8. # sizes and positions.
  9.  
  10. cell_count = 8
  11. cell_size = 70
  12. grid_originx = 50
  13. grid_originy = 50
  14. greenPen = QColor(17, 87, 64)
  15. goldPen = QColor(185, 151, 91)
  16.  
  17.  
  18. class TribeSquares(QWidget):
  19.  
  20.     def __init__(self):
  21.         super().__init__()
  22.         #TODO add your implementation.
  23.         self.setGeometry(300, 300, 650, 800)
  24.         self.setWindowTitle("TribeSquares")
  25.         self.grid = [[0 for f in range(cell_count)] for j in range(cell_count)]
  26.         self.show()
  27.         self.player_value = False
  28.         self.current_scoreone = 0
  29.         self.current_scoretwo = 0
  30.         self.current_multiplierone = 1
  31.         self.current_multipliertwo = 1
  32.  
  33.     def paintEvent(self, event):
  34.         qp = QPainter()
  35.         blackPen = QPen(QBrush(Qt.black), 1)
  36.         qp.begin(self)
  37.         qp.fillRect(event.rect(), Qt.white)
  38.         for j in range(len(self.grid)):
  39.             for f in range(len(self.grid[j])):
  40.                 qp.drawRect(grid_originx + f * cell_size, grid_originy + j * cell_size, cell_size, cell_size)
  41.                 if self.grid[j][f] == 0:
  42.                     filler = Qt.white
  43.                 if 1 == self.grid[j][f]:
  44.                     filler = greenPen
  45.                 elif 2 == self.grid[j][f]:
  46.                     filler = goldPen
  47.                 qp.fillRect(grid_originx + f * cell_size + 12, grid_originy + j * cell_size + 12, cell_size - 20, cell_size - 20, filler)
  48.  
  49.         if not self.player_value:
  50.             qp.setFont(QFont("Courier", 100, 100))
  51.             qp.setPen(greenPen)
  52.             qp.drawText(350, 690, "Player One's Turn")
  53.  
  54.         else:
  55.             qp.setFont(QFont("Courier", 100, 100))
  56.             qp.setPen(goldPen)
  57.             qp.drawText(350, 730, "Player Two's Turn")
  58.  
  59.         for i in range (1,8):
  60.             for row in range(cell_count):
  61.                 for column in range(cell_count):
  62.                     if j + 1 <= 7 and f + i <= 7:
  63.                         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]):
  64.                             if 0 == self.grid[j][f]:
  65.                                 qp.setPen(greenPen)
  66.                                 qp.drawRect(grid_originx + 35 + column * 70, grid_originy + 35 + row * 70, i * 70, i * 70)
  67.                         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]):
  68.                             if 1 == self.grid[j][f]:
  69.                                 qp.setPen(goldPen)
  70.                                 qp.drawRect(grid_originx + 35 + column * 70, grid_originy + 35 + row * 70, i * 70, i * 70)
  71.         qp.setPen(blackPen)
  72.         qp.setFont(QFont("Courier", 100, 100))
  73.         qp.drawText(70, 650, "Score")
  74.         qp.drawText(175, 650, "Multiplier")
  75.         qp.drawText(440, 650, "Turn")
  76.         qp.setPen(greenPen)
  77.         qp.drawText(100, 690, str(self.current_scoreone))
  78.         qp.drawText(230, 690, "x" + str(self.current_multiplierone))
  79.         qp.setPen(goldPen)
  80.         qp.drawText(100, 730, str(self.current_scoretwo))
  81.         qp.drawText(230, 730, "x" + str(self.current_multipliertwo))
  82.         qp.setPen(blackPen)
  83.  
  84.  
  85.  
  86.  
  87.  
  88.         qp.end()
  89.         # TODO We suggest the following private method, which takes the indices of
  90.         # a player's move and updates that player's score object so that it is
  91.         # displayed correctly on the next update. THis method is not required, and if
  92.         # you choose to use it, you are free to change its parameters.
  93.  
  94.  
  95.     def mousePressEvent(self, event):
  96.         row = (event.y() - grid_originy) // cell_size
  97.         col = (event.x() - grid_originx) // cell_size
  98.  
  99.         if 0 <= row < cell_count and 0 <= col < cell_count:
  100.             if self.grid[row][col] == 0:
  101.                 self.grid[row][col] = self.player_value + 1
  102.                 if not self.player_value:
  103.                     squarecheck = self.count_newsquare(1, row, col)
  104.                     new_score = squarecheck[0]
  105.                     new_multiplier = squarecheck[1]
  106.                     self.current_scoreone += new_score
  107.                     self.current_multiplierone = new_multiplier
  108.                 else:
  109.                     squarecheck = self.count_newsquare(2, row, col)
  110.                     new_score = squarecheck[0]
  111.                     new_multiplier = squarecheck[1]
  112.                     self.current_scoretwo += new_score
  113.                     self.current_multipliertwo = new_multiplier
  114.  
  115.                 self.player_value = not self.player_value
  116.  
  117.         self.update()
  118.  
  119.  
  120.     def count_newsquare(self, check, row, col):
  121.         area_sum = 0
  122.         multiplier = 0
  123.         for row2 in range(cell_count):
  124.             for col2 in range(cell_count):
  125.                 if not row2 == row and not col2 == col and self.grid[row2][col2] == check:
  126.                     if self.grid[row][col2] == check and self.grid[row2][col] == check:
  127.                         area_sum += (abs(col2 - col)+1) * (abs(row2 - row) + 1)
  128.                         multiplier += 1
  129.         return area_sum * multiplier, multiplier
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. if __name__ == '__main__':
  137.     app = QApplication(sys.argv)
  138.     ex = TribeSquares()
  139.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement