

What we've effectively done in the above code is take the image stored in the surface under the variable tom, resize it to be 50 pixels wide and 95 pixels high, then save it back into the original surface, tom. NewSurface = (originalImage, (newWidth, newHeight)) If you add the above code in the Processing section of your game loop and run your program you should find that Tom is now half the size he was before. Let's make Tom a more reasonable size for our window : I've just introduced the more useful / common ones here but if you do a quick search you can discover more complex transforms available to you. There are other operations available through the transform module as well. You may apply these operations to any surface however, not just images.

In the above program you will notice that our character ( Tom ) is a little bit large in our window. This is a nice advantage of using PNG over JPEG as the file format for your images as otherwise your game elements will have borders around them which is a bit unsightly. If you change the BACKGROUND colour you will also notice that the transparency in the png image has been brought through as well. The blit command does what is called a Bit BLock Transfer and copies the bits from the surface which holds our image (in the above script, held by the variable tom) into the relevant location on the surface which is our actual WINDOW. Add in the following line just between filling the window and updating the display : If you save and run the program you will notice that nothing has yet happened. You can leave this off and the conversion will happen when you BLIT the image to the screen (see below) but if you do this then it has to do the conversion every time the screen is updated (30, maybe 60 times a second) which will give a big hit to your game performance. convert_alpha() sets your surface to manage colours and transparency in the native format for your operating system. This will give a slight performance boost as you won't need to keep loading the same image. If you will be using the image in several areas across several functions however then it may be better to load the image once at the top of the script as a global variable. This is an ideal location to load the image if it is only going to be used within that function.

Here we have loaded the image into a variable within the function that it will be used. This command will load the image into what is called a surface and save it to a variable called tom. tom = ('images/tom_standing.png').convert_alpha().Place the following lines of code in your main function, near the top like so : Let's start by loading our image so that it is ready to use. Make some modifications to the image (resize, crop, rotate etc).Including images is a three step process (though a lot of the time we can skip the second step) : It also makes things easier when you want to package up and share your game later on. It is possible to access and load the images from anywhere on the system but if you place them in the same location as the Python file it makes things a little easier. Create a directory called images in the same directory as your Python file and place these images into this directory. We will also need some images to play with. Once you've saved your file with the template code in it, run the file to make sure you've copied it in ok. Getting Set Upīefore we begin, let's create a new file (call it images.py) and copy in the template code from the previous section Good quality visuals can go a long way to making a game engaging and drawing the player in. In this section we will look at how you include and manipulate images in Pygame.
