Pygame低帧速率,但立即等待输入

2024-10-03 23:27:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

如果我不点击60/1000毫秒,它就不工作了,对吗?若我是,那个么是否还有其他可能以每秒60帧的速度运行游戏并像往常一样等待输入

使用此按钮捕捉单击:

    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)



Tags: andposeventtrue游戏forgetif
1条回答
网友
1楼 · 发布于 2024-10-03 23:27:57

如果事件处理程序检测到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暂停整个程序,因此冻结窗口

相关问题 更多 >