![](/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.