Using a Vertex Buffer Object With Lines

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
17WINDOW_WIDTH = 1280
18WINDOW_HEIGHT = 720
19WINDOW_TITLE = "Vertex Buffer Object With Lines Example"
20
21
22class GameView(arcade.View):
23 """
24 Main application class.
25 """
26
27 def __init__(self):
28 """
29 Set up the application.
30 """
31 super().__init__()
32
33 self.shape_list = ShapeElementList()
34 point_list = ((0, 50),
35 (10, 10),
36 (50, 0),
37 (10, -10),
38 (0, -50),
39 (-10, -10),
40 (-50, 0),
41 (-10, 10),
42 (0, 50))
43
44 # Filter out anything other than a Color, such as imports and
45 # helper functions.
46 colors = [
47 color for name, color in
48 getmembers(arcade.color, lambda c: isinstance(c, Color))]
49
50 for i in range(200):
51 x = WINDOW_WIDTH // 2 - random.randrange(WINDOW_WIDTH)
52 y = WINDOW_HEIGHT // 2 - random.randrange(WINDOW_HEIGHT)
53 color = random.choice(colors)
54 points = [(px + x, py + y) for px, py in point_list]
55
56 my_line_strip = create_line_strip(points, color, 5)
57 self.shape_list.append(my_line_strip)
58
59 self.shape_list.center_x = WINDOW_WIDTH // 2
60 self.shape_list.center_y = WINDOW_HEIGHT // 2
61 self.shape_list.angle = 0
62
63 self.background_color = arcade.color.BLACK
64
65 def on_draw(self):
66 """
67 Render the screen.
68 """
69 # This command has to happen before we start drawing
70 self.clear()
71
72 self.shape_list.draw()
73
74 def on_update(self, delta_time):
75 self.shape_list.angle += 1 * 60 * delta_time
76 self.shape_list.center_x += 0.1 * 60 * delta_time
77 self.shape_list.center_y += 0.1 * 60 * delta_time
78
79
80def main():
81 """ Main function """
82 # Create a window class. This is what actually shows up on screen
83 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, vsync=True)
84
85 # Create and setup the GameView
86 game = GameView()
87
88 # Show GameView on screen
89 window.show_view(game)
90
91 # Start the arcade game loop
92 arcade.run()
93
94
95if __name__ == "__main__":
96 main()