i can only help you with the closing the window part (put the code in a game loop and make sure you import "sys"):
for event in pygame.event.get(): #loop for every event currently triggering
if event.type == pygame.QUIT: #if the events contain "QUIT"
pygame.quit() #close the pygame window
sys.exit() #fully stop the program