Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

General design help



  • Hi all,

    I'm currently coding a Monopoly game using PyQt5 and I've it a bit of a bump in my progression.
    I have a board ready to go and I can't figure out how i'm going to handle the tokens meaning:

    • Keep the look of my board (QLabels with borders)
    • Access the position of the tokens
    • Access the property at the position of the tokens

    And i think that's it? I tried putting a gridlayout for every tile but since I can't draw borders around the grid layout that doesn't really work. I thought about putting the name of the players in the Qlabels since i'm able to get the text inside the tiles but that's not ideal.

    Any idea?

    Here is my code for reference (I'm just putting the relevant portion)

    class Monopoly(QWidget):
       """ actual game, display the board and everything """
       def __init__(self, players):
          super().__init__()
    
          self.main_layout = QVBoxLayout()
          self.turn_layout = QHBoxLayout()
          self.board_layout = QGridLayout()
          self.player_info_layout = QGridLayout()
          self.buttons_layout = QHBoxLayout()
    
          self.board_layout.setSpacing(0)
    
          if len(players) > 1:
             ordered_players = self.order_players(players)
          else:
             ordered_players = players
    
          turn_label = QLabel(f"Turn of {ordered_players[0]}")
          turn_label.setFont(QtGui.QFont("Comic Sans MS", 20, QtGui.QFont.Bold))
    
          self.board = [
             "Free Parking", "Strand", "Fleet Street", "Chance", "Trafalgar Square",
             "Fenchurch Street station", "Leicester Square", "Coventry Street", "Water Works", "Piccadilly", "Go to Jail",
             "Vine Street", "", "", "", "", "", "", "", "", "", "Regent Street",
             "Marlborough Street", "", "", "", "", "", "", "", "", "", "Oxford Street",
             "Community Chest", "", "", "", "", "", "", "", "", "", "Community Chest",
             "Bow Street", "", "", "", "", "", "", "", "", "", "Bond Street",
             "Marylebine station", "", "", "", "", "", "", "", "", "", "Liverpool Street station",
             "Northumberland Avenue", "", "", "", "", "", "", "", "", "", "Chance",
             "Whitehall", "", "", "", "", "", "", "", "", "", "Park Lane",
             "Electric Company", "", "", "", "", "", "", "", "", "", "Super Tax",
             "Pall Mall", "", "", "", "", "", "", "", "", "", "Mayfair",
             "Visit Jail", "Pentonville Road", "Euston Road", "Chance", "The Angel Islington", "King's Cross station",
             "Income Tax", "Whitechapel Road", "Community Chest", "Old Kent Road", "Start"
          ]
    
          self.board_positions = {
             0: 20, 1: 21, 2: 22, 3: 23, 4: 24,
             5: 25, 6: 26, 7: 27, 8: 28, 9: 29,
             10: 30, 11: 19, 12: 31, 13: 18, 14: 32,
             15: 17, 16: 33, 17: 16, 18: 34, 19: 15,
             20: 35, 21: 14, 22: 36, 23: 13, 24: 37,
             25: 12, 26: 38, 27: 11, 28: 39, 29: 10,
             30: 9, 31: 8, 32: 7, 33: 6, 34: 5,
             35: 4, 36: 3, 37: 2, 38: 1, 39: 0
          }
    
          positions = [(i, j) for i in range(11) for j in range(11)]
    
          for position, name in zip(positions, self.board):
             if name == "":
                continue
             
             label = QLabel(name)
             label.setAlignment(Qt.AlignVCenter)
             label.setMargin(20)
             label.setStyleSheet("border: 1px solid black")
             self.board_layout.addWidget(label, *position)
    
          balance_info = QLabel("Money left:")
          balance = QLabel(f"{ordered_players[0].get_balance()}")
          possessions_info = QLabel("You have these properties:")
          possessions = QLabel(f"{ordered_players[0].get_possessions()}")
          roll_button = QPushButton("Roll")
    
          self.turn_layout.addWidget(turn_label)
          self.turn_layout.setAlignment(Qt.AlignCenter)
          self.buttons_layout.addWidget(roll_button)
          self.player_info_layout.addWidget(balance_info, 1, 0, Qt.AlignCenter)
          self.player_info_layout.addWidget(balance, 2, 0, Qt.AlignCenter)
          self.player_info_layout.addWidget(possessions_info, 1, 2, Qt.AlignCenter)
          self.player_info_layout.addWidget(possessions, 2, 2, Qt.AlignCenter)
    
          self.main_layout.addLayout(self.turn_layout)
          self.main_layout.addLayout(self.board_layout)
          self.main_layout.addLayout(self.player_info_layout)
          self.main_layout.addLayout(self.buttons_layout)
    
          self.setLayout(self.main_layout)
    
          self.get_tile()
    
       def order_players(self, players):
          """ roll dices for players to order them """
    
          rolls = []
    
          for player in players:
             rolls.append(sum(mp_console.roll()))
    
          rolls_players = sorted(set(zip(rolls, players)), key=lambda x: -x[0])
          ordered_players = [player[1] for player in rolls_players]
    
          message_box = QMessageBox()
          message = "Order of players:\n"
    
          for roll, player in rolls_players:
             message += f"- {player} rolled {roll}\n"
    
          message_box.setText(message)
          message_box.exec_()
    
          return ordered_players
    
       def get_tile(self):
          for i in range(0, 40):
             property_name = self.board_layout.itemAt(i).widget().text()
             # property_name = self.board_layout.itemAt(i).itemAt(0).widget().text()
             true_position = self.board_positions[i]
             print(f"Property: {property_name} {true_position}")
    

    monopoly_board.png


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Not that I want to discourage you but I would have rather used the Graphics View framework. It seems more suited to the needs of your game with regard to custom tiles and moving things around with animation.



  • @SGaist Looking at it, it does look more suited. I think it has all the stuff i need (child, pos methods). However, i don't think there is layouts for them? How hard you reckon it would be to recreate the board?



  • @Padgaus Found the layouts, thanks for your help, i'll try some stuff out!


Log in to reply