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