import pygame pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Moving Rectangle") clock = pygame.time.Clock() BG = (20, 20, 20) BLUE = (60, 120, 220) x = 0 y = 200 speed = 3 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # move the rectangle to the right each frame x += speed # wrap around when it goes off screen if x > 640: x = -60 screen.fill(BG) pygame.draw.rect(screen, BLUE, (x, y, 60, 40)) pygame.display.flip() clock.tick(60) pygame.quit()