Using a Vertex Buffer Object With Lines

Screen shot of a maze created by depth first
lines_buffered.py
 1"""
 2Using a Vertex Buffer Object With Lines
 3
 4If Python and Arcade are installed, this example can be run from the command line with:
 5python -m arcade.examples.lines_buffered
 6"""
 7import random
 8import arcade
 9from arcade.shape_list import (
10    ShapeElementList,
11    create_line_strip,
12)
13from inspect import getmembers
14from arcade.types import Color
15
16# Do the math to figure out our screen dimensions
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19SCREEN_TITLE = "Vertex Buffer Object With Lines Example"
20
21
22class MyGame(arcade.Window):
23    """
24    Main application class.
25    """
26
27    def __init__(self, width, height, title):
28        """
29        Set up the application.
30        """
31        super().__init__(width, height, title)
32        self.set_vsync(True)
33
34        self.shape_list = ShapeElementList()
35        point_list = ((0, 50),
36                      (10, 10),
37                      (50, 0),
38                      (10, -10),
39                      (0, -50),
40                      (-10, -10),
41                      (-50, 0),
42                      (-10, 10),
43                      (0, 50))
44
45        # Filter out anything other than a Color, such as imports and
46        # helper functions.
47        colors = [
48            color for name, color in
49            getmembers(arcade.color, lambda c: isinstance(c, Color))]
50
51        for i in range(200):
52            x = SCREEN_WIDTH // 2 - random.randrange(SCREEN_WIDTH)
53            y = SCREEN_HEIGHT // 2 - random.randrange(SCREEN_HEIGHT)
54            color = random.choice(colors)
55            points = [(px + x, py + y) for px, py in point_list]
56
57            my_line_strip = create_line_strip(points, color, 5)
58            self.shape_list.append(my_line_strip)
59
60        self.shape_list.center_x = SCREEN_WIDTH // 2
61        self.shape_list.center_y = SCREEN_HEIGHT // 2
62        self.shape_list.angle = 0
63
64        self.background_color = arcade.color.BLACK
65
66    def on_draw(self):
67        """
68        Render the screen.
69        """
70        # This command has to happen before we start drawing
71        self.clear()
72
73        self.shape_list.draw()
74
75    def on_update(self, delta_time):
76        self.shape_list.angle += 1 * 60 * delta_time
77        self.shape_list.center_x += 0.1 * 60 * delta_time
78        self.shape_list.center_y += 0.1 * 60 * delta_time
79
80
81def main():
82    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
83    arcade.run()
84
85
86if __name__ == "__main__":
87    main()