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
23def on_draw(_delta_time):
24    """ Use this function to draw everything to the screen. """
25
26    # Move the angle of the sweep.
27    on_draw.angle += RADIANS_PER_FRAME
28
29    # Calculate the end point of our radar sweep. Using math.
30    x = SWEEP_LENGTH * math.sin(on_draw.angle) + CENTER_X
31    y = SWEEP_LENGTH * math.cos(on_draw.angle) + CENTER_Y
32
33    # Start the render. This must happen before any drawing
34    # commands. We do NOT need an stop render command.
35    arcade.start_render()
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, CENTER_Y, SWEEP_LENGTH,
42                               arcade.color.DARK_GREEN, 10)
43
44
45# This is a function-specific variable. Before we
46# use them in our function, we need to give them initial
47# values.
48on_draw.angle = 0  # type: ignore # dynamic attribute on function obj
49
50
51def main():
52
53    # Open up our window
54    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
55    arcade.set_background_color(arcade.color.BLACK)
56
57    # Tell the computer to call the draw command at the specified interval.
58    arcade.schedule(on_draw, 1 / 80)
59
60    # Run the program
61    arcade.run()
62
63    # When done running the program, close the window.
64    arcade.close_window()
65
66
67if __name__ == "__main__":
68    main()