Pygame – Surface
When using Pygame, surfaces are generally used to represent the appearance of the object and its position on the screen. All the objects, text, images that we create in Pygame are created using surfaces.
Creating a surface
Creating surfaces in pygame is quite easy. We just have to pass the height and the width with a tuple to pygame.Surface() method. We can use various methods to format our surface as we want. For example, we can use pygame.draw() to draw shapes, we can use surface.fill() method to fill on the surface. Now, the implementation of these functions. Let’s discuss syntax and parameters.
Syntax: pygame.surface()
It takes 4 arguments a tuple of width and height, flags, depth, mask.
pygame.draw():
It is used to draw an object, shape.
Syntax: Surface.rect(surface, color, rect)
The code for drawing a rectangle using the pygame.draw.rect() method is below:
Python
# Importing the libraryimport pygameimport time # Initializing Pygamepygame.init() # Creating the surfacesample_surface = pygame.display.set_mode((400,300)) # Choosing red color for the rectanglecolor = (255,255,0) # Drawing Rectanglepygame.draw.rect(sample_surface, color, pygame.Rect(30, 30, 60, 60))# The pygame.display.flip() method is used# to update content on the display screenpygame.display.flip() |
Output:
surface_name.fill():
pygame.Surface.fill: It is used for filling color in the surface.
Syntax:pygame.Surface.fill(color, rect=None, special_flags=0)
The code for filling color in the surface using the surface_name.fill() method is:
Python
# Importing the libraryimport pygame # Initializing Pygamepygame.init() # Creating the surfacesample_surface = pygame.display.set_mode((400,300)) # Choosing yellow color to fillcolor = (255,255,0)# filling color to the surfacesample_surface.fill(color)# updating the displaypygame.display.flip() |
Output:
Loading image on the surface
Although we can draw shapes and fill out colors on the surface, we still need to have a picture on the surface. We can make such a surface easily with pygame.image.load() method. This method takes the image path relative or absolute as an input.
Syntax: pygame.image.load(img)
Code:
Python
# Importing the libraryimport pygame# Initializing Pygamepygame.init()# creating the display surfacedisplay_surface = pygame.display.set_mode((500, 500 ))# Creating the image surfaceimage = pygame.image.load('gfg_logo.png')# putting our image surface on display# surfacedisplay_surface.blit(image,(100,100))# updating the displaypygame.display.flip() |
Output:
Adding Blitting
For a surface to be displayed it needs to blitted on the screen. Blitting can be thought of as copying the pixels of one surface to another. We can blit by using the surface.blit() method which takes a surface that needs to be blit as the first argument and the tuple of coordinates as a second coordinate.
Syntax: pygame.Surface.blit(source, dest, area=None, special_flags=0)
Python
# Importing the libraryimport pygame# Initializing Pygamepygame.init()# creating the display surfacedisplay_surface = pygame.display.set_mode((500, 500 ))# Creating the first image surfaceimage1 = pygame.image.load('gfg_logo.png')# Creating the second image surfaceimage2 = pygame.image.load('gfg_logo.png')# putting our first image surface on# display surfacedisplay_surface.blit(image1,(0,0))# putting our second image surface on# display surfacedisplay_surface.blit(image1,(300,300))# updating the displaypygame.display.flip() |
Output:
Now that we have already discussed some surface functions like .blit(), .fill(), etc. Let us discuss some more important functions of pygame screens.
- pygame.Surface.convert: It makes a copy of the surface with pixel format changed. The new pixel format can be determined from an existing surface or depth, flags, and masks arguments can be used.
Syntax: pygame.Surface.convert(Surface=None)
Code:
Python
# Importing the libraryimport pygame # Initializing Pygamepygame.init() # Creating the surfacesample_surface = pygame.display.set_mode((400,300))# changing the pixel format of an imagepygame.Surface.convert(sample_surface)# updating the displaypygame.display.flip() |
Output:
- pygame.Surface.convert_alpha: It creates a new copy of the surface with the desired pixel format. The new surface will be in a format suited for quick blitting to the given format with per-pixel alpha. If no surface is given, the new surface will be optimized for blitting to the current display.
Syntax: pygame.Surface.convert_alpha(Surface)
Code:
Python
# Importing the libraryimport pygame # Initializing Pygamepygame.init() # Creating the surfacesample_surface = pygame.display.set_mode((400,300))# changing the pixel format# of an image including per pixel alphaspygame.Surface.convert_alpha(sample_surface)# updating the displaypygame.display.flip() |
Output:
- pygame.Surface.copy: It creates a new copy of the surface. The duplicate surface will have the same pixel formats, color palettes, transparency settings, and class as the original.
Syntax: pygame.Surface.copy()
Code:
Python
# Importing the libraryimport pygame # Initializing Pygamepygame.init() # Creating the surfacesample_surface = pygame.display.set_mode((400,300))# creating a copy of sample_surface# and naming it as copied_surfacecopied_surface=pygame.Surface.copy(sample_surface)# updating the displaypygame.display.flip() |
Output:
- pygame.Surface.set_colorkey: Set the current color key for the surface. When blitting this surface onto a destination, any pixels that have the same color as the colorkey will be transparent.
Syntax: set_colorkey(Color, flags=0)
Code:
Python
# Importing the libraryimport pygame# Initializing Pygamepygame.init()# creating the display surfacedisplay_surface = pygame.display.set_mode((500, 500 ))# Creating the image surfaceimage = pygame.image.load('gfg_logo.png')# putting our image surface on display surface# making the white colored part# of the surface as transparentpygame.Surface.set_colorkey (image, [255,255,255])display_surface.blit(image,(100,100))# updating the displaypygame.display.flip() |
Output:
The output of the above code will be geeksforgeeks logo on a black surface with white-colored pixel changed to transparent.
- pygame.Surface.get_colorkey: It returns the current color key value for the Surface. If the color key is not set, then None is returned.
Syntax: get_colorkey()
Code:
Python
# Importing the libraryimport pygame# Initializing Pygamepygame.init()# creating the display surfacedisplay_surface = pygame.display.set_mode((500, 500))# Creating the image surfaceimage = pygame.image.load('gfg_logo.png')# putting our image surface on display surface# making the white colored part of the surface# as transparentpygame.Surface.set_colorkey(image, [255, 255, 255])# printing the colorkey value for the surfaceprint(pygame.Surface.get_colorkey(image))display_surface.blit(image, (100, 100))# updating the displaypygame.display.flip() |
Output:
The output of the above code will be a window showing various surfaces as seen in the get_colorkey example as well as the colorkey value will also be printed.
- pygame.Surface.set_alpha: The alpha value set for the full surface image. Pass 0 for invisible and 255 for fully opaque.
Syntax: set_alpha(value, flags=0) or set_alpha(None)
Code:
Python
# Importing the libraryimport pygame# Initializing Pygamepygame.init()# creating the display surfacedisplay_surface = pygame.display.set_mode((500, 500 ))# Creating the image surfaceimage = pygame.image.load('gfg_logo.png')# putting our image surface on display surface# making the alpha value of surface as 100pygame.Surface.set_alpha(image, 100)display_surface.blit(image,(100,100))# updating the displaypygame.display.flip() |
Output:
The output of the above code will be, geeksforgeeks logo, which will be slightly transparent as we have changed its alpha value to 100.
- pygame.Surface.get_alpha: It returns the current alpha value for the surface.
Syntax: get_alpha()
Code:
Python
# Importing the libraryimport pygame# Initializing Pygamepygame.init()# creating the display surfacedisplay_surface = pygame.display.set_mode((500, 500 ))# Creating the image surfaceimage = pygame.image.load('gfg_logo.png')# putting our image surface on display surface# making alpha value of image surface to 100pygame.Surface.set_alpha(image, 100)# printing the alpha value of the surfaceprint(pygame.Surface.get_alpha(image))display_surface.blit(image,(100,100))# updating the displaypygame.display.flip() |
Output:
The output of the above code will be a window showing various surfaces as seen in the set_alpha example as well as the alpha value will also be printed.


Please Login to comment...