Cant figure out how to kill a checker when it is jumped over

Viewed 43

I am trying to make a game of checkers in pygame, and can't think of a way to do the logic for killing a checker once I jump over it. If you need any more information let me know. Here is the function in the checker sprite class:

    # 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
1 Answers

ok I think I figured it out, if there is a better way let me know (the distances in the if statements are the output if it is a jump in a specific direction):

            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()
Related