繁体   English   中英

用Python创建国际象棋,棋子动作受到Bishop的影响

[英]Creating Chess in Python, and Pawn Movement Gets Affected By Bishop

我正在使用Pygame制作国际象棋。

目前,我只能制造白色的棋子和白色的主教。 我为每个WhitePawnWhiteBishop类都有一个projected()函数,该函数突出显示棋子可以移动的位置。 例如,如果E2上有一个棋子,而E4上有一个主教,则E2(当您单击该棋子时)和E3会亮起,但E4不会亮​​起,因为主教在路上。 当您单击发光的正方形时,它将把棋子移动到板上的那个位置。 这两个类的noProjected()函数应该删除板上的发光位置。

现在我的问题是,如果白色棋子试图通过白色主教的投影路径移动(即使我没有单击它),它也不会照亮正方形,因此我不能移动到那里。 例如,如果E2上有白色棋子,D4上有White Bishop,则当E2,E3和E4应该亮起时,E2和E4亮起。

我确实认为问题出在底部的for循环:play.totalPlayList由play.whitePawnList(板上的所有白色棋子)和play.whiteBishopList依次组成。 当我颠倒顺序时,在该列表中,典当运动起作用(但是主教运动当然没有起作用)。 因此,即使我仅单击了pawn,whiteBishop.noProjected()函数仍然会被调用,而这是我不希望的。

class PlayWhiteBishop(pygame.sprite.Sprite):
    def __init__(self):
        self.select = 0
    def update(self):
    def highlight(self):
        self.image = images["sprWhiteBishopHighlighted"]
        self.select = 1
    def projected(self):
        for grid in room.gridList:
            for i in range(1,8):
                if ord(grid.coordinate[0]) == ord(self.coordinate[0])-i and grid.coordinate[1] == self.coordinate[1]-i and grid.occupied == 0:
                    grid.highlight()
            for i in range(1,8):
                if ord(grid.coordinate[0]) == ord(self.coordinate[0])-i and grid.coordinate[1] == self.coordinate[1]+i and grid.occupied == 0:
                    grid.highlight()
            for i in range(1,8):
                if ord(grid.coordinate[0]) == ord(self.coordinate[0])+i and grid.coordinate[1] == self.coordinate[1]-i and grid.occupied == 0:
                    grid.highlight()
            for i in range(1,8):
                if ord(grid.coordinate[0]) == ord(self.coordinate[0])+i and grid.coordinate[1] == self.coordinate[1]+i and grid.occupied == 0:
                    grid.highlight()
    def noHighlight(self):
        self.image = images["sprWhiteBishop"]
        self.select = 0
    def noProjected(self):
        #SAME EXACT CODE as projected() except replace grid.highlight() with grid.noHighlight()

while RUNNING:
    elif (event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]
        for pieceList in play.totalPlayList:
            for piece in pieceList:
                if (piece.rect.collidepoint(mousePos) and piece.select == 0):
                    piece.highlight()
                    piece.projected()
                else:
                    piece.noHighlight()
                    piece.noProjected()

谢谢!

您几乎已经确定了问题所在:无论板上是否移动,您都在为板上的每个部件调用noProjected() ,这不是您所需要的功能。 相反,你需要等待,直到玩家选择一块,然后进行电话追踪移动仅件。

如果您要移动典当,那么主教就无法进行业务处理。 唯一的问题是主教本身是否挡路。 您不应基于多条攻击线来更改亮点。

你最终的游戏一个注意: 必须易位的时候要注意:王不能城堡出来,进入或通过检查:如果这中间的广场下敌人的攻击,玩家无法城堡。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM