简体   繁体   English

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

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

I am in the progress of making chess using Pygame. 我正在使用Pygame制作国际象棋。

Currently, I only have the ability to make white pawns and white bishops. 目前,我只能制造白色的棋子和白色的主教。 I have a projected() function for each of the WhitePawn and WhiteBishop classes, which highlights the positions on the board that the pieces can go. 我为每个WhitePawnWhiteBishop类都有一个projected()函数,该函数突出显示棋子可以移动的位置。 For example, if there is a pawn on E2 and a bishop on E4, then E2 (when you click the piece) and E3 would light up, but E4 would not light up because the bishop is in the way. 例如,如果E2上有一个棋子,而E4上有一个主教,则E2(当您单击该棋子时)和E3会亮起,但E4不会亮​​起,因为主教在路上。 When you click on the lit up square, then it moves the piece to that position on the board. 当您单击发光的正方形时,它将把棋子移动到板上的那个位置。 The noProjected() function for both classes is supposed to remove the lit up positions on the board. 这两个类的noProjected()函数应该删除板上的发光位置。

My issue right now is that if the white pawn tries to move through a white bishop's projected path (even if I didn't click on it), it doesn't light up the square and therefore I can't move there. 现在我的问题是,如果白色棋子试图通过白色主教的投影路径移动(即使我没有单击它),它也不会照亮正方形,因此我不能移动到那里。 For example, if a White Pawn is on E2 and White Bishop on D4, then E2 and E4 light up, when E2, E3, and E4 are supposed to light up. 例如,如果E2上有白色棋子,D4上有White Bishop,则当E2,E3和E4应该亮起时,E2和E4亮起。

I do have a guess that the issue is the for loop on the bottom: play.totalPlayList consists of play.whitePawnList (all the white pawns on board) and play.whiteBishopList in that order. 我确实认为问题出在底部的for循环:play.totalPlayList由play.whitePawnList(板上的所有白色棋子)和play.whiteBishopList依次组成。 When I reversed the order, in that list, the pawn movement worked (but of course the bishop movement didn't). 当我颠倒顺序时,在该列表中,典当运动起作用(但是主教运动当然没有起作用)。 So even if I click on the pawn only, the whiteBishop.noProjected() function still gets called which I don't want. 因此,即使我仅单击了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()

Thanks! 谢谢!

You've pretty much identified the problem yourself: you're calling noProjected() for every piece on the board, regardless of whether or not it's moving -- and that's not the functionality you need there. 您几乎已经确定了问题所在:无论板上是否移动,您都在为板上的每个部件调用noProjected() ,这不是您所需要的功能。 Instead, you need to wait until the player chooses a piece, and then make the calls to trace the moves for only that piece. 相反,你需要等待,直到玩家选择一块,然后进行电话追踪移动仅件。

If you're moving the pawn, you have no business processing where the bishop can move; 如果您要移动典当,那么主教就无法进行业务处理。 the only problem is whether the bishop itself is in the way. 唯一的问题是主教本身是否挡路。 You shouldn't change highlights based on multiple lines of attack. 您不应基于多条攻击线来更改亮点。

One note for your eventual game: you do have to pay attention when castling: the king cannot castle out of, into, or through check: if that middle square is under enemy attack, the player can't castle. 你最终的游戏一个注意: 必须易位的时候要注意:王不能城堡出来,进入或通过检查:如果这中间的广场下敌人的攻击,玩家无法城堡。

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

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