Thanks to the Pygame library we installed in the previous post, we can draw 2D graphics. In this post, I propose to introduce controls with the keyboard as well as some improvements like window centering and frame rate handling.
This post is part of the Discover Python and Patterns series
Keyboard events
Pygame events allow the capture of keyboard presses:
import pygame
pygame.init()
window = pygame.display.set_mode((640,480))
x = 120
y = 120
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
elif event.type == pygame.KEYDOWN:
if event.key …