# Homework 34, problem 3: follow the mouse. # # A square moves toward the current mouse position each frame. It does not # jump to the cursor; it moves a fixed number of pixels toward it per frame, # stopping when it arrives. Compute the difference in x and y first. # # Run with: python exercises/34/homework/03-follow-mouse.py import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Follow Mouse") clock = pygame.time.Clock() rect = pygame.Rect(370, 270, 50, 50) speed = 5 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # TODO: read pygame.mouse.get_pos() and move rect toward it by speed screen.fill((20, 20, 20)) pygame.draw.rect(screen, (255, 200, 50), rect) pygame.display.flip() clock.tick(60) pygame.quit()