Game Development 9 min read

How to Build a Missile Auto‑Tracking System in Python with Pygame

This article explains the mathematics and step‑by‑step Python implementation of a missile auto‑tracking algorithm for shooting games, covering time‑slice integration, angle calculation, coordinate updates, image rotation handling, and provides complete, runnable Pygame code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build a Missile Auto‑Tracking System in Python with Pygame

Automatic tracking algorithms are often used in shooting games; they can be derived from solving differential equations.

By dividing time into small slices (e.g., 1/1000 s) and constructing right‑angled triangles for each slice, we compute the missile’s direction (angle a) and travel distance (vt = |AC|). The target moves during each slice, so the next slice starts from the new point.

Assuming the missile starts at (x1, y1) and the target at (x, y), we build triangle ABE to obtain sin a and cos a, then calculate the incremental moves AD = vt·cos a and CD = vt·sin a.

The distance between two points gives the hypotenuse, and the angle is obtained with atan2. Updating the missile’s coordinates each slice yields a smooth pursuit path.

In Pygame the y‑axis points downwards, so the same coordinate system is used. The basic implementation is shown below.

import pygame, sys
from math import *
pygame.init()
screen = pygame.display.set_mode((800, 700))
missile = pygame.image.load('element/red_pointer.png').convert_alpha()
x1, y1 = 100, 600          # missile start
velocity = 800              # speed
time = 1/1000               # time slice
clock = pygame.time.Clock()
old_angle = 0
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    clock.tick(300)
    x, y = pygame.mouse.get_pos()          # target = mouse
    distance = sqrt((x1 - x) ** 2 + (y1 - y) ** 2)
    section = velocity * time
    sina = (y1 - y) / distance
    cosa = (x - x1) / distance
    angle = atan2(y - y1, x - x1)
    x1, y1 = x1 + section * cosa, y1 - section * sina
    d_angle = degrees(angle) - old_angle
    old_angle = degrees(angle)
    screen.blit(missile, (x1 - missile.get_width(), y1 - missile.get_height() / 2))
    pygame.display.update()

When rotating the missile image, the rotation centre shifts and the image size changes, causing the tip to drift. By calculating the rotated image’s tip position (the green arrow in the diagrams) and adjusting the blit coordinates accordingly, the missile tip stays aligned with the calculated trajectory.

Four quadrant cases are handled separately to compute the correct tip offset, and the final blit uses the corrected coordinates:

screen.blit(missiled, (x1 - width + (x1 - C[0]), y1 - height/2 + (y1 - C[1])))

The complete, working code is provided, demonstrating a functional missile auto‑tracking demo in Pygame.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Game DevelopmentrotationPygameMissile Tracking
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.