Note that there are some explanatory texts on larger screens.

plurals
  1. PO2d lists replacing objects
    primarykey
    data
    text
    <p>I'm trying to make a board game with python 3 using the pygame library. What I'm trying to do is create an x by y 2d list of objects which I've termed spaces to represent the board. First I initialize all of the spaces with the color as gray and the is_piece attribute set to False, indicating that they are all empty spaces. Then I want to replace the empty spaces with pieces by replacing the board[x][y] values with objects that have the is_piece attribute set to true.</p> <p>The bug that I'm having is that the self.coords values are getting flipped. For example, in the below code, the stone object with the [2, 3] self.coords value is ending up in the board[3][2] position, and visa versa. Adding more stones also screws up the self.coords by flipping the index values and sometimes subsequently adding one to one of them.</p> <pre><code>def initialize_board(): #creates an array of empty spaces representing the board board = [[Space(GRAY, RADIUS, [x, y], False) for x in range(BOARDWIDTH)] for y in range(BOARDHEIGHT) #Create the center stones board[3][3] = Space(RED, RADIUS, [3, 3], True) board[2][3] = Space(RED, RADIUS, [2, 3], True) board[4][3] = Space(RED, RADIUS, [4, 3], True) board[3][2] = Space(RED, RADIUS, [3, 2], True) board[3][4] = Space(RED, RADIUS, [3, 4], True) </code></pre> <p>Here's the <strong>init</strong> method of the Space class that I'm using:</p> <pre><code>def __init__(self, color, size, coords, is_stone): self.color = color self.size = size self.coords = coords self.is_stone = is_stone #false if empty self.pos = [CCONSTANT + DISTANCE * self.coords[0], CCONSTANT + DISTANCE * self.coords[1]] </code></pre> <p>Can anyone tell me what I'm messing up?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload