3

I'm new to python and I'm trying to make a chess game with pygame, I have the chessboard and the various pieces placed as objects on it. This is the pieces class:

class piece(object):
    def __init__(self, x, y, which):
        self.x = x
        self.y = y
        self.which = which
        self.square = getsquare(x + PIECE/2, y + PIECE/2)
        self.dragging = False

    def drag(self, x, y):
        limit = 720
        if x >= limit:
            x = limit
        self.x = x - PIECE/2
        self.y = y - PIECE/2
    def draw(self, win):
        win.blit(self.which, (self.x, self.y))
        self.square = getsquare(self.x + PIECE/2, self.y + PIECE/2)

where PIECE is the dimension of the spritesheets with the pieces images. I tried to make a drag system for pieces (stored in a 64 element long list) and by using only 1 piece it worked, but when I used the full list it stopped working without rasing any error. This is the drag system:

"""event listeners"""
for event in pygame.event.get():
    if event.type == pygame.QUIT: #quit event
        run = False
    """mouse release"""
    if event.type == pygame.MOUSEBUTTONUP:
        clickpos = pygame.mouse.get_pos()
        x = clickpos[0]
        y = clickpos[1]
        sqr = getsquare(x, y)
        for i in pieceslist:
            if not i == "none":
                if i.dragging:
                    i.dragging = False
                    try:
                        i.x = squarepos(i.square[0], i.square[1])[1]
                        i.y = squarepos(i.square[0], i.square[1])[0]
                    except:
                        i.x = squarepos(originalsquare[0], originalsquare[1])[1]
                        i.y = squarepos(originalsquare[0], originalsquare[1])[0]
    """mouse click"""
    if event.type == pygame.MOUSEBUTTONDOWN:
        clickpos = pygame.mouse.get_pos()
        x = clickpos[0]
        y = clickpos[1]
        #print("X: " + str(x) + ", Y: " + str(y))
        sqr = getsquare(x, y)
        for i in pieceslist:
            if not i == "none":
                if sqr == i.square:
                    originalsquare = sqr
                    i.dragging = True
    """mouse drag"""
    if event.type == pygame.MOUSEMOTION:
        clickpos = pygame.mouse.get_pos()
        x = clickpos[0]
        y = clickpos[1]
        #print("X: " + str(x) + ", Y: " + str(y))
        sqr = getsquare(x, y)
        for i in pieceslist:
            if not i == "none":
                if i.dragging:
                    i.drag(x, y)

since pieceslist is filled with piece objects and "none" strings I made the if checks (I know there surely are better ways to do this but I'm new to python)

So, the problem is that the click event works and it modifies dragging, but when it comes to the drag event the object no longer has dragging == True

EDIT:

squarepos() returns the coordinates where to put the spritesheet, getsquare() returns the coordinates by row-column:

def getsquare(x, y):
    if x <= BORDER or y <= BORDER or x >= squarepos(1,  9)[0] or y >= squarepos(9, 1)[1]:
        pass #not on the board
    else:
        x -= BORDER
        y -= BORDER
        x /= SQUARE
        y /= SQUARE
        return [int(x) + 1, int(y) + 1]

EDIT:

Full program here for testing and debugging

2

2 Answers 2

1

The dragging algorithm actually works. However, defBoardPieces() is called in every frame. Therefore, the game is reset every frame. And the dragging has no effect.

Remove the defBoardPieces() call from the drawBoardPieces function, but call it once before the application loop:

#renders board pieces
def drawBoardPieces(win):

    # defBoardPieces()    <--- DELETE

    for i in pieceslist:
        if not i == "none":
            i.draw(win)
pieceslist = []
startsquare = []

defBoardPieces()    # <--- INSERT

run = True
while run:
    # [...]

Call defBoardPieces() also in reset:

def reset():
    global position
    position = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
    defBoardPieces()

Sign up to request clarification or add additional context in comments.

Comments

0

Use mouse.get_pressed()[0] to drag. 0 is left button.

if event.type == pygame.MOUSEBUTTONDOWN:
    if pygame.mouse.get_pressed()[0]:
    ... drag code here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.