PyGame can be defined as an ultraportable open source Python library used to create games and multimedia applications based on the SDL library.
To draw, we need a window. To create such a window, call the set_mode () function in the example below. This function receives as argument a tuple with 2 elements representing the resolution of the window.
import pygame
screen = pygame.display.set_mode ((800, 600))
To develop graphical applications, we need an infinite loop explicitly created by the developer. Thus, at each step of the loop, we can modify the status of the program according to the user’s input and the events triggered by various components of the program.
import pygame
screen = pygame.display.set_mode ((800, 600))
running = True
while running:
# user events processing
# output event check (eg if the user presses the Esc key or the X button
if event_section ():
running = False
# change the current status of the game
# Drawing the current state of the modified game in the previous step
# You need to call the flip () function to draw what has been changed in the previous step
pygame.display.flip ()
pygame.quit ()
To draw graphical elements in PyGame, we use pygame.draw
As you can see in the previous link, there are several forms supported by pygame. As an example, we will draw a rectangle:
# – * – coding: utf-8 – * –
import pygame
screen = pygame.display.set_mode ((800, 600))
# pygame.draw.rect (Surface, color, Rect, width = 0) -> Rect
# As you can see from the signature of the draw.rect () function, it
# Receives:
# – a surface on which to draw the rectangle
# – a color, in RGB format
# – a Rect object
# Rect (left, top, width, height) -> Rect
#
pygame.draw.rect (screen, (255, 0, 0), pygame.Rect (120, 120, 200, 100))
# We show changes on the screen
pygame.display.flip ()
# We wait 3000 ms to see the drawing result
pygame.time.wait (3000)
To add an image, we do the same, but first we load the image into memory.
# – * – coding: utf-8 – * –
import pygame
screen = pygame.display.set_mode ((800, 600))
# Upload the image to memory
image = pygame.image.load (‘image.png’)
# Transfer image pixels to the window
screen.blit (image, (0, 0))
pygame.display.flip ()
# We wait 3000 milliseconds again to see the drawing
pygame.time.wait (3000)
Input from user
# – * – coding: utf-8 – * –
import pygame
screen = pygame.display.set_mode ((800, 600))
running = True
while running:
for event in pygame.event.get ():
# Our exit path, triggered when we close the window.
if event.type == pygame.QUIT:
running = False
break
if event.type == KEYDOWN:
if event.key == K_UP:
print ‘sageata up’
elif event.key == K_DOWN:
print ‘down arrow’
elif event.key == K_LEFT:
print ‘left arrow’
elif event.key == K_RIGHT:
print ‘right arrow’
elif event.key == K_SPACE:
print ‘space’
pygame.quit ()
PyGame is a graphical engine. A graphics engine is a system designed for creating and developing video games. Several game engines are designed to work on video game consoles and personal computers. The basic functionality typically provided by a graphics engine includes a rendering engine for 2D or 3D graphics, a physics or collision detection engine (and collision response), sound, scripting, animation, artificial intelligence, network, streaming, management memory, location support, etc. The game development process is often saved, in large part, by the reuse/adaptation of a similar engine to create different games.