Table of contents
1.
Introduction
2.
The Empty PyGame Window
3.
How to determine the size of the PyGame Window
4.
Allowing window resizing in PyGame
5.
How do I change a Pygame window's name?
6.
Frequently Asked Questions
6.1.
Is pygame a graphical user interface?
6.2.
How do I get pygame to run on Windows?
6.3.
Is it possible to combine Python and Tkinter?
6.4.
What is the purpose of pygame?
7.
Conclusion
Last Updated: Mar 27, 2024

Window in PyGame

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

Introduction

Pete Shinners wrote Pygame, a Python wrapper for SDL, a cross-platform C library for controlling multimedia. You can use pygame to write video games or other multimedia applications in Python that will run on SDL's supported platforms without modification (Windows, Unix, Mac, BeOS, and others).

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

How to determine the size of the PyGame Window

Game programming is enriching these days, and it can also be used for advertising and education. Game development involves math, logic, physics, artificial intelligence, and much more, and it can be a lot of fun. Game programming in Python is done with pygame, one of the best modules.

The following command can be used to install this library:

//how to get pygame window height size
//Python
import pygame
pygame.init()


window = pygame.display.set_mode((500, 500))
x, y = window.get_size()


print("The width is %s" %x)
print("The height is %s" %y)


//pygame how to get the surface length


screen = pygame.display.set_mode((700, 500))
#get 2 variables for width and height
x, y = pygame.display.get_surface()
#get width
x = screen.get_width()
#get height
y = screen.get_height()


//pygame surface


# blitting a surface into another surface
screen.blit(my_image, (0, 0))
pygame.display.update() # or pygame.display.flip()

Allowing window resizing in PyGame

Set mode() should not be called in pygame because the display surface is automatically resized when the window is. The VIDEORESIZE event, on the other hand, allows us to resize any other surfaces or objects.


import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500, 500), RESIZABLE)
pic = pygame.image.load("example.png")  # You need an example picture in the same folder as this file!
running = True
while running:
    pygame.event.pump()
    event = pygame.event.wait()
    if event.type == QUIT:
        running = False
    elif event.type == VIDEORESIZE:
        screen.blit(pygame.transform.scale(pic, event.dict['size']), (0, 0))
        pygame.display.update()
    elif event.type == VIDEOEXPOSE:  # handles window minimising/maximising
        screen.fill((0, 0, 0))
        screen.blit(pygame.transform.scale(pic, screen.get_size()), (0, 0))
        pygame.display.update()

How do I change a Pygame window's name?

PyGame is a simple window that shows our game on the computer's screen. Pygame window's default title and logo are "Pygame window" and "pygame icon," respectively. The set caption() and set icon() functions can change the window's name and icon.

To rename the pygame window, follow these steps:

Syntax: pygame.display.set_caption('Title of window')

 

Go to: to change the icon of the pygame window.

Syntax: pygame.display.set_icon(Icon_name)

 

Implementation in Steps:

Step 1: We import and initialize all imported modules first. 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.display.set_mode((width_of_window,height_of_window))


Step 3: The default title and icon of the pygame window are changed using the.set caption() and.set icon() functions.We use pygame.image to load the icon image to change the icon. After that, we use the.set icon() function to change the default image.

pygame.display.set_caption('CodingNinjas')

Icon = pygame.image.load('gfglogo.png')

pygame.display.set_icon(Icon)

 

Step 4: Keep that window open until the user chooses to close it. Unless the user presses the quit button, we use a true variable. We use a while loop to keep the game running and check whether our variable is true or not.

running = True

while running:  
    for event in pygame.event.get():  
        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, which is 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 getting started with game development by creating a template for controlling a Python game's main loop. 

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!

Thank you for reading.

Live masterclass