How to make a PyGame Window?

The very first step to creating your Pygame application is to make the Pygame Window. This is the window to which you will be drawing and rendering various shapes, sprites, text and other objects.

Let’s explore how we can make this Window and use the various flags and settings related to it.


Setting up Pygame

The first step to creating the Pygame Window is to actually install the Pygame library. Once you have done this, you can begin writing Pygame code by importing the pygame module.

import pygame

The next step is the call pygame.init(). This function initializes the various modules within Pygame and makes them ready for use.

pygame.init()

Now we can begin writing proper pygame code.


Creating the Pygame Display

The very first thing we will do is create our display/window using the pygame.display.set_mode() function. Shown below is the syntax for this function.

pygame.display.set_mode(size=(0, 0), flags=0) 

The first parameter is the size of the screen that you want in the form of a tuple. The second allows us to pass in special flags which modify the behavior of the screen.

screen = pygame.display.set_mode((800, 600))

The above code creates a 800 pixels wide, and 600 pixels tall display screen. Do note that we need to save the returned screen object as a variable, which will be used later for drawing to the screen.

On newer pygame versions, using (0, 0) as the parameter for size will make the Display size equal to your Screen resolution. So if you have a 1920 x 1080 screen, those will be the dimensions of your pygame display.

Now let’s explore some flags that we can use.

  1. pygame.FULLSCREEN: This flag opens the Display in Full screen mode.
  2. pygame.RESIZABLE: Allows the Pygame display to be resized
  3. pygame.HIDDEN: Can be used to keep the pygame window “hidden” when it is first created.
  4. pygame.SHOW: Used to make the window show itself after it has been hidden.
  5. pygame.NOFRAME: Removes the “frame” around the screen, making it borderless.

You can pass in multiple flags at once by separating them using the pipe operator.

screen = pygame.display.set_mode((800, 600), 
                                 flags=pygame.FULLSCREEN | 
                                 pygame.NOFRAME)

Do not try running to create and execute the screen code without creating the game loop first. Otherwise you will not be able to close the window properly. This is because the QUIT event needs to be handled in a specific manner.


The Complete Code

The below code is a complete example for this tutorial. You can run it safely as it has handled the QUIT event for the pygame window. (Use the Backspace key to exit the program once it opens up in full screen)

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((800, 600), 
                                 flags=pygame.FULLSCREEN | 
                                 pygame.NOFRAME)

while True:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_BACKSPACE:
                exit()
    
    pygame.display.update()

If this is your first time hearing about the Game Loop concept, refer to our tutorial on it for more information.


This marks the end of the How to make a PyGame Window? Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments