Now comes the most exciting part. In this article, we will be Display Images with Pygame. So, one of the most important things that you need to move further in this tutorial is of-course images.
To download free icons, we can use Flaticon.
Flaticon offers a wide catalog of free icons. When you perform a search, there will be a few options shown on the top left corner of the screen, where you can filter by “Free”, for content free of charge, or by “Premium”, for icons available only to Premium users.
1. Display Images with Pygame: Background Image
To download images, we can use Freepik.
Freepik offers a wide catalog of free resources, which you can find by filtering by “Free”, from the “Filters” option on the site, on the top right corner of the screen. This content is completely free of charge.
Now, to Display Images with Pygame, we will be importing our image to our python program by using pygame. And it is very simple just like we did earlier by using the image module of pygame. The function that we are gonna use is load().
load() takes 2 arguments if you are passing a file object else you need to enter only one argument that is the filename.
Syntax:
pygame.image.load(filename) -> Surface pygame.image.load(fileobj, namehint="") -> Surface
Load an image from a file source. You can pass either a filename or a Python file-like object.
While we want to Display Images with Pygame, Pygame will automatically determine the image type (e.g., GIF
or bitmap) and create a new Surface object from the data. In some cases, it will need to know the file extension (e.g., GIF
images should end in “.gif”). If you pass a raw file-like object, you may also want to pass the original filename as the namehint argument.
To load the image we will use the following code:
background = pygame.image.load('<name of the image>')
After adding this, your code should be looking like this:
Now, after we have loaded the image inside our background variable. We need to place it on our game window. To do so, we need to use a function called blit(). It is the function of our window/screen. It is used to draw one surface on to another.
blit() takes 4 arguments that are the following:
blit(source, dest, area=None, special_flags=0) -> Rect
The draw can be positioned with the dest argument. The dest argument can either be a pair of coordinates representing the position of the upper left corner of the blit or a Rect, where the upper left corner of the rectangle will be used as the position for the blit. The size of the destination rectangle does not effect the blit.
An optional area rectangle and a special flag can be passed as well. This represents a smaller portion of the source Surface to draw.
So, now we will write code inside our game loop (while loop). We need to put it in the beginning of the loop as it is our background image, nothing should come before it.
The code that we need to write is:
screen.blit(background,(0,0))
After writing this your code must be looking like this:
(adsbygoogle = window.adsbygoogle || []).push({});
2. Display Images with Pygame: Game Window Image
Now we will be adding images and graphics and it is the same way we added our background image. But we need to specify the coordinate of our images.
The first thing that we need to do is load our image and then we need to do screen.blit(). It is as simple as that. But what we need to take care of that is we need to specify the coordinate of the image that you want to set in the cartesian coordinate system. And we need to put that part of the code after the background image inside the loop so that it does not get hidden behind the background image.
As pygame is specifically used to create Two Dimensional Games it has only two coordinates that are X and Y.
So let’s say that we want our image to change it’s place so we will define it’s coordinates (x,y) in variables. Like if the image is of a player then we will define it’s x coordinate inside a variable, let’s say playerx and the y coordinate inside another variable, let’s say playery.
Now if I want to change the position of the player image, then I can simply make changes to my playerx and playery variable.
If we want that the player should change it’s position horizontally, then we can easily do it by changing the x coordinate that is inside the playerx variable.
And just like that if we want that the player should change it’s position vertically, then we can easily do it by changing the y coordinate that is inside the playery variable.
So, now let’s consider the following code:
playerx = 100 # Creating the variable for the x coordinate of the player
playery = 150 # Creating the variable for the y coordinate of the player
Now loading the image into our python file
player = pygame.image.load('player.jpg')
Now, let’s do screen.blit()
screen.blit(player,(playerx,playery))
Remember that we need to write screen.blit() code inside our game loop after the for-loop but before pygame.display.update().
After doing that, your code should be looking like this:
import pygame pygame.init() screen = pygame.display.set_mode((800,600)) # Title pygame.display.set_caption("Copy Assignment") icon = pygame.image.load("task.png") pygame.display.set_icon(icon) isRunning = True #Background background = pygame.image.load("planet.png") #Player playerx = 100 # Creating the variable for the x coordinate of the player playery = 150 # Creating the variable for the y coordinate of the player # Now loading the image into our python file player = pygame.image.load('player.jpg') while(isRunning ==True): screen.blit(background,(0,0)) for event in pygame.event.get(): if event.type == pygame.QUIT: isRunning = False screen.blit(player,(playerx,playery)) pygame.display.update()
So, now that’s for this article, For any further queries, drop it inside the comment section. Until then Enjoy learning.
Also read:
- Games in Python | Assignment Expert
- Creating a Pong Game using Python TurtleIntroduction Today, we are going to create a Pong Game using Python Turtle. Pong is a well-known computer game that is similar to table tennis. The two players in this game control the two paddles on either side of the game window. To hit the moving ball, they move the paddles up and down. A…
- Balloon Shooter Game using Python PyGameWe all know the importance of the pygame library in terms of game development. We know that Pygame is a collection of Python modules for making video games. It is a collection of computer graphics and sound libraries for the Python programming language. Pygame is well suited to the development of client-side applications that may…
- Complete PyGame Tutorial and ProjectsWithout a doubt, we can say that Python is a multi-purpose language. Ranging from basic programming to its application in Data Science, from Machine Learning to Artificial Intelligence, and also in Developing Games. In simple words, it’s the language that is incomparable.With knowing the advantages of Python, today, in this article of Complete PyGame Tutorial…
- Flappy Bird In Python Pygame with source codeOVERVIEW OF THE PROJECT Hi there, welcome to the copyassignment hope you all are doing wonderful. Today, we are here to make a very interesting game Flappy Bird In Python Pygame. Hope you are familiar with this entertaining and very fascinating game. We would see how we can code the game Flappy bird from Scratch….