簡體   English   中英

我在 pygame 中制作跳棋,我想要一塊移動的方式是點擊 2 次鼠標,但我不知道該怎么做

[英]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_xstart_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM