Creating a radar sweep animation using Pygame in Python

Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. A radar sweep animation creates a rotating line that sweeps across a circular display, commonly used in games and simulations to represent detection systems.

Components of a Radar Sweep Animation

A basic radar sweep animation consists of the following components ?

  • Radar Circle ? The circular boundary representing the radar's detection range
  • Radar Sweep ? A rotating line that sweeps around the center at 360 degrees
  • Radar Targets ? Objects or points that appear when detected by the sweep

Setting Up the Environment

First, we need to import the required libraries and initialize Pygame ?

import pygame
import math
import random

# Initialize Pygame
pygame.init()

# Constants
WIDTH = 800
HEIGHT = 600
FPS = 60

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

Creating the Game Window

Set up the display window and clock for frame rate control ?

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Radar Sweep Animation")
clock = pygame.time.Clock()

Defining Radar Properties

Configure the radar circle, sweep line, and target properties ?

# Radar circle properties
radar_center_x = WIDTH // 2
radar_center_y = HEIGHT // 2
radar_radius = 200

# Radar sweep properties
sweep_angle = 0
sweep_length = radar_radius

# Generate random targets
num_targets = 8
targets = []
for i in range(num_targets):
    # Generate targets within the radar circle
    angle = random.uniform(0, 2 * math.pi)
    distance = random.uniform(50, radar_radius - 20)
    x = radar_center_x + distance * math.cos(angle)
    y = radar_center_y + distance * math.sin(angle)
    targets.append((int(x), int(y)))

Drawing Functions

Create functions to draw the radar components ?

def draw_radar_circle():
    """Draw the radar circle boundary"""
    pygame.draw.circle(screen, GREEN, (radar_center_x, radar_center_y), radar_radius, 2)

def draw_radar_sweep():
    """Draw the rotating sweep line"""
    end_x = radar_center_x + sweep_length * math.cos(math.radians(sweep_angle))
    end_y = radar_center_y + sweep_length * math.sin(math.radians(sweep_angle))
    pygame.draw.line(screen, GREEN, (radar_center_x, radar_center_y), (end_x, end_y), 3)

def draw_targets():
    """Draw targets and check if they're detected by sweep"""
    sweep_end_x = radar_center_x + sweep_length * math.cos(math.radians(sweep_angle))
    sweep_end_y = radar_center_y + sweep_length * math.sin(math.radians(sweep_angle))
    
    for target in targets:
        # Calculate if target is near the sweep line
        target_angle = math.degrees(math.atan2(target[1] - radar_center_y, target[0] - radar_center_x))
        if target_angle < 0:
            target_angle += 360
        
        # Check if target is within sweep detection range
        angle_diff = abs(target_angle - sweep_angle)
        if angle_diff > 180:
            angle_diff = 360 - angle_diff
            
        if angle_diff < 10:  # Detection range
            pygame.draw.circle(screen, RED, target, 8)  # Detected target
        else:
            pygame.draw.circle(screen, BLUE, target, 5)  # Undetected target

def update_sweep():
    """Update the sweep angle for animation"""
    global sweep_angle
    sweep_angle = (sweep_angle + 2) % 360

Complete Radar Animation Program

import pygame
import math
import random

# Initialize Pygame
pygame.init()

# Constants
WIDTH = 800
HEIGHT = 600
FPS = 60

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

# Set up display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Radar Sweep Animation")
clock = pygame.time.Clock()

# Radar properties
radar_center_x = WIDTH // 2
radar_center_y = HEIGHT // 2
radar_radius = 200
sweep_angle = 0
sweep_length = radar_radius

# Generate random targets
num_targets = 8
targets = []
for i in range(num_targets):
    angle = random.uniform(0, 2 * math.pi)
    distance = random.uniform(50, radar_radius - 20)
    x = radar_center_x + distance * math.cos(angle)
    y = radar_center_y + distance * math.sin(angle)
    targets.append((int(x), int(y)))

def draw_radar_circle():
    pygame.draw.circle(screen, GREEN, (radar_center_x, radar_center_y), radar_radius, 2)

def draw_radar_sweep():
    end_x = radar_center_x + sweep_length * math.cos(math.radians(sweep_angle))
    end_y = radar_center_y + sweep_length * math.sin(math.radians(sweep_angle))
    pygame.draw.line(screen, GREEN, (radar_center_x, radar_center_y), (end_x, end_y), 3)

def draw_targets():
    for target in targets:
        target_angle = math.degrees(math.atan2(target[1] - radar_center_y, target[0] - radar_center_x))
        if target_angle < 0:
            target_angle += 360
        
        angle_diff = abs(target_angle - sweep_angle)
        if angle_diff > 180:
            angle_diff = 360 - angle_diff
            
        if angle_diff < 10:
            pygame.draw.circle(screen, RED, target, 8)
        else:
            pygame.draw.circle(screen, BLUE, target, 5)

def update_sweep():
    global sweep_angle
    sweep_angle = (sweep_angle + 2) % 360

# Main game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # Clear screen
    screen.fill(BLACK)
    
    # Update and draw
    update_sweep()
    draw_radar_circle()
    draw_radar_sweep()
    draw_targets()
    
    # Update display
    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()

Key Features

Component Function Visual Effect
Radar Circle draw_radar_circle() Green circular boundary
Sweep Line draw_radar_sweep() Rotating green line
Target Detection draw_targets() Blue/Red dots based on detection

Conclusion

This radar sweep animation demonstrates fundamental game development concepts using Pygame. The animation combines mathematical calculations for rotation, collision detection logic, and real-time rendering to create an interactive radar display effect.

Updated on: 2026-03-27T05:59:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements