![](/img/trans.png)
[英]Making Checkers in python with the module pygame but are running into some logic errors I don't want the code changed just this problem fixed
[英]Im making Checkers in pygame the way I want a piece to move is with 2 mouse clicks but I don't know how to do that
现在我有一个拖动机制,它是下面的代码有人知道我可以如何制作它,所以第一次点击选择一块,第二次点击移动一块。 通过简单地将另一个 mousebuttondown 放入第一个 mousebuttondown 没有任何反应。
while not game_over:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
start_x = event.pos[0]
start_y = event.pos[1]
if event.type == pygame.MOUSEBUTTONUP:
end_x = event.pos[0]
end_y = event.pos[1]
board.valid_moves(start_x, start_y)
board.get_mouse_pos_and_place(start_x, start_y, end_x, end_y)
使用第一次单击鼠标时设置的变量。 当鼠标第二次点击时进行移动。
用None
初始化start_x
和start_y
。 单击鼠标时设置变量。 再次单击鼠标时使用变量:
start_x = None
start_y = None
while not game_over:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if start_x == None and start_y == None:
# select piece
start_x, start_y = event.pos
else:
# do movement
end_x, end_y = event.pos
board.valid_moves(start_x, start_y)
board.get_mouse_pos_and_place(start_x, start_y, end_x, end_y)
# prepare for next move
start_x = None
start_y = None
您可以通过检查第一次点击是否在一块上来改进算法:
if event.type == pygame.MOUSEBUTTONDOWN:
if start_x == None and start_y == None:
# select piece
click_x, click_y = event.pos
is_on_piece = # [...] check if click_x, click_y is on a piece
if is_on_piece:
start_x = click_x
start_y = click_y
else:
# [...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.