What is a good way to draw images using pygame?

前端 未结 4 860
灰色年华
灰色年华 2020-12-06 05:49

I would like to know how to draw images using pygame. I know how to load them. I have made a blank window. When I use screen.blit(blank, (10,10)), it does not d

4条回答
  •  青春惊慌失措
    2020-12-06 06:36

    Images are represented by "pygame.Surface" objects. A Surface can be created from an image with pygame.image.load:

    my_image_surface = pygame.load.image('my_image.jpg')
    

    However, the pygame documentation notes that:

    The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call convert() with no arguments, to create a copy that will draw more quickly on the screen.
    For alpha transparency, like in .png images, use the convert_alpha() method after loading so that the image has per pixel transparency.

    Use the appropriate conversion method for best performance:

    image_surface = pygame.load.image('my_image.jpg').convert()
    
    alpha_image_surface = pygame.load.image('my_icon.png').convert_alpha()
    

    A Surface can be drawn on or blended with another Surface using the blit method. The first argument to blit is the Surface that should be drawn. The second argument is either a tuple (x, y) representing the upper left corner or a rectangle. With a rectangle, only the upper left corner of the rectangle is taken into account. It should be mentioned that the window respectively display is also represented by a Surface. Therefore, drawing a Surface in the window is the same as drawing a Surface on a Surface:

    window_surface.blit(image_surface, (x, y))
    
    window_surface.blit(image_surface,
        image_surface.get_rect(center = window_surface.get_rect().center))
    

    Minimal example:

    import pygame
    
    pygame.init()
    window = pygame.display.set_mode((300, 300))
    clock = pygame.time.Clock()
    
    pygameSurface = pygame.image.load('apple.png').convert_alpha()
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        window.fill((127, 127, 127))
        window.blit(pygameSurface, pygameSurface.get_rect(center = window.get_rect().center))
        pygame.display.flip()
    
    pygame.quit()
    exit()
    

    pygame.image.load is bale to load most images. According to the documentation the following formats are supported: JPG, PNG, GIF (non-animated), BMP, PCX, TGA (uncompressed), TIF, LBM (and PBM), PBM (and PGM, PPM), XPM.

    If you want to use images in PyGame that are loaded with other libraries, see:

    • PIL and pygame.image
    • How do I convert an OpenCV (cv2) image (BGR and BGRA) to a pygame.Surface object

    For information on loading Scalable Vector Graphics (SVG) files, see:

    • SVG rendering in a PyGame application

    Loading animated GIF files is presented at:

    • How can I load an animated GIF and get all of the individual frames in PyGame?
    • How do I make a sprite as a gif in pygame?

    Or see how to load NumPy frames:

    • Pygame and Numpy Animations

提交回复
热议问题