Implementing an Automatic Missile Tracking Algorithm in Python with Pygame
This article explains the mathematical basis and step‑by‑step implementation of an automatic missile tracking algorithm for shooting games, using Python’s pygame library, covering differential‑equation‑derived motion, angle calculations, image rotation handling, and complete source code.
Automatic missile tracking is often used in shooting games; it can be derived from solving differential equations and applying simple trigonometry.
The algorithm divides time into small intervals (e.g., 1/1000 s), constructs a right‑triangle for each slice, computes the missile’s direction angle ∠a and travel distance vt = |AC|, then updates the missile’s position for the next slice.
Using the distance formula, sin a and cos a are obtained from the relative positions of the missile and the target, and the new coordinates are calculated as AD = vt·cos a and CD = vt·sin a.
In pygame the coordinate system has the y‑axis pointing downwards, so the same convention is used. The following Python code demonstrates the basic implementation without image rotation:
<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
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)
screen.blit(missile, (x1-missile.get_width(), y1-missile.get_height()/2))
dis_angle = d_angle - old_angle
old_angle = d_angle
pygame.display.update()</code>To handle missile rotation, the image is rotated each frame, and the pivot point must be corrected because pygame rotates around the image’s top‑left corner, causing the visual tip to shift. The corrected drawing uses the rotated image’s size and the computed head position (point C) to align the tip with the logical missile position.
<code>missiled = pygame.transform.rotate(missile, -(d_angle))
screen.blit(missiled, (x1-width+(x1-C[0]), y1-height/2+(y1-C[1])))</code>The full source code, including the quadrant‑specific calculations for the rotated image’s head position, is provided at the end of the article.
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.