What is a good way to draw images using pygame?

2020-02-10 04:58发布

问题:

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 draw the image and instead leaves the screen blank.

回答1:

This is a typical layout:

myimage = pygame.image.load("myimage.bmp")
imagerect = myimage.get_rect()

while 1:
    your_code_here

    screen.fill(black)
    screen.blit(myimage, imagerect)
    pygame.display.flip()


回答2:

import pygame, sys, os
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((100, 100))

player = pygame.image.load(os.path.join("player.png"))
player.convert()

while True:
    screen.blit(player, (10, 10))
    pygame.display.flip()

pygame.quit()

Loads the file player.png. Run this and it works perfectly. So hopefully you learn something.



回答3:

After using blit or any other update on your drawing surface, you have to call pygame.display.flip() to actually update what is displayed.