Pyglet Draw Moving Shapes Source Code
draw_shapes.py
1"""
2Moving Sprites Stress Test
3"""
4
5import random
6import os
7import arcade
8import arcade.shape_list
9from performance_timing import PerformanceTiming
10import pyglet.shapes
11
12# Set up the constants
13SCREEN_WIDTH = 1800
14SCREEN_HEIGHT = 1000
15SCREEN_TITLE = "Pyglet Moving Shapes"
16
17
18class MovingEllipse(pyglet.shapes.Rectangle):
19 """ Generic base shape class """
20 def __init__(self, x, y, a, b, color=(255, 255, 255, 255), batch=None, group=None):
21 super().__init__(x, y, a, b, color, batch, group)
22 self.delta_x = 0
23 self.delta_y = 0
24 self.delta_angle = 0
25 # Anchor the rotation to the middle of the rectangle, instead of the corner.
26 self.anchor_x = a / 2
27 self.anchor_y = b / 2
28
29 def move(self):
30 # self.x += self.delta_x
31 # self.y += self.delta_y
32 # self.rotation += self.delta_angle
33 # if self.x < 0 and self.delta_x < 0:
34 # self.delta_x *= -1
35 # if self.y < 0 and self.delta_y < 0:
36 # self.delta_y *= -1
37 # if self.x > SCREEN_WIDTH and self.delta_x > 0:
38 # self.delta_x *= -1
39 # if self.y > SCREEN_HEIGHT and self.delta_y > 0:
40 # self.delta_y *= -1
41
42 x, y = self.position
43 x += self.delta_x
44 y += self.delta_y
45 self.position = x, y
46 if x < 0 and self.delta_x < 0:
47 self.delta_x *= -1
48 if y < 0 and self.delta_y < 0:
49 self.delta_y *= -1
50 if x > SCREEN_WIDTH and self.delta_x > 0:
51 self.delta_x *= -1
52 if y > SCREEN_HEIGHT and self.delta_y > 0:
53 self.delta_y *= -1
54
55
56class MyGame(arcade.Window):
57 """ Main application class. """
58
59 def __init__(self):
60 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
61
62 self.shape_list = None
63 self.batch = pyglet.graphics.Batch()
64
65 # Set the working directory (where we expect to find files) to the same
66 # directory this .py file is in. You can leave this out of your own
67 # code, but it is needed to easily run the examples using "python -m"
68 # as mentioned at the top of this program.
69 file_path = os.path.dirname(os.path.abspath(__file__))
70 os.chdir(file_path)
71
72 self.results_file = "../../result_data/pyglet/moving_shapes.csv"
73 self.results_image = "../../result_data/pyglet/moving_shapes.png"
74
75 self.performance_timing = PerformanceTiming(results_file=self.results_file,
76 start_n=0,
77 increment_n=200,
78 end_time=60)
79
80 def setup(self):
81 """ Set up the game and initialize the variables. """
82 self.shape_list = []
83
84 def add_shapes(self, amount):
85 for i in range(amount):
86 x = random.randrange(0, SCREEN_WIDTH)
87 y = random.randrange(0, SCREEN_HEIGHT)
88 width = random.randrange(10, 30)
89 height = random.randrange(10, 30)
90
91 d_x = random.randrange(-3, 4)
92 d_y = random.randrange(-3, 4)
93
94 red = random.randrange(256)
95 green = random.randrange(256)
96 blue = random.randrange(256)
97 # alpha = random.randrange(256)
98
99 shape = MovingEllipse(x, y, width, height,
100 color=(red, green, blue),
101 batch=self.batch)
102 shape.delta_x = d_x
103 shape.delta_y = d_y
104 self.shape_list.append(shape)
105
106 def on_update(self, dt):
107 """ Move everything """
108
109 # Start update timer
110 self.performance_timing.start_timer('update')
111
112 for shape in self.shape_list:
113 shape.move()
114
115 # Stop timing the update
116 self.performance_timing.stop_timer('update')
117
118 # Figure out if we need more shapes
119 if self.performance_timing.target_n > len(self.shape_list):
120 new_amount = self.performance_timing.target_n - len(self.shape_list)
121 self.add_shapes(new_amount)
122
123 # End the program run
124 if self.performance_timing.end_run():
125 # Save screenshot
126 image = arcade.get_image()
127 image.save(self.results_image, 'PNG')
128 self.close()
129
130 def on_draw(self):
131 """ Render the screen. """
132 # Start timing how long this takes
133 self.performance_timing.start_timer('draw')
134
135 self.clear()
136 self.batch.draw()
137
138 # Stop timing how long this takes
139 self.performance_timing.stop_timer('draw')
140
141
142def main():
143 window = MyGame()
144 window.setup()
145 arcade.run()
146
147
148if __name__ == "__main__":
149 main()