Game Development 9 min read

Python Pygame Missile Auto‑Tracking Algorithm Tutorial

This article explains how to implement a missile auto‑tracking system in a 2D shooting game using Python and Pygame, covering the underlying differential‑equation‑based algorithm, step‑by‑step calculations, handling of coordinate systems, and code for rotation and rendering.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Pygame Missile Auto‑Tracking Algorithm Tutorial

The tutorial introduces an automatic missile tracking algorithm commonly used in shooting games, which mathematically solves a differential equation to determine the missile's direction and displacement at each small time slice.

By dividing time t into tiny intervals (e.g., 1/1000 s), the algorithm constructs a right‑angled triangle for each slice, computes the angle a using sine and cosine derived from the distance between missile and target, and updates the missile's position with AD = vt·cos(a) and CD = vt·sin(a) . The target moves independently, so the process repeats for the next slice.

Because Pygame’s coordinate system has the y‑axis pointing downwards, the same convention is used throughout the calculations. The article also discusses the challenges of rotating the missile sprite: rotation changes the image size and the tip position, requiring an offset correction based on the rotated image’s bounding box.

Key code snippets are provided below. All code lines are kept intact and wrapped in tags.</p><pre><code>import pygame, sys from math import * pygame.init() screen = pygame.display.set_mode((800, 700), 0, 32) missile = pygame.image.load('element/red_pointer.png').convert_alpha() x1, y1 = 100, 600 # missile start position velocity = 800 # missile speed time = 1/1000 # time slice length 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 is mouse position distance = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2)) section = velocity * time # distance moved each slice 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 # change in direction old_angle = degrees(angle) missiled = pygame.transform.rotate(missile, -d_angle) # compute corrected blit position based on rotated image size (omitted for brevity) screen.blit(missiled, (x1 - missile.get_width(), y1 - missile.get_height() / 2)) pygame.display.update() Additional sections show how to calculate the rotated image’s tip position for the four quadrants, adjust the blit coordinates accordingly, and finally render the missile with its tip accurately following the mouse cursor. The complete source code is provided at the end of the article, enabling readers to reproduce the missile‑tracking demo and adapt the technique to their own games.

AlgorithmPythonGame developmentphysics simulationPygameMissile Tracking
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login 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.