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




