Python Pygame代码从另一个文件导入时运行非常慢
我对python比较陌生,所以如果答案很明显,请原谅我Python Pygame代码从另一个文件导入时运行非常慢,python,pygame,Python,Pygame,我对python比较陌生,所以如果答案很明显,请原谅我 def create_fleet(ai_settings, screen, aliens): """Create the horizontal Alien fleet""" alien = Alien(ai_settings, screen) alien_width = alien.rect.width available_x = ai_settings.screen_width - alien_width
def create_fleet(ai_settings, screen, aliens):
"""Create the horizontal Alien fleet"""
alien = Alien(ai_settings, screen)
alien_width = alien.rect.width
available_x = ai_settings.screen_width - alien_width
num_aliens_x = int(available_x / ( 2 * alien_width))
for alien_number_x in range(num_aliens_x):
alien = Alien(ai_settings, screen)
# define the starting point of each alien
alien.x = alien_width + 2 * alien_width * alien_number_x
alien.rect.x = alien.x
aliens.add(alien)
def update_screen(ai_settings, screen, ship, bullets, aliens):
screen.fill(ai_settings.bg_colour)
ship.blitme()
for bullet in bullets.sprites():
bullet.draw_bullet()
aliens.draw(screen)
pygame.display.flip()
这是主游戏文件:
def run_game():
运行游戏()
每当我将这些函数导入主游戏文件并运行游戏时,它都会爬行,但当代码直接在主游戏文件上运行时,它会正常运行。请提供有关如何使其在导入时正常工作的建议
编辑:我添加了主游戏文件,粗体是我使用导入代码的地方,这给我带来了问题。谢谢,我已经解决了。函数gf.create_fleet不应该在while循环中,因为它将不断添加到同一位置的组中,并且gf update_屏幕必须在同一点绘制所有的函数,这是一个严重的问题,并使我的编程爬行您可以添加主脚本以便我们可以重现该问题吗?谢谢。我就这么做了。
ai_settings = Settings()
pygame.init()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("ALIEN INVASION")
bullets = Group()
aliens = Group()
ship = Ship(screen, ai_settings)
while True:
gf.check_event(ai_settings, screen, ship, bullets)
ship.update()
bullets.update()
gf.remove_old_bullets(bullets)
**gf.create_fleet(ai_settings, screen, aliens)
gf.update_screen(ai_settings, screen, ship, bullets, aliens)**