Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 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
  5. #from Score import Score
  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. player_value = True
  15.  
  16. greenPen = QColor(17, 87, 64)
  17. goldPen = QColor(185, 151, 91)
  18.  
  19. class TribeSquares(QWidget):
  20.  
  21.     def __init__(self):
  22.         super().__init__()
  23.         #TODO add your implementation.
  24.         self.setGeometry(300, 300, 650, 800)
  25.         self.setWindowTitle("TribeSquares")
  26.         self.grid = [[0 for f in range(cell_count)] for j in range(cell_count)]
  27.         self.show()
  28.  
  29.  
  30.     def paintEvent(self, event):
  31.         # TODO draw the playing field, including all moves so far and resulting
  32.         #  squares. Also draw score information for both players and display
  33.         # whose turn it is.
  34.         qp = QPainter()
  35.         blackPen = QPen(QBrush(Qt.black), 1)
  36.         qp.begin(self)
  37.         qp.fillRect(event.rect(), Qt.white)
  38.         qp.setPen(blackPen)
  39.         for j in range(len(self.grid)):
  40.             for f in range(len(self.grid[j])):
  41.                 qp.drawRect(grid_originx + f * cell_size, grid_originy + j * cell_size, cell_size, cell_size)
  42.                 if self.grid[j][f] == 0:
  43.                     filler = Qt.white
  44.                 if player_value:
  45.                     if 1 == self.grid[j][f]:
  46.                         filler = greenPen
  47.                 if not player_value:
  48.                     if 2 == self.grid[j][f]:
  49.                         filler = goldPen
  50.                 qp.fillRect(grid_originx + f * cell_size + 12, grid_originy + j * cell_size + 12, cell_size - 20, cell_size - 20, filler)
  51.         if player_value:
  52.             QFont("Courier", 15)
  53.             qp.setPen(greenPen)
  54.             qp.drawText(300, 750, "Player One's Turn")
  55.         if not player_value:
  56.             qp.setPen(goldPen)
  57.             qp.drawText(80, 80, "Player Two's Turn")
  58.         qp.end()
  59.         # TODO We suggest the following private method, which takes the indices of
  60.         # a player's move and updates that player's score object so that it is
  61.         # displayed correctly on the next update. THis method is not required, and if
  62.         # you choose to use it, you are free to change its parameters.
  63.     def __move(self, x, y):
  64.         pass
  65.  
  66.     def mousePressEvent(self, event):
  67.         # TODO replace pass with your implementation
  68.         # Here, the player clicked on the window. If she clicked on a valid
  69.         # space, process her score changes, if any. Be sure to update the atr
  70.         # needed to draw the resulting game board.
  71.         row = (event.y() - grid_originy) // cell_size
  72.         col = (event.x() - grid_originx) // cell_size
  73.         if 0 <= row < cell_count and 0 <= col < cell_count:
  74.             while player_value:
  75.                 if self.grid[row][col] == 0:
  76.                     self.grid[row][col] = 1
  77.                 player_value = not player_value
  78.  
  79.             while not player_value:
  80.                 if self.grid[row][col] == 0:
  81.                     self.grid[row][col] = 2
  82.                 player_value = not player_value
  83.         self.update()
  84.  
  85.  
  86.  
  87.  
  88. if __name__ == '__main__':
  89.     app = QApplication(sys.argv)
  90.     ex = TribeSquares()
  91.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement