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"""
 7
 8import arcade
 9import math
10
11# Set up the constants
12SCREEN_WIDTH = 800
13SCREEN_HEIGHT = 600
14SCREEN_TITLE = "Radar Sweep Example"
15
16# These constants control the particulars about the radar
17CENTER_X = SCREEN_WIDTH // 2
18CENTER_Y = SCREEN_HEIGHT // 2
19RADIANS_PER_FRAME = 0.02
20SWEEP_LENGTH = 250
21
22
23class Radar:
24    def __init__(self):
25        self.angle = 0
26
27    def update(self):
28
29        # Move the angle of the sweep.
30        self.angle += RADIANS_PER_FRAME
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        # Start the render. This must happen before any drawing
40        # commands. We do NOT need an stop render command.
41        arcade.start_render()
42
43        # Draw the radar line
44        arcade.draw_line(CENTER_X, CENTER_Y, x, y, arcade.color.OLIVE, 4)
45
46        # Draw the outline of the radar
47        arcade.draw_circle_outline(CENTER_X,
48                                   CENTER_Y,
49                                   SWEEP_LENGTH,
50                                   arcade.color.DARK_GREEN,
51                                   border_width=10,
52                                   num_segments=60)
53
54
55class MyGame(arcade.Window):
56    """ Main application class. """
57
58    def __init__(self, width, height, title):
59        super().__init__(width, height, title)
60
61        # Create our rectangle
62        self.radar = Radar()
63
64        # Set background color
65        arcade.set_background_color(arcade.color.BLACK)
66
67    def on_update(self, delta_time):
68        # Move the rectangle
69        self.radar.update()
70
71    def on_draw(self):
72        """ Render the screen. """
73
74        # Clear screen
75        self.clear()
76        # Draw the rectangle
77        self.radar.draw()
78
79
80def main():
81    """ Main function """
82    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
83    arcade.run()
84
85
86if __name__ == "__main__":
87    main()