First game window in Pygame and Python

First game window in Pygame and Python

Hello people, this is going to be our first tutorial in which we will learn how to create our first game window in Pygame and Python.

So, the first step is to open VS Code and create a python file inside your directory.

1. Click on Create File:

2. Name your file with .py extension in the end

3. Now write ‘import pygame’ and run it.

If you see ‘ pygame 1.9.6
Hello from the pygame community.‘ Then you are on right track, else there might be some problem with your installation. Check out our previous article on Getting Started with Pygame.

Now let’s learn!

Importing the library:

To import pygame to our python file, we use the ‘import’ keyword and then the name of the package/library.

# Importing Pygame
import pygame

Initializing Pygame:

So, in order to access all the methods and functions of pygame, we need to initialize it first.

To initialize Pygame, we need to write the following code.

# Here init() stands for initialization of the package

This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand.

Creating windows screen:

If you run the above code, it won’t do anything except displaying a hello message from Pygame Community. To create our game, we need a surface to put our game objects. So that’s why we create a window or screen. By Window/Screen I mean that it is the surface where we will put our game objects and run our game.

So, to create our screen we require the display module from pygame, because display module controls the display window and screen. And inside display module, we need to access the set_mode() function to initialize the window or screen for display.

set_mode() takes 5 arguments. And below is it’s Syntax:

set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0)

This function will create a display Surface. The arguments passed in are requests for a display type. The actual created display will be the best possible match supported by the system.

The size argument is a tuple of a pair of numbers representing the width and height. The flags argument is a collection of additional options. The depth argument represents the number of bits to use for colour.

The Surface that gets returned can be drawn to like a regular Surface but changes will eventually be seen on the monitor. We can simply create the window and show it but we need to customize it in the further. So, we will store it in a variable for our ease.

To create the screen, we need to write the following code:

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

But now if you run it. The screen will just appear for an instant and then disappear. Well that is because there is nothing else in the program that will keep it running so after it displays the screen, there is nothing else to execute so it terminates and so does the screen.

To keep the screen remain on our monitor, We will run a while loop after the above code. But don’t run it instantly after adding while loop that is given below, because there is nothing there in the program that will stop the while loop, so your screen would look like that it is hanged and nothing will work on it, not even the close button, in that case, you may need your task manager to kill it. So to prevent that, we use events. We will learn more about them in the future articles but for now, you need to know that events in pygame are the activities that are happening on the screen/window like clicking, moving the cursor, keypress, etc.

So after adding the while loop, your program must be looking like this:

import pygame

# So as we want our display screen to be of size 800px-600px
screen = pygame.display.set_mode((800,600))


# But this will hang your screen so don't run it.

Instead, we add an if statement that will check all the events that are happening on our screen and if the close button is clicked, then it returns False that will break the loop and terminate the program.

So we will also create a boolean variable that takes True or False to check if the program is running or not. Example: If isRunning == True; then keep looping else if isRunning == False; that will break the while-loop.

So, consider the following snippet of code and try to understand it.

import pygame


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

isRunning = True

while(isRunning ==True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            isRunning = False

Explanation of the above code:

isRunning is a boolean variable that takes only True and False. And in the while-loop, before entering inside it we have added a statement isRunning == True. It checks if the value of isRunning is True or not, if it is then loop through else break the loop and move out of it.

Inside the while-loop we have a for-loop that iterates through the events that are happening inside the game window with the help of the statement pygame.event.get() and stores it inside a variable called event. And then inside the for-loop, we have an if statement that checks if the type of event that happened on the screen was a Quit type of event. (QUIT type events occur when the user clicks on the close button). And if the type of event that happened and pygame.QUIT event are equal then set isRunning = False, that will break the while-loop after the execution that is required further in the loop is completed, else it keeps game screen/window running.

Your final code window after running the program must be looking like this:

So, now you know how to create a display screen/window for your game in python using the pygame library. Now we will learn how to customize our display screen/window in the next article.

Thanks for reading

Keep learning

If you found something wrong in this article, please let us know

Also Read:

What is Pygame? Full Introduction

Getting Started with Pygame

Snake Game in Python using Pygame

Rock Paper Scissor Game Using Python Tkinter

Guess the Number Python

Tic Tac Toe Python

Sudoku solver Python |Backtracking|Recursion


Author: Ayush Purawr