Table of contents
1.
Introduction
2.
Creating a Surface
2.1.
pygame.draw()
2.2.
Fill() function
2.3.
Blit ()function
3.
Frequently Asked Questions
3.1.
How do you show more space in a pygame?
3.2.
Why is Python mainly used for making games?
3.3.
What is a Blit pygame?
4.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Pygame surface

Author Ashish Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.
You can also try this code with Online Python Compiler
Run Code

 

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)
You can also try this code with Online Python Compiler
Run Code

 

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)
You can also try this code with Online Python Compiler
Run Code

 

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)
You can also try this code with Online Python Compiler
Run Code

 

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))
You can also try this code with Online Python Compiler
Run Code

 

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()
You can also try this code with Online Python Compiler
Run Code

 

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.

Frequently Asked Questions

How do you show more space in a pygame?

Create an object over the image i.e. the object over which the image is drawn, using the image. load () pygame method. Copy the object over the image into the top of the display using the blit () method of the pygame display surface object. Display the above item displayed in the pygame window using the display.

Why is Python mainly used for making games?

Pygame Library is an open-source software module for Python program designed to help you play games with other multimedia applications. Built on the most portable SDL (Simple DirectMedia Layer) development library, pygame can work in all forums and applications.

What is a Blit pygame?

A small wrap around the Pygame area allows you to easily draw pictures on the screen (“off”). Classroom screen location. Pygame green area representing screen bath. You can apply this to the performance of advanced images.

Conclusion 

In this article, we have extensively discussed the pygame surface, the creation of surfaces, and some of their functions. We start with a brief introduction of the pygame surfaces, then discuss the creation of surfaces.

After reading about the pygame surfaces, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. To learn, see Operating SystemUnix File SystemFile System Routing, and File Input/Output.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass