Bouncing Shapes

shapes.py
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
11import arcade
12import random
13
14# Set up the constants
15WINDOW_WIDTH = 1280
16WINDOW_HEIGHT = 720
17WINDOW_TITLE = "Shapes!"
18
19NUMBER_OF_SHAPES = 200
20
21
22class Shape:
23 """ Generic base shape class """
24 def __init__(self, x, y, width, height, angle, delta_x, delta_y,
25 delta_angle, color):
26 self.x = x
27 self.y = y
28 self.width = width
29 self.height = height
30 self.angle = angle
31 self.delta_x = delta_x
32 self.delta_y = delta_y
33 self.delta_angle = delta_angle
34 self.color = color
35
36 def move(self):
37 self.x += self.delta_x
38 self.y += self.delta_y
39 self.angle += self.delta_angle
40 if self.x < 0 and self.delta_x < 0:
41 self.delta_x *= -1
42 if self.y < 0 and self.delta_y < 0:
43 self.delta_y *= -1
44 if self.x > WINDOW_WIDTH and self.delta_x > 0:
45 self.delta_x *= -1
46 if self.y > WINDOW_HEIGHT and self.delta_y > 0:
47 self.delta_y *= -1
48
49
50class Ellipse(Shape):
51
52 def draw(self):
53 arcade.draw_ellipse_filled(self.x, self.y, self.width, self.height,
54 self.color, self.angle)
55
56
57class Rectangle(Shape):
58
59 def draw(self):
60 arcade.draw_rect_filled(arcade.rect.XYWH(self.x, self.y, self.width, self.height),
61 self.color, self.angle)
62
63
64class Line(Shape):
65
66 def draw(self):
67 arcade.draw_line(self.x, self.y,
68 self.x + self.width, self.y + self.height,
69 self.color, 2)
70
71
72class GameView(arcade.View):
73 """ Main application class. """
74
75 def __init__(self):
76 # Call the parent __init__
77 super().__init__()
78
79 # Create a shape list
80 self.shape_list = []
81
82 for i in range(NUMBER_OF_SHAPES):
83
84 # Random spot
85 x = random.randrange(0, WINDOW_WIDTH)
86 y = random.randrange(0, WINDOW_HEIGHT)
87
88 # Random size
89 width = random.randrange(15, 40)
90 height = random.randrange(15, 40)
91
92 # Random angle
93 angle = random.randrange(0, 360)
94
95 # Random movement
96 d_x = random.randrange(-3, 4)
97 d_y = random.randrange(-3, 4)
98 d_angle = random.randrange(-3, 4)
99
100 # Random color
101 red = random.randrange(256)
102 green = random.randrange(256)
103 blue = random.randrange(256)
104 alpha = random.randrange(256)
105
106 # Random line, ellipse, or rect
107 shape_type = random.randrange(3)
108
109 if shape_type == 0:
110 shape = Rectangle(x, y, width, height, angle, d_x, d_y,
111 d_angle, (red, green, blue, alpha))
112 elif shape_type == 1:
113 shape = Ellipse(x, y, width, height, angle, d_x, d_y,
114 d_angle, (red, green, blue, alpha))
115 else:
116 shape = Line(x, y, width, height, angle, d_x, d_y,
117 d_angle, (red, green, blue, alpha))
118
119 # Add this new shape to the list
120 self.shape_list.append(shape)
121
122 def on_update(self, dt):
123 """ Move everything """
124 for shape in self.shape_list:
125 shape.move()
126
127 def on_draw(self):
128 """ Render the screen. """
129 self.clear()
130
131 # Draw the shapes
132 for shape in self.shape_list:
133 shape.draw()
134
135
136def main():
137 """ Main function """
138 # Create a window class. This is what actually shows up on screen
139 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
140
141 # Create the GameView
142 game = GameView()
143
144 # Show GameView on screen
145 window.show_view(game)
146
147 # Start the arcade game loop
148 arcade.run()
149
150
151if __name__ == "__main__":
152 main()