简体   繁体   中英

How to draw an image in (pygame)

I want to replace this draw rectangle function, and instead let it produce an image.

Rectangle Function:

pygame.draw.rect(pygame.display.set_mode((800,600)), black, [0,0, 100, 100])

Looking for something on the lines of this:

pygame.draw.image(**coordinatesX**, **coordinatesY**, **imagefile.jpg**)

To load an image, you need the file stored, and you load it using:

pygame.image.load('example.png')

Now your code should look like:

pygame.image.load('example.png')

pygame.display.set_mode((800, 600))

so first you have to load the image using image = pygame.image.load() and then use screen.blit(image) to draw it on the screen. For this to work you should define the screen at the beginning not in the draw function like you have done above. Just screen = pygame.display.set_mode((800,600)) should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM