Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PYGAME:为什么在游戏循环中调用游戏循环中的函数会使我的游戏延迟?_Python_Python 3.x_Pygame_Pygame Surface_Pygame Tick - Fatal编程技术网

Python PYGAME:为什么在游戏循环中调用游戏循环中的函数会使我的游戏延迟?

Python PYGAME:为什么在游戏循环中调用游戏循环中的函数会使我的游戏延迟?,python,python-3.x,pygame,pygame-surface,pygame-tick,Python,Python 3.x,Pygame,Pygame Surface,Pygame Tick,我正在做一个简单的游戏,敌人在屏幕上移动,我们需要射击他们。我想模块化我的代码,所以我想用一个函数替换游戏循环逻辑。但一旦我这么做,fps就下降了。在while循环中调用函数是否会降低fps 如果不使用函数,我的游戏循环是: while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()

我正在做一个简单的游戏,敌人在屏幕上移动,我们需要射击他们。我想模块化我的代码,所以我想用一个函数替换游戏循环逻辑。但一旦我这么做,fps就下降了。在while循环中调用函数是否会降低fps

如果不使用函数,我的游戏循环是:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            crosshair.shoot()
    
        pygame.display.update()
        #blit bg
        displaysurf.blit(background,(0,0))
        #render group of sprites
        target_group.draw(displaysurf)
        crosshair_group.draw(displaysurf)
        #call the update methods
        crosshair_group.update()
        target_group.update()
        #display fps
        #print(clock.get_fps())
        #restrict to 60frames drawing per second
        clock.tick(60)
具有以下功能:

def first_level():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            crosshair.shoot()
    
        pygame.display.update()
        #blit bg
        displaysurf.blit(background,(0,0))
        #render group of sprites
        target_group.draw(displaysurf)
        crosshair_group.draw(displaysurf)
        #call the update methods
        crosshair_group.update()
        target_group.update()
        #display fps
        #print(clock.get_fps())
        #restrict to 60frames drawing per second
        clock.tick(60)
while True: 
        first_level()

但当我添加此功能时,我的游戏开始因FPS减少而延迟。为什么会发生这种情况?

看起来你把缩进搞糟了
pygame.display.update()
以及之后的所有内容都不应成为事件循环的一部分

def first_level():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            crosshair.shoot()
    
    pygame.display.update()
    #blit bg
    displaysurf.blit(background,(0,0))
    #render group of sprites
    target_group.draw(displaysurf)
    crosshair_group.draw(displaysurf)
    #call the update methods
    crosshair_group.update()
    target_group.update()
    #display fps
    #print(clock.get_fps())
    #restrict to 60frames drawing per second
    clock.tick(60)

while True: 
    first_level()