Solving problem is about exposing yourself to as many situations as possible like What is a good way to draw images using pygame? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about What is a good way to draw images using pygame?, which can be followed any time. Take easy to follow this discuss.
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.
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()
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
Run this and it works perfectly. So hopefully you learn something.
blit or any other update on your drawing surface, you have to call
pygame.display.flip() to actually update what is displayed.
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))
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:
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: