Any way to speed up Python and Pygame?

前端 未结 6 1484
故里飘歌
故里飘歌 2020-12-28 08:37

I am writing a simple top down rpg in Pygame, and I have found that it is quite slow.... Although I am not expecting python or pygame to match the FPS of games made with com

6条回答
  •  半阙折子戏
    2020-12-28 08:57

    All of these are great suggestions and work well, but you should also keep in mind two things:

    1) Blitting surfaces onto surfaces is faster than drawing directly. So pre-drawing fixed images onto surfaces (outside the main game loop), then blitting the surface to the main screen will be more efficient. For exmample:

    # pre-draw image outside of main game loop
    image_rect = get_image("filename").get_rect()
    image_surface = pygame.Surface((image_rect.width, image_rect.height))
    image_surface.blit(get_image("filename"), image_rect)
    ......
    # inside main game loop - blit surface to surface (the main screen)
    screen.blit(image_surface, image_rect)
    

    2) Make sure you aren't wasting resources by drawing stuff the user can't see. for example:

    if point.x >= 0 and point.x <= SCREEN_WIDTH and point.y >= 0 and point.y <= SCREEN_HEIGHT:
        # then draw your item
    

    These are some general concepts that help me keep FPS high.

提交回复
热议问题