precisiontore.blogg.se

Cropit tutorial
Cropit tutorial







cropit tutorial

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.

  • flip - which can be done either horizontally or vertically.Īlthough I've talked about an image with the operations above what they are actually processing is a surface (in this case the surface which holds the image).
  • scale - make the image larger or smaller.
  • Using the transform module we can perform a variety of operations on our image : PyGame offers us a means to transform the image within our code and this is an easier and cleaner way to go about it (especially if you want to scale your image for various reasons within your game or you don't know what size will be ideal yet and just want to tinker). We could for instance make the window larger, or we could open up the image of Tom in our favourite image editor and scale the image down a bit. There are a few ways we can rectify this.

    cropit tutorial

    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.

    cropit tutorial

    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.









    Cropit tutorial