繁体   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