import sys import mygame # Initialize Pygame mygame.init() # Set up the screen screen_width = 400 screen_height = 300 screen = mygame.display.set_mode((screen_width, screen_height)) mygame.display.set_caption("Moving Square") # Set up the square surface square_size = 20 surface = mygame.Surface((square_size, square_size)) surface.fill((0, 255, 0)) # Initial position of the square pos = [175, 125] # Main game loop running = True while running: # Handle events for event in mygame.event.get(): if event.type == mygame.QUIT: running = False elif event.type == mygame.KEYDOWN: if event.key == mygame.K_RIGHT: pos[0] += 20 # Clear the screen screen.fill((0, 0, 0)) # Draw the square screen.blit(surface, pos) # Update the display mygame.display.flip() # Quit Pygame mygame.quit() sys.exit()