import pygame import random def distance(pos1, pos2): # a and b are TEMPORARY VARIABLES. They go away when the # function goes out of SCOPE. a = pos1[0] - pos2[0] b = pos1[1] - pos2[1] c = (a ** 2 + b ** 2) ** 0.5 return c # Send this value back to the caller pygame.display.init() pygame.font.init() screen = pygame.display.set_mode((800, 600)) font = pygame.font.SysFont("Courier New", 16) done = False target_pos = (400, 300) while not done: # INPUT evt = pygame.event.poll() if evt.type == pygame.QUIT: done = True elif evt.type == pygame.KEYDOWN: if evt.key == pygame.K_ESCAPE: done = True mpos = pygame.mouse.get_pos() # UPDATE d = int(distance(mpos, target_pos)) mid_x = (mpos[0] + target_pos[0]) // 2 mid_y = (mpos[1] + target_pos[1]) // 2 # DRAW screen.fill((0,0,0)) pygame.draw.line(screen, (255,255,255), mpos, target_pos) pygame.draw.circle(screen, (255,0,0), target_pos, 5) temps = font.render(str(d), False, (255,255,255), (32,32,32)) screen.blit(temps, (mid_x - temps.get_width() // 2, mid_y - temps.get_height() // 2)) pygame.display.flip() pygame.font.quit() pygame.display.quit()