Create radar scan animation using Pygame in Python

WBOY
Release: 2023-08-28 10:09:05
forward
1274 people have browsed it

Pygame is a set of cross-platform Python modules designed for writing video games. It includes computer graphics and sound libraries designed for use with the Python programming language. Pygame is not a game development engine, but a set of tools and libraries that allow developers to create 2D games using Python.

Pygame provides a variety of functions and classes to help developers create games, including image loading and manipulation, sound playback and recording, keyboard and mouse input handling, sprite and group management, and collision detection. It also includes built-in support for common game development tasks such as animation, scrolling, and tile-based maps.

Pygame is open source and free to use, and it runs on Windows, macOS, Linux, and other platforms. It is often used in educational settings as an introduction to game development or as a tool to teach programming concepts.

Components of radar scan animation

The basic radar scanning animation consists of the following parts -

  • RADAR CIRCLE - This is the circle that represents the radar range. It is centered on the origin and has a fixed radius.

  • RADAR SCAN - This is a line that rotates around the center of the circle. It represents the beam emitted from the radar with a scanning range of 360 degrees.

  • RADAR TARGETS - These are the objects we want to detect using radar. They are represented as points on the screen.

Now that we know the components of a radar scan animation, let’s dive into the implementation using Pygame.

prerequisites

Before we delve into the task, there are a few things that need to be installed to your

system-

Recommended settings list -

  • pip installs Numpy, pygame and Math

  • It is expected that users will be able to access any standalone IDE such as VSCode, PyCharm, Atom or Sublime text.

  • You can even use online Python compilers like Kaggle.com, Google Cloud Platform, or any other platform.

  • Updated Python version. As of this writing, I'm using version 3.10.9.

  • Learn about the use of Jupyter Notebook.

  • Knowledge and application of virtual environments would be beneficial but not required.

  • It is also expected that the person has a good understanding of statistics and mathematics.

Implementation details

Import Libraries - We will first import the necessary libraries - Pygame, NumPy and Math.

import pygame
import math
import random
Copy after login

Initializing the Game Window - We will use the Pygame library to initialize the game window with the required width and height.

WIDTH = 800
HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Radar Sweep Animation")
clock = pygame.time.Clock()
Copy after login

Set up the game environment - We will set up the game environment by defining the color, background and frame rate of the animation.

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
Copy after login

Set the frame rate of animation

Define Radar Circle - We will define the radar circle with the desired radius and center coordinates. We'll also set the circle's color and line thickness.

radar_circle_radius = 200
radar_circle_center_x = int(WIDTH / 2)
radar_circle_center_y = int(HEIGHT / 2)
Copy after login

Define Radar Scan - We will define the radar scan by setting the initial angle to 0 degrees and incrementing it every frame so that it scans 360 degrees. We will also set the color and thickness of the scan lines.

Define radar scan

radar_sweep_angle = 0
radar_sweep_length = radar_circle_radius + 50
def update():
   # Increment the angle in each frame
   global radar_sweep_angle
   radar_sweep_angle += 1
# Draw the radar sweep line
def draw_radar_sweep():
   x = radar_circle_center_x + radar_sweep_length *
math.sin(math.radians(radar_sweep_angle))
   y = radar_circle_center_y + radar_sweep_length *
math.cos(math.radians(radar_sweep_angle))
   pygame.draw.line(screen, BLACK, (radar_circle_center_x,
radar_circle_center_y), (x, y), 3)
Copy after login

Define Radar Target - We will define the radar target using random x and y coordinates within the radar circle. We'll also set the target's color and radius.

num_targets = 10
target_radius = 10
targets = []
for i in range(num_targets):
   x = random.randint(radar_circle_center_x - radar_circle_radius,
radar_circle_center_x + radar_circle_radius)
   y = random.randint(radar_circle_center_y - radar_circle_radius,
radar_circle_center_y + radar_circle_radius)
   targets.append((x, y))
   # Draw the radar targets
def draw_radar_targets():
   for target in targets:
      pygame.draw.circle(screen, BLUE, target, target_radius)
      distance_to_target = math.sqrt((target[0] - x) ** 2 + (target[1] - y) ** 2)
      if distance_to_target <= target_radius:
         pygame.draw.rect(screen, RED, (target[0], target[1], 60, 20))
         font = pygame.font.SysFont(None, 25)
         text = font.render("DETECTED", True, BLACK)
         screen.blit(text, (target[0], target[1]))
Copy after login

Running the Game - We will run the game by creating a pygame window, setting up the necessary event handlers, and running the game loop.

# Main game loop
running = True
while running:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         running = False
   screen.fill(WHITE)
   update()
   draw_radar_sweep()
   draw_radar_targets()
   pygame.display.update()
   clock.tick(FPS)
# Quit the game
pygame.quit()
Copy after login

Final program, code

import pygame
import math
import random
# Initializing the Game Window:
WIDTH = 800
HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Radar Sweep Animation")
clock = pygame.time.Clock()
# Defining colors:
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Set the frame rate of the animation
FPS = 60
# Defining the Radar Circle:
radar_circle_radius = 200
radar_circle_center_x = int(WIDTH / 2)
radar_circle_center_y = int(HEIGHT / 2)
# Define the radar sweep
radar_sweep_angle = 0
radar_sweep_length = radar_circle_radius + 50
# Define the radar targets
num_targets = 10
target_radius = 10
targets = []
for i in range(num_targets):
   x = random.randint(radar_circle_center_x - radar_circle_radius,
radar_circle_center_x + radar_circle_radius)
   y = random.randint(radar_circle_center_y - radar_circle_radius,
radar_circle_center_y + radar_circle_radius)
targets.append((x, y))
def update():
   # Increment the angle in each frame
   global radar_sweep_angle
   radar_sweep_angle += 1
# Draw the radar sweep line
def draw_radar_sweep():
   x = radar_circle_center_x + radar_sweep_length *
math.sin(math.radians(radar_sweep_angle))
   y = radar_circle_center_y + radar_sweep_length *
math.cos(math.radians(radar_sweep_angle))
   pygame.draw.line(screen, BLACK, (radar_circle_center_x,
radar_circle_center_y), (x, y), 3)
# Draw the radar targets
def draw_radar_targets():
   for target in targets:
      pygame.draw.circle(screen, BLUE, target, target_radius)
      distance_to_target = math.sqrt((target[0] - x) ** 2 + (target[1] - y)** 2)
      if distance_to_target <= target_radius:
         pygame.draw.rect(screen, RED, (target[0], target[1], 60, 20))
         font = pygame.font.SysFont(None, 25)
         text = font.render("DETECTED", True, BLACK)
         screen.blit(text, (target[0], target[1]))
# Main game loop
running = True
while running:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         running = False
         screen.fill(WHITE)
         update()
         draw_radar_sweep()
         draw_radar_targets()
      pygame.display.update()
      clock.tick(FPS)
# Quit the game
pygame.quit()
Copy after login
Create radar scan animation using Pygame in Python

We can see the output of the program and observe the animation of the game.

in conclusion

In this document, we explore how to create a radar scan animation using Pygame in Python. We learned about the components of a radar scan animation and walked through the implementation details using code snippets and practical examples. Pygame provides a user-friendly API for game development and is an excellent choice for creating 2D video games and animations. With the knowledge gained from this document, you should now be able to create your own radar scan animation using Pygame.

The above is the detailed content of Create radar scan animation using Pygame in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template