0

我想用 pygame 在 python 中创建简单的游戏。有一些按钮,但有时它不响应。我想要低帧率,因为它的游戏简单,但我也想让按钮像往常一样等待点击。有可能吗?

就像我不点击那 60/1000 毫秒它不起作用,对吗?如果我是,那么还有其他可能以 60 fps 运行游戏并像往常一样等待输入吗?

使用它来捕捉点击:

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680:
                button_hod_clicked = True

在主循环中使用它:

clock.tick(FPS)

结构体:

def check_buttons(pos):

    button_hod_clicked = False
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680:
                button_hod_clicked = True

    return button_hod_clicked



def main():
   run = True
   striedanie = True
   while run:
      clock.tick(FPS)
      pos = pygame.mouse.get_pos()
      button_hod_clicked = check_buttons(pos)
      if button_hod_clicked:
          if striedanie == True:
              player1_position, kolko_hodil = player_movement(player1_position)
              striedanie = False
            else:
              player2_position, kolko_hodil = player_movement(player2_position)
              striedanie = True
            time.sleep(1)


4

1 回答 1

1

MOUSEBUTTONUP如果事件处理程序检测到并调用函数,只需获取鼠标位置,而不是每帧获取鼠标位置check_buttons

def check_buttons(pos):
    return pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680

button_hod_clicked = False
def main():
    global button_hod_clicked
    ...
    ...
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONUP:
            pos = pygame.mouse.get_pos()
            button_hod_clicked = check_buttons(pos)

    if button_hod_clicked:
        ...

让我知道这个是否奏效。

注意:time.sleep暂停整个程序,因此它冻结窗口。

于 2021-08-31T02:15:26.107 回答