【问题标题】:How to set framerate in Pygame? [duplicate]如何在 Pygame 中设置帧率? [复制]
【发布时间】:2021-06-13 23:26:44
【问题描述】:

所以在回答我的问题 (How to continuously move an image smoothly in Pygame?) 时,我被告知要设置帧速率以制作动画。但无论我把它放在哪里,clock.tick(60) 什么都不做。我做clock = pygame.time.Clock() BTW。那么我该怎么做呢?我已经研究并发现了这个(pygame clock.tick() vs framerate in game main loop),但我不太明白。

我的主要游戏功能;

def main_game():
    global var_escape
    global var_start_game
    global var_menu_screen
    global var_camera
    global var_bounce
    global var_x
    global var_x_timer
    global var_door
    global var_light
    global camera_flip
    
    var_start_game = False
    var_escape = False
    var_menu_screen = 0
    var_bounce = 0
    var_x = 0
    var_camera = 1
    var_x_timer = 0
    var_light = 0
    var_door = 0
    var_camera_flip = 0
    
    pygame.mixer.init()
    pygame.mixer.music.load(r'audio\Menu_Music.mp3')
    pygame.mixer.music.play(-1)

    pygame.mixer.music.set_volume(1.0)

    while True:
        if var_menu_screen == 0:
            menu_screen()
        if var_menu_screen == 1:
            options_screen()
        if var_menu_screen == 2:
            new_game_boyz()
        if var_menu_screen == 3:
            pygame.quit()
            sys.quit()
        if var_menu_screen == 4:
            credits_screen()
        if var_start_game == True:
            break
    
    var_start_game = False
    
    start_game()
    image_door_6_off = pygame.image.load(r'textures\door\light off\frame_6_off.png')
    image_door_button_off = pygame.image.load(r'textures\door button\door_button_off.png')
    image_light_button_off = pygame.image.load(r'textures\light button\light_button_off.png')
    image_door_6_off_size = image_door_6_off.get_rect().size
    centered_image_door_6_off = [(display_size[0] - image_door_6_off_size[0])/2, (display_size[1] - image_door_6_off_size[1])/2]
    screen.blit(image_door_6_off, centered_image_door_6_off)
    screen.blit(image_door_button_off, (0, 0))
    screen.blit(image_light_button_off, (1113, 0))
    pygame.display.update()

    while var_escape == False:
#This is my main game loop in the function
#This is where I would like to set the framerate
        mouse_down = False
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    quit_popup()
            elif event.type == MOUSEBUTTONDOWN:
                mouse_down = True
            
        light(mouse_down)
        door(mouse_down)
        camera_flip()
#Just so I can get a smoother animation for this function below
        cameras() #<--- This function here
#Just so I can get a smoother animation for this function above
        camera_buttons(mouse_down)

这是我的主要游戏循环;

while True:     
#Calls the function 
    main_game()

主游戏循环不会设置帧率,因为 main_game 函数内部有一个循环。这只是一个循环,因为当我想重新启动该功能时,我可以。我刚刚添加了这个来显示函数是如何运行的。

【问题讨论】:

  • 在“while var_escape == False:”之后放置clock.tick(FPS)。尝试使用 FPS = 60,然后尝试将其更改为 FPS = 5。您需要在开始附近的某个位置设置 FPS。
  • 你必须在主循环中运行它 - 在每个循环中 - 即。屏幕翻转后。但这会尝试设置最大 FPS(每秒帧数) - 如果您有可以运行 120 FPS 的快速计算机,那么它将降低到 60 FPS,但是如果您的计算机速度较慢而无法获得 60 FPS,那么您可能会获得更少的 FPS。人眼至少需要 24 FPS 才能看到动画。
  • 主游戏循环是while var_escape == False: 循环。

标签: python pygame


【解决方案1】:

使用pygame.time.Clock 控制每秒帧数,从而控制游戏速度。

pygame.time.Clock 对象的方法tick() 以这种方式延迟游戏,即循环的每次迭代消耗相同的时间段。见pygame.time.Clock.tick()

这个方法应该每帧调用一次。

例如:

def main_game():
    # [...]

    FPS = 60
    clock = pygame.time.Clock()
    while var_escape == False:
        clock.tick(60)

        # [...]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    相关资源
    最近更新 更多