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
import pygame
import time
pygame.init()
sample_surface = pygame.display.set_mode(( 400 , 300 ))
color = ( 255 , 255 , 0 )
pygame.draw.rect(sample_surface, color,
pygame.Rect( 30 , 30 , 60 , 60 ))
pygame.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
import pygame
pygame.init()
sample_surface = pygame.display.set_mode(( 400 , 300 ))
color = ( 255 , 255 , 0 )
sample_surface.fill(color)
pygame.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
import pygame
pygame.init()
display_surface = pygame.display.set_mode(( 500 , 500 ))
image = pygame.image.load( 'gfg_logo.png' )
display_surface.blit(image,( 100 , 100 ))
pygame.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
import pygame
pygame.init()
display_surface = pygame.display.set_mode(( 500 , 500 ))
image1 = pygame.image.load( 'gfg_logo.png' )
image2 = pygame.image.load( 'gfg_logo.png' )
display_surface.blit(image1,( 0 , 0 ))
display_surface.blit(image1,( 300 , 300 ))
pygame.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
import pygame
pygame.init()
sample_surface = pygame.display.set_mode(( 400 , 300 ))
pygame.Surface.convert(sample_surface)
pygame.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
import pygame
pygame.init()
sample_surface = pygame.display.set_mode(( 400 , 300 ))
pygame.Surface.convert_alpha(sample_surface)
pygame.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
import pygame
pygame.init()
sample_surface = pygame.display.set_mode(( 400 , 300 ))
copied_surface = pygame.Surface.copy(sample_surface)
pygame.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
import pygame
pygame.init()
display_surface = pygame.display.set_mode(( 500 , 500 ))
image = pygame.image.load( 'gfg_logo.png' )
pygame.Surface.set_colorkey (image, [ 255 , 255 , 255 ])
display_surface.blit(image,( 100 , 100 ))
pygame.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
import pygame
pygame.init()
display_surface = pygame.display.set_mode(( 500 , 500 ))
image = pygame.image.load( 'gfg_logo.png' )
pygame.Surface.set_colorkey(image, [ 255 , 255 , 255 ])
print (pygame.Surface.get_colorkey(image))
display_surface.blit(image, ( 100 , 100 ))
pygame.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
import pygame
pygame.init()
display_surface = pygame.display.set_mode(( 500 , 500 ))
image = pygame.image.load( 'gfg_logo.png' )
pygame.Surface.set_alpha(image, 100 )
display_surface.blit(image,( 100 , 100 ))
pygame.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
import pygame
pygame.init()
display_surface = pygame.display.set_mode(( 500 , 500 ))
image = pygame.image.load( 'gfg_logo.png' )
pygame.Surface.set_alpha(image, 100 )
print (pygame.Surface.get_alpha(image))
display_surface.blit(image,( 100 , 100 ))
pygame.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.
Last Updated :
29 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...