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