Table of contents
1.
Introduction
1.1.
PyGame's window
1.2.
Putting the window shut
1.3.
Changing the properties of the window
1.4.
The final presentation
2.
The Empty PyGame Window
3.
Frequently Asked Questions
3.1.
Is pygame a graphical user interface?
3.2.
How do I get pygame to run on Windows?
3.3.
Is it possible to combine Python and Tkinter?
3.4.
What is the purpose of pygame?
4.
Conclusion
Last Updated: Mar 27, 2024

Creation of An Empty PyGame Window

Author Palak Mishra
0 upvote

Introduction

SDL stands for Simple DirectMedia Layer and PyGame is a Python wrapper for it. SDL gives you cross-platform access to your system's multimedia hardware, including sound, video, the mouse, keyboard, and joystick. PyGame began as a replacement for the PySDL project, which had become stalled. Because SDL and PyGame are cross-platform, you can use them to create games and rich multimedia Python programs on any platform that supports them.

PyGame's window

The Pygame module will be used, so the first step is to import it.

import pygame

 

We can now use the pygame.display.set mode to create a Pygame window object (which I've named'screen') (). This object needs two values to define the window's width and height. We'll define two variables, width, and height, rather than using constants to make the program easier to change later. Use any integers that you want. The flip() function is used to display the window:

(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()

 

A 300 x 200 pixel window will appear and then disappear when you run this program now. The issue is that once the flip() function is called, the program ends because it reaches the end of the code.

We must ensure that the program does not end to keep the screen visible for as long as we desire. An infinite loop could be used to accomplish this.

 while True:
         pass

 

An infinite loop has the disadvantage of never-ending by definition. Even if we try, the program will not shut down. Nothing happens if we click the X to close the window. Press Ctrl + C in the command line to force the program to complete.

Putting the window shut

Our window should remain open until the user closes it. We do this by using pygame.event.get to track user inputs (also known as "events") (). This function returns a list of events that we can loop through and check for QUIT events. If such an event occurs, we exit the loop by changing a boolean variable ('running').

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

 

The window will now stay open as long as the value of 'running' is True, which it will be until you close it (by clicking the X). It's worth noting that using an IDE for Python programming may cause Pygame to malfunction. This isn't usually a big deal, but it can prevent the Pygame window from properly closing. If this is the case, using pygame.quit() should fix the issue.

Changing the properties of the window

We can now change the properties of the usable window. The set caption() function, for example, can be used to change the title.

pygame.display.set_caption('Tutorial 1')

 

By filling the screen object, we can change the background color. The red, green, and blue values are represented by a 3-tuple of integers ranging from 0 to 255. For instance, white (255,255,255). Before calling the flip() function, some changes must be made.

background_colour = (255,255,255)
screen.fill(background_colour)

 

The final presentation

After a little rearranging, the entire program should now look like this:

import pygame
 
background_colour = (255,255,255)
(width, height) = (300, 200)
 
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
 
pygame.display.flip()
 
running = True
while running:
 for event in pygame.event.get():
   if event.type == pygame.QUIT:
     running = False

 

When you run the program, you should see something like this (on Windows XP).

It's not particularly exciting, but we now have a window that will remain open until we close it.

Output Window:

The Empty PyGame Window

The Pygame window is a standard window where we display our game screen. It's the first thing we do to get our output onto something. The main goal is to create a window and keep it open until the user closes it. First, we must install the pygame package and import some pre-defined functions.


Installation

To install this module, run the following command in the terminal.

pip install pygame

 

Implementation in Steps:

Step 1: All imported modules are first imported and initialized. All modules are imported using import pygame, and the.init() function is used to initialize them.

import pygame
pygame.init()

 

Step 2: Create a window that will be displayed. To make a window, we use the.set mode() function. The width and height of our window are passed to the set_mode() function as parameters.

pygame. the display.set_mode((width_of_window,height_of_window))

 

Step 3: Leave the window open until the user closes it. Unless the user presses the quit button, a variable is set to true. We use a while loop to keep the game running and check whether or not our variable is true.

running = True


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


The entire code is as follows:

 

Python 3

# import pygame package
import pygame
  
# initializing imported module
pygame.init()
  
# displaying a window of height
# 500 and width 400
pygame.display.set_mode((400, 500))
  
# creating a bool value that checks
# if game is running
running = True
  
# While running, keep the game running until it is true::
      
    # Check for event if user has pushed
    # any event in queue
    for event in pygame.event.get():
          
        # if event is of type quit then 
        # set running bool to false
        if event.type == pygame.QUIT:
            running = False

Frequently Asked Questions

Is pygame a graphical user interface?

Pygame GUI is a module that allows you to create graphical user interfaces for pygame games. The module is focused on the future, and it is compatible with Pygame 2 and Python 3.

How do I get pygame to run on Windows?

Double-click the .bat file to start the program. Your pygame window should appear after a black console window appears. The console window will remain open after you close the pygame window, with the message "Hit any key to continue...".

Is it possible to combine Python and Tkinter?

HARMONY between PYGAME and TKINTER Despite what some forums claim, Tkinter, a lightweight Python GUI, works well with pygame, the Python SDL layer, at least when pygame is not in full-screen mode.

What is the purpose of pygame?

The pygame library is an open-source module for the Python programming language designed to aid in developing games and other multimedia applications. Pygame, based on the SDL (Simple DirectMedia Layer) development library, can run on various platforms and operating systems.

Conclusion

This blog walked you through creating an empty pygame window, renaming it for use in your Python 2 or Python 3 programming environment, and starting game development by creating a template for controlling a Python game's main loop. Here we have extensively discussed the topic of PyGame Window. 

We hope this blog has helped you learn more about PyGame, and if you want to learn more, see our other Pygame articles. Still, the learning never stops, have a look at more related articles PygletBasic Of PythonPython practice questionsSQL, and many more. Please vote for our blog to help other ninjas succeed.

A ninja never stops learning, so to feed your quest to learn and become more advanced and skilled, head over to our practice platform Coding Ninjas Studio to practice advanced-level problems. Attempt 100 SQL problems, read interview experiences, and much more!

Happy Coding!

Live masterclass