Radar Sweep

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
11WINDOW_WIDTH = 800
12WINDOW_HEIGHT = 600
13WINDOW_TITLE = "Radar Sweep Example"
14
15# These constants control the particulars about the radar
16CENTER_X = WINDOW_WIDTH // 2
17CENTER_Y = WINDOW_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 GameView(arcade.View):
50 """ Main application class. """
51
52 def __init__(self):
53 super().__init__()
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 # Create a window class. This is what actually shows up on screen
76 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
77
78 # Create the GameView
79 game = GameView()
80
81 # Show GameView on screen
82 window.show_view(game)
83
84 # Start the arcade game loop
85 arcade.run()
86
87
88if __name__ == "__main__":
89 main()