python pygame鼠标事件不起作用并且不会引发错误

问题描述

我是 python 的新手,我正在尝试用 pygame 制作一个国际象棋游戏,我将棋盘和各种棋子放在上面作为对象。这是件类:

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,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)

其中 PIECE 是带有碎片图像的精灵表的尺寸。我试图为碎片制作一个拖动系统(存储在一个 64 个元素的长列表中)并且只使用 1 个它工作,但是当我使用完整列表时它停止工作而没有出现任何错误。这是拖拽系统:

"""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)
        for i in pieceslist:
            if not i == "none":
                if i.dragging:
                    i.drag(x,y)

因为 pieceslist 充满了 piece 对象和 "none" 字符串,所以我做了 if 检查(我知道肯定有更好的方法来做到这一点,但我是 Python 新手)

所以,问题是点击事件有效并且它修改dragging,但是当涉及到拖动事件时,对象不再有dragging == True

编辑:

squarepos() 返回放置 spritesheet 的坐标,getsquare() 返回按行列的坐标:

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]

编辑:

用于测试和调试的完整程序 here

解决方法

拖动算法确实有效。但是,defBoardPieces() 在每一帧中都会被调用。因此,游戏每帧都会重置。并且拖动没有效果。

defBoardPieces() 函数中删除 drawBoardPieces 调用,但在应用程序循环之前调用一次:

#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:
    # [...]

也在 defBoardPieces() 中调用 reset

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

,

使用 mouse.get_pressed()[0] 拖动。 0 是左键。

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