Radar Sweep#

Screenshot of radar sweep example
radar_sweep.py#
 1"""
 2This animation example shows how perform a radar sweep animation.
 3
 4If Python and Arcade are installed, this example can be run from the command line with:
 5python -m arcade.examples.radar_sweep
 6"""
 7from __future__ import annotations
 8
 9import arcade
10import math
11
12# Set up the constants
13SCREEN_WIDTH = 800
14SCREEN_HEIGHT = 600
15SCREEN_TITLE = "Radar Sweep Example"
16
17# These constants control the particulars about the radar
18CENTER_X = SCREEN_WIDTH // 2
19CENTER_Y = SCREEN_HEIGHT // 2
20RADIANS_PER_FRAME = 0.02
21SWEEP_LENGTH = 250
22
23
24class Radar:
25    def __init__(self):
26        self.angle = 0
27
28    def update(self, delta_time=0):
29        # Move the angle of the sweep.
30        self.angle += RADIANS_PER_FRAME * delta_time
31
32    def draw(self):
33        """ Use this function to draw everything to the screen. """
34
35        # Calculate the end point of our radar sweep. Using math.
36        x = SWEEP_LENGTH * math.sin(self.angle) + CENTER_X
37        y = SWEEP_LENGTH * math.cos(self.angle) + CENTER_Y
38
39        # Draw the radar line
40        arcade.draw_line(CENTER_X, CENTER_Y, x, y, arcade.color.OLIVE, 4)
41
42        # Draw the outline of the radar
43        arcade.draw_circle_outline(CENTER_X,
44                                   CENTER_Y,
45                                   SWEEP_LENGTH,
46                                   arcade.color.DARK_GREEN,
47                                   border_width=10,
48                                   num_segments=60)
49
50
51class MyGame(arcade.Window):
52    """ Main application class. """
53
54    def __init__(self, width, height, title):
55        super().__init__(width, height, title)
56
57        # Create our rectangle
58        self.radar = Radar()
59
60        # Set background color
61        self.background_color = arcade.color.BLACK
62
63    def on_update(self, delta_time):
64        # Move the rectangle
65        self.radar.update(delta_time * 60)
66
67    def on_draw(self):
68        """Draw the screen"""
69        # Clear screen
70        self.clear()
71        # Draw the rectangle
72        self.radar.draw()
73
74
75def main():
76    """ Main function """
77    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
78    arcade.run()
79
80
81if __name__ == "__main__":
82    main()