Introduction
In this article, we will learn what is Pygame Surface, and how to create a surface, and we will also create a surface using the functions of pygame. The surface plays a big role in Pygame's representation on the screen. Any image, or object you create in Pygame will be created with the help of Surface. The most widely used object in Pygame is created using pygame.display.set_mode.
displaysurface = pygame.display.set_mode((400,600))
In the above code, display.set.mode(width, height) is used to define the width and height of the window(screen).
Creating a Surface
Creating surfaces in a pygame is very easy. We just have to pass the length and width with a tuple in the pygame.Surface () method. We can use various methods to shape our faces the way we want. For example, we can use pygame.draw () to draw shapes, we can use the surface.fill () method to fill in the surface.
Syntax: pygame.surface()
It takes four arguments a tuple of width and height, flags, depth, mask.
mySurface = pygame.Surface((50, 50))
mySurface2 = pygame.Surface((50, 100))
pygame.draw()
It is used to draw a shape.
Syntax: Surface.rect(surface, color, rect)
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption("coding ninjas")
gameDisplay.fill(black)
pixAr = pygame.PixelArray(gameDisplay)
pixAr[10][20] = green
pygame.draw.line(gameDisplay, blue, (200,200), (200,250),5)
pygame.draw.rect(gameDisplay, red, (300,300,60,55))
pygame.draw.circle(gameDisplay, white, (250,250), 95)
pygame.draw.polygon(gameDisplay, green, ((15,65),(86,125),(250,375),(400,25),(60,540)))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
Output
Fill() function
The fill () function takes a color object and "fills" the surface with that color.
SCREEN.fill(RED)
This fill function will set the color of the screen to red.
Blit ()function
The blit () object-held method is used to draw more than one object.
Syntax
surface.blit(source, destination)
This function usually takes two parameters. The first is the location of the source to be drawn in the area named for this method. This could be a pair of links representing the source's upper left corner or a side object where the upper left corner of the rectangle will be used as a blit lock.
displaysurface.blit(mySurface, (50,50))
Let's look at the code above. Here, displaysurface is an area that represents the entire screen. Its size determines the size of the pygame window. Remember, however, until the pygame.display.update () function is called, there will be no changes to the name blit () function which will appear on the screen.
Below is the full code of the Pygame Surface creation demonstration. It combines code and concepts from the functions described earlier and combines them into a single final program.
import pygame
from pygame.locals import (
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.surf = pygame.Surface((95, 95))
self.surf.fill((250, 250, 250))
self.rect = self.surf.get_rect()
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("coding ninjas")
player = Player()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
screen.fill((0, 0, 0))
screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
pygame.display.flip()
pygame.quit()
Output

There are a number of different functions associated with objects in Surface. However, we will not include them here, except for a few that we thought would be very helpful:
pygame.Surface.copy() is a function used to create a direct copy of another Surface object with the same color, transparency, etc.
pygame.Surface.get_size() returns the width and height of the object in Surface.
pygame.Surface.get_width() and pygame.Surface.get_height() helps in restoring the width and height of the Top.
pygame.Surface.set_alpha: The alpha value set for the full surface image. It will Pass 0 for invisible and pass 255 for fully opaque.
pygame.Surface.get_colorkey: It returns the current value of the color to Surface. If the color key is not set, then nothing will get returned.



