我正在尝试在 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/