Create Board Game-like Grid in Python

后端 未结 2 914
小鲜肉
小鲜肉 2020-12-30 17:18

I am thinking of creating a board game in Python, one which will have a grid of spaces, each with different properties, and which may or may not have pieces resting on them.

2条回答
  •  庸人自扰
    2020-12-30 17:53

    You can consider your underlying board implementation as different kind of datastructures.

    1. List of lists - l1 = [[1,2,3],[4,5,6],[7,8,9]]
    2. Dict with values as list = d1 = {a:[1,2,3],b:[4,5,6],c:[7,8,9]}
    3. Dict with keys are coordinates and values which you can assign.
    4. As a Graph

    Here is a design of an empty chessboard.

    >>> chessboard = {}
    >>> for row in range(8):
    ...     for col in range(8):
    ...             chessboard[(row,col)] = 0
    ... 
    >>>
    

    You can use any of these and design the logic of your game. For higher level elements you can tie these objects or elements within to spites of pygame

提交回复
热议问题