python - 跳过跳棋时无法弄清楚如何杀死它

标签 python pygame

我正在尝试在 pygame 中制作跳棋游戏,但我想不出一种方法来执行跳过跳棋后杀死跳棋的逻辑。如果您需要更多信息,请告诉我。这是检查器 Sprite 类中的函数:

    # Determine if move is valid, and if so snap to the grid
    # and tell the game logic that there is a checker there
    def snap_to_grid(self):
        global turn
        x, y = self.rect.topleft
        
        coord_x = round(x / square_size)
        coord_y = round(y / square_size)
        
        new_x = coord_x * square_size + 5
        new_y = coord_y * square_size + 5
        
        # get all valid moves from the starting position
        valid_moves = self.valid_moves()
        
        # check if it is valid, or if it is going back to the same spot. If either is true,
        # then reset. If not, move to that spot.
        if [new_x, new_y] == self.location or not [coord_x, coord_y] in valid_moves:
            self.set_pos(self.location, 'topleft')
        else:
            # move to new position
            self.set_pos((new_x, new_y), 'topleft')
            
            # tell game data that the checker moved from the old square
            board[self.coords[1]][self.coords[0]] = None
            
            # the next few lines are to determine if the checker jumped or not
            old_x, old_y = self.coords
            
            self.location = [new_x, new_y]
            self.coords = [int(self.location[0]//square_size), int(self.location[1]//square_size)]
            
            distance = abs((old_x - self.coords[0])^2 + (old_y - self.coords[1])*2)
        
            
            # check if checker jumped
            if not distance == 1 and not distance == 5:
                print("jumped")
                
                """
                    this code here should trigger when a jump happens.
                    I need to kill the checker it jumped over
                """

            # tell game data the checker moved to the new square
            board[self.coords[1]][self.coords[0]] = self
            
            # set to be the next turn
            if self.color == white:
                turn = dark_gray
            else:
                turn = white

最佳答案

好的,我想我明白了,如果有更好的方法让我知道(if 语句中的距离是在特定方向跳跃时的输出):

            old_x, old_y = self.coords
            
            self.location = [new_x, new_y]
            self.coords = [int(self.location[0]//square_size), int(self.location[1]//square_size)]
            
            distance = (old_x - self.coords[0])^2 + (old_y - self.coords[1])*2
            
            # check if checker jumped top right
            if distance == -8:
                board[self.coords[1]+1][self.coords[0]-1].kill()
            # check if checker jumped top left
            elif distance == 4:
                board[self.coords[1]+1][self.coords[0]+1].kill()
            # check if checker jumped bottom right
            elif distance == 0:
                board[self.coords[1]-1][self.coords[0]-1].kill()
            # check if checker jumped bottom left
            elif distance == -4:
                board[self.coords[1]-1][self.coords[0]+1].kill()

关于python - 跳过跳棋时无法弄清楚如何杀死它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64707585/

相关文章:

python - 以特定格式转储 JSON 内容

python - 更改外部包的记录器类

python - 有没有办法找到在 "pygame"上按下的任何数字?以及如何显示它?

python - 在 Numpy 中复制子数组的列

python - 编辑 pickle 数据

Python peewee 迭代 SelectQuery

python - pygame - 按下按键

python - 在Python中获取 'TypeError: invalid destination position for blit'

python - 近距离 3D 显示困惑

python - 我的播放器移动不正确,按住键时,它应该连续移动,但只移动一次