1"""
2This simple animation example shows how to use classes to animate
3multiple objects on the screen at the same time.
4
5Note: Sprites draw much faster than drawing primitives
6
7If Python and Arcade are installed, this example can be run from the command line with:
8python -m arcade.examples.shapes
9"""
10
11from __future__ import annotations
12
13import arcade
14import random
15
16# Set up the constants
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19SCREEN_TITLE = "Shapes!"
20
21NUMBER_OF_SHAPES = 250
22
23
24class Shape:
25 """ Generic base shape class """
26 def __init__(self, x, y, width, height, angle, delta_x, delta_y,
27 delta_angle, color):
28 self.x = x
29 self.y = y
30 self.width = width
31 self.height = height
32 self.angle = angle
33 self.delta_x = delta_x
34 self.delta_y = delta_y
35 self.delta_angle = delta_angle
36 self.color = color
37
38 def move(self):
39 self.x += self.delta_x
40 self.y += self.delta_y
41 self.angle += self.delta_angle
42 if self.x < 0 and self.delta_x < 0:
43 self.delta_x *= -1
44 if self.y < 0 and self.delta_y < 0:
45 self.delta_y *= -1
46 if self.x > SCREEN_WIDTH and self.delta_x > 0:
47 self.delta_x *= -1
48 if self.y > SCREEN_HEIGHT and self.delta_y > 0:
49 self.delta_y *= -1
50
51
52class Ellipse(Shape):
53
54 def draw(self):
55 arcade.draw_ellipse_filled(self.x, self.y, self.width, self.height,
56 self.color, self.angle)
57
58
59class Rectangle(Shape):
60
61 def draw(self):
62 arcade.draw_rectangle_filled(self.x, self.y, self.width, self.height,
63 self.color, self.angle)
64
65
66class Line(Shape):
67
68 def draw(self):
69 arcade.draw_line(self.x, self.y,
70 self.x + self.width, self.y + self.height,
71 self.color, 2)
72
73
74class MyGame(arcade.Window):
75 """ Main application class. """
76
77 def __init__(self):
78 # Call the parent __init__
79 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
80
81 # Create a shape list
82 self.shape_list = []
83
84 for i in range(NUMBER_OF_SHAPES):
85
86 # Random spot
87 x = random.randrange(0, SCREEN_WIDTH)
88 y = random.randrange(0, SCREEN_HEIGHT)
89
90 # Random size
91 width = random.randrange(15, 40)
92 height = random.randrange(15, 40)
93
94 # Random angle
95 angle = random.randrange(0, 360)
96
97 # Random movement
98 d_x = random.randrange(-3, 4)
99 d_y = random.randrange(-3, 4)
100 d_angle = random.randrange(-3, 4)
101
102 # Random color
103 red = random.randrange(256)
104 green = random.randrange(256)
105 blue = random.randrange(256)
106 alpha = random.randrange(256)
107
108 # Random line, ellipse, or rect
109 shape_type = random.randrange(3)
110
111 if shape_type == 0:
112 shape = Rectangle(x, y, width, height, angle, d_x, d_y,
113 d_angle, (red, green, blue, alpha))
114 elif shape_type == 1:
115 shape = Ellipse(x, y, width, height, angle, d_x, d_y,
116 d_angle, (red, green, blue, alpha))
117 else:
118 shape = Line(x, y, width, height, angle, d_x, d_y,
119 d_angle, (red, green, blue, alpha))
120
121 # Add this new shape to the list
122 self.shape_list.append(shape)
123
124 def on_update(self, dt):
125 """ Move everything """
126 for shape in self.shape_list:
127 shape.move()
128
129 def on_draw(self):
130 """ Render the screen. """
131
132 # Clear teh screen
133 self.clear()
134
135 # Draw the shapes
136 for shape in self.shape_list:
137 shape.draw()
138
139
140def main():
141 MyGame()
142 arcade.run()
143
144
145if __name__ == "__main__":
146 main()