Advertisement
Guest User

helpmepls

a guest
May 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.24 KB | None | 0 0
  1. import sys
  2. from PyQt5 import QtWidgets
  3. from PyQt5 import QtCore
  4. from PyQt5 import QtGui
  5. from PyQt5.QtWidgets import *
  6. from PyQt5.QtGui import *
  7. from PyQt5.QtCore import *
  8. import time
  9. import random
  10. from guiLoop import guiLoop
  11. from functools import partial
  12.  
  13. class Settings():
  14.     SCENE_SIZE_X = 1200 #Settings for everything
  15.     SCENE_SIZE_Y = 900
  16.     GRID_COUNT_X = 4
  17.     GRID_COUNT_Y = 5
  18.     GRID_COUNT_TOTAL = GRID_COUNT_X * GRID_COUNT_Y
  19.     GRID_BOX_WIDTH = 100
  20.     GRID_BOX_HEIGHT = 100
  21.     DEF_FONT_SIZE = 15
  22.     DEF_PLAYER_SIZE = 25
  23.     DEF_LEFT_PADDING = 5
  24.     P1_TOP_PADDING = 10
  25.     P2_TOP_PADDING = DEF_PLAYER_SIZE * 2 + 10
  26.     ROLL_TIME = 20
  27.  
  28. class Drawing():
  29.     p1 = QPen(QColor(66, 134, 244), 1, Qt.SolidLine) #p1 blue, #4286f4
  30.     brushP1 = QBrush(QColor(66, 134, 244), Qt.SolidPattern)
  31.     p2 = QPen(QColor(244, 65, 65), 1, Qt.SolidLine) #p2 red, #f44141
  32.     brushP2 = QBrush(QColor(244, 65, 65), Qt.SolidPattern)
  33.     transparent = QPen(QColor(255, 255, 255), 1, Qt.SolidLine)
  34.     transparentB = QBrush(QColor(255, 255, 255), Qt.SolidPattern)
  35.  
  36. class Grid(QtWidgets.QGraphicsScene):
  37.  
  38.     def __init__(self):
  39.         super().__init__()
  40.        
  41.         self.scrolled = 0
  42.         self.ps = Players()
  43.         self.texts = []
  44.         self.initUI()
  45.  
  46.     def initUI(self):
  47.         self.draw_grid(Settings.GRID_COUNT_X, Settings.GRID_COUNT_Y, Settings.GRID_BOX_WIDTH, Settings.GRID_BOX_HEIGHT) #drawing grid first
  48.         self.draw_text(Settings.GRID_COUNT_X, Settings.GRID_COUNT_Y, Settings.GRID_BOX_WIDTH, Settings.GRID_BOX_HEIGHT, Settings.DEF_FONT_SIZE) #drawing text next
  49.         rect = self.ps.draw_players("P1", 0)
  50.         self.addRect(rect[0], Drawing.p1, Drawing.brushP1)
  51.         rect = self.ps.draw_players("P2", 0)
  52.         self.addRect(rect[1], Drawing.p2, Drawing.brushP2)
  53.  
  54.     def draw_grid(self, x_count, y_count, x_size, y_size):
  55.         width = x_count * x_size
  56.         height = y_count * y_size
  57.         self.setSceneRect(0, 0, width, height)
  58.         self.setItemIndexMethod(QtWidgets.QGraphicsScene.NoIndex)
  59.  
  60.         pen = QPen(QColor(0,0,0), 1, Qt.SolidLine)
  61.  
  62.         for x in range(0,x_count+1):    
  63.             for y in range(0,y_count+1): #nested for loop for x and y (0,0) - (0,1) - (0,2) etc
  64.                 xc = x * x_size
  65.                 yc = y * y_size
  66.                 self.addLine(xc,0,xc,height,pen)
  67.                 self.addLine(0,yc,width,yc,pen)
  68.  
  69.     def draw_text(self, x_count, y_count, x_size, y_size, font_size):
  70.         font = QFont("Courier New", font_size, QFont.Bold)
  71.        
  72.         total = Settings.GRID_COUNT_TOTAL
  73.         row_count = Settings.GRID_COUNT_Y
  74.         text_pos_ev = []
  75.         text_pos_od = []
  76.        
  77.         for y in range(0,y_count): #another nested for loop with out the +1 to y and x count
  78.             for x in range(0,x_count):
  79.                 xcTEXT = x * x_size
  80.                 ycTEXT = y * y_size
  81.                 if row_count % 2 == 0: #if the row is even
  82.                     text_pos_ev.append([xcTEXT, ycTEXT, total]) #add to even array
  83.                 else:
  84.                     text_pos_od.append([xcTEXT, ycTEXT, total]) #otherwise add to odd array
  85.                 total = total - 1                
  86.             row_count = row_count - 1
  87.  
  88.         for pos in text_pos_ev: #draws the even numbers
  89.             text = self.addText(str(pos[2]), font)
  90.             self.texts.append(text)
  91.             text.setPos(pos[0], pos[1])
  92.             text.setOpacity(0.6)
  93.            
  94.         new_text_pos_od = reverse_section(text_pos_od, 4) #flips the odd numbers so they snake around the board
  95.         for pos in new_text_pos_od: #draws new odd numbers
  96.             text = self.addText(str(pos[2]), font)
  97.             self.texts.append(text)
  98.             text.setPos(pos[0], pos[1])
  99.             text.setOpacity(0.6)
  100.  
  101.     def move_players(self, player, spaces):
  102.         rect = []
  103.         if(player == "P1"):
  104.             self.remove_player("P1")
  105.             rect = self.ps.draw_players("P1", spaces)
  106.             self.addRect(rect[0], Drawing.p1, Drawing.brushP1)
  107.         elif(player == "P2"):
  108.             self.remove_player("P2")
  109.             rect = self.ps.draw_players("P2", spaces)
  110.             self.addRect(rect[1], Drawing.p2, Drawing.brushP2)
  111.        
  112.     def remove_player(self, player):
  113.         rect = self.ps.clear_old_player(player)
  114.         self.addRect(rect, Drawing.transparent, Drawing.transparentB)
  115.         self.redraw_text()
  116.  
  117.     def redraw_text(self):
  118.         for text in self.texts:
  119.             self.removeItem(text)
  120.         del self.texts[:]
  121.         self.draw_text(Settings.GRID_COUNT_X, Settings.GRID_COUNT_Y, Settings.GRID_BOX_WIDTH, Settings.GRID_BOX_HEIGHT, Settings.DEF_FONT_SIZE)
  122.        
  123. class Players(QRectF):
  124.     def __init__(self):
  125.         super().__init__()
  126.  
  127.         self.poses = []
  128.         self.row_count = 0
  129.         self.p1 = QRectF()
  130.         self.p2 = QRectF()
  131.         self.p1pos = 0
  132.         self.p2pos = 0
  133.        
  134.         self.initUI()
  135.  
  136.     def initUI(self):
  137.         self.gen_pos_arr()
  138.  
  139.     def draw_players(self, player, spaces): #space 1 is box 1, 2 is box 2, 3 is box 3..      
  140.         if(player == "P1"):
  141.             self.p1pos += spaces;
  142.         elif(player == "P2"):
  143.             self.p2pos += spaces;
  144.        
  145.         self.p1 = QRectF(self.poses[self.p1pos][0] + Settings.DEF_LEFT_PADDING, self.poses[self.p1pos][1] + Settings.P1_TOP_PADDING, Settings.DEF_PLAYER_SIZE, Settings.DEF_PLAYER_SIZE)
  146.         self.p2 = QRectF(self.poses[self.p2pos][0] + Settings.DEF_LEFT_PADDING, self.poses[self.p2pos][1] + Settings.P2_TOP_PADDING, Settings.DEF_PLAYER_SIZE, Settings.DEF_PLAYER_SIZE)
  147.  
  148.         return [self.p1, self.p2]
  149.  
  150.     def clear_old_player(self, player):
  151.         pos = []
  152.         if(player == "P1"):
  153.             pos = self.poses[self.p1pos]
  154.             pos[0] += Settings.DEF_LEFT_PADDING
  155.             pos[1] += Settings.P1_TOP_PADDING
  156.         elif(player == "P2"):
  157.             pos = self.poses[self.p2pos]
  158.             pos[0] += Settings.DEF_LEFT_PADDING
  159.             pos[1] += Settings.P2_TOP_PADDING
  160.  
  161.         return QRectF(pos[0], pos[1], Settings.DEF_PLAYER_SIZE, Settings.DEF_PLAYER_SIZE)
  162.        
  163.  
  164.     def gen_pos_arr(self):
  165.         y = Settings.GRID_COUNT_Y-1
  166.         for i in range(1, Settings.GRID_COUNT_Y+1):
  167.             if(y % 2 == 0):
  168.                 for x in range(0, Settings.GRID_COUNT_X):
  169.                     self.poses.append([x*Settings.GRID_BOX_WIDTH,y*Settings.GRID_BOX_HEIGHT])
  170.             else:
  171.                 for x in range(Settings.GRID_COUNT_X-1, -1, -1):
  172.                     self.poses.append([x*Settings.GRID_BOX_WIDTH,y*Settings.GRID_BOX_HEIGHT])
  173.             y = y - 1
  174.  
  175. class App(QtWidgets.QDialog):
  176.  
  177.     def __init__(self):
  178.         super().__init__()
  179.        
  180.         self.images = ['1.png','2.png','3.png','4.png','5.png','6.png']
  181.         self.image = QLabel(self)
  182.         self.currTurn = "<font color='#4286f4'>Player 1's turn</font>"
  183.         self.turn = QLabel(self)
  184.         self.roll = QPushButton("Roll!")
  185.         self.move = QPushButton("Move player")
  186.         self.currPl = 1
  187.         self.vbox1 = QVBoxLayout()
  188.         self.vbox2 = QVBoxLayout()
  189.         self.grid = Grid()
  190.         self.initUI()
  191.  
  192.     def initUI(self):
  193.         self.setGeometry(300, 100, Settings.SCENE_SIZE_X, Settings.SCENE_SIZE_Y)
  194.         self.setWindowTitle("NEA Board Game")
  195.  
  196.         self.layout()
  197.  
  198.     @guiLoop
  199.     def move_players(self, players, spaces):
  200.         temp = 0
  201.  
  202.         print(players, spaces)
  203.        
  204.         self.move.setEnabled(False)
  205.            
  206.         self.turn.setText(self.currTurn)
  207.        
  208.         #self.grid.move_players(players, spaces)
  209.  
  210.         if(self.currPl == 1):
  211.             while(temp < spaces):
  212.                 self.grid.move_players("P1", 1)
  213.                 temp += 1
  214.                 yield 0.5
  215.         else:
  216.             while(temp < spaces):
  217.                 self.grid.move_players("P2", 1)
  218.                 temp += 1
  219.                 yield 0.5
  220.        
  221.         if(self.currPl == 1):
  222.             self.currPl = 2
  223.             self.currTurn = "<font color='#f44141'>Player 2's turn</font>"
  224.             self.turn.setText(self.currTurn)
  225.         elif(self.currPl == 2):
  226.             self.currPl = 1
  227.             self.currTurn = "<font color='#4286f4'>Player 1's turn</font>"
  228.             self.turn.setText(self.currTurn)
  229.  
  230.         self.roll.setEnabled(True)
  231.         spaces = 0
  232.            
  233.  
  234.  
  235.     def layout(self):
  236.         gridGview = QtWidgets.QGraphicsView(self.grid)
  237.  
  238.         d = self.dice()
  239.        
  240.         layout = QGridLayout(self)
  241.         layout.addWidget(d[1], 2, 1)
  242.         layout.addWidget(d[0], 1, 1)
  243.         layout.addWidget(gridGview, 0, 1)
  244.  
  245.     def dice(self):
  246.         rollBox = QGroupBox("")
  247.         font = QtGui.QFont("Courier New", 23, QtGui.QFont.Bold)
  248.        
  249.         self.roll.clicked.connect(self.diceRoll)
  250.        
  251.         self.turn.setText(self.currTurn)
  252.         self.turn.setFont(font)
  253.         self.turn.setAlignment(QtCore.Qt.AlignCenter)
  254.        
  255.         self.vbox1.addWidget(self.turn)
  256.         self.vbox1.addWidget(self.roll)
  257.        
  258.         rollBox.setLayout(self.vbox1)
  259.  
  260.         diceBox = QGroupBox("")
  261.         self.vbox2.addWidget(self.move)
  262.         self.move.setEnabled(False)
  263.         diceBox.setLayout(self.vbox2)
  264.        
  265.  
  266.         return [rollBox, diceBox]
  267.  
  268.     @guiLoop
  269.     def diceRoll(self, argument):
  270.         temp = 0
  271.         rollT = Settings.ROLL_TIME
  272.         self.roll.setEnabled(False)
  273.         while(temp <= rollT):
  274.             yield ((temp * 0.02) + 0.05)
  275.             img = random.choice(self.images)
  276.             pixmap = QPixmap(img)
  277.             self.image.setPixmap(pixmap)
  278.             self.vbox2.addWidget(self.image)
  279.             temp += 1
  280.         self.move.setEnabled(True)
  281.         a = lambda: self.move_players("P1" if self.currPl == 1 else "P2", int(img[0:1]))
  282.         self.move.clicked.connect(a)
  283.            
  284.        
  285. def reverse_section(arr, subsection):
  286.     ml2 = [ (arr[i*subsection:(i+1)*subsection]) for i in range(int(len(arr)/subsection)) ]
  287.     ml3 = [ sl[i][0:subsection-2] + [sl[len(sl)-i-1][2]] for sl in ml2 for i in range(len(sl)) ]
  288.     return ml3
  289.  
  290.  
  291. if __name__ == '__main__':
  292.     app = QtWidgets.QApplication(sys.argv)
  293.     app.setStyle("Fusion")
  294.     ex = App()
  295.     ex.show()
  296.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement