# Importing the Pygame library for building games and multimedia applications. import pygame # Importing the sys module to exit the program. import sys # Initializing all Pygame modules. pygame.init() # Creating a window surface with a size of 400x300 pixels. screen = pygame.display.set_mode((400, 300)) # Creating a background surface with the same size as the window. background = pygame.Surface((400, 300)) # Creating a surface with a size of 10x10 pixels. surface = pygame.Surface((10, 10)) # Filling the surface with a green color. surface.fill((0,255,0)) # Setting the initial position for the surface to be displayed. pos = [175, 125] # Main game loop that runs indefinitely until the program is exited. while True: # Getting all the events that have occurred. events = pygame.event.get() # Iterating through each event. for event in events: # If the event is the user closing the window... if event.type == pygame.QUIT: # ...quit Pygame and exit the program. pygame.quit() sys.exit() # If the event is a key press... if event.type == pygame.KEYDOWN: # ...and the pressed key is the right arrow key... if event.key == pygame.K_RIGHT: # ...move the surface 20 pixels to the right. pos[0] += 20 # Drawing the background on the window at position (0,0). screen.blit(background, (0,0)) # Drawing the surface on the window at the current position. screen.blit(surface, pos) # Updating the display to show the changes. pygame.display.update()