Implementing Automatic Missile Tracking in a Shooting Game Using Python and Pygame
This article explains the mathematical basis and step‑by‑step Python/Pygame implementation of an automatic missile‑tracking algorithm for a shooting game, covering time‑slice integration, angle calculation, image rotation handling, and provides complete source code for a functional demo.
The article introduces an automatic missile‑tracking algorithm commonly used in shooting games, explaining that it is essentially a numerical solution to a differential equation rather than a military technique.
By dividing time into very small slices (e.g., 1/1000 s), the algorithm constructs a right‑angled triangle for each slice to compute the missile's direction (angle α) and travel distance (vt = |AC|). Using the target’s updated position for each slice, the missile’s new coordinates are calculated with trigonometric relations: AD = vt·cos(α) and CD = vt·sin(α).
The necessary geometry is derived from the distance formula and the sine/cosine of the angle between the missile and the target. The article provides the key formulas and visual illustrations of the triangles used in the computation.
Implementation in Python uses the pygame library. The core loop obtains the mouse position as the target, computes distance, angle, and updates the missile’s position each frame. Sample code for the basic version (without rotation) is shown:
<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 # initial missile position
velocity = 800
time = 1/1000
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()
distance = sqrt(pow(x1 - x, 2) + pow(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()
</code>When rotating the missile image, the article discusses the problem that rotation changes the image’s bounding box and shifts the tip, causing misalignment with the calculated missile head. To fix this, the rotated image’s tip coordinates are computed for each quadrant, and the blit position is adjusted by the offset between the original tip and the rotated tip.
The final corrected code handles four angle ranges, calculates points A, B, and C for the rotated image, and draws the missile so that its tip follows the target accurately. The complete, refined source code is provided in the article.
Overall, the tutorial combines differential‑equation‑based motion modeling, trigonometry, and practical Pygame techniques to create a functional auto‑tracking missile in a 2D shooting game.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.