Gradients Example

Screenshot of a program demoing how to use gradients
gradients.py
 1"""
 2Drawing Gradients
 3
 4If Python and Arcade are installed, this example can be run from the command line with:
 5python -m arcade.examples.gradients
 6"""
 7import arcade
 8from arcade import shape_list
 9
10# Do the math to figure out our screen dimensions
11SCREEN_WIDTH = 800
12SCREEN_HEIGHT = 600
13SCREEN_TITLE = "Gradients Example"
14
15
16class MyGame(arcade.Window):
17    """
18    Main application class.
19    """
20
21    def __init__(self, width, height, title):
22        """
23        Set up the application.
24        """
25
26        super().__init__(width, height, title)
27
28        self.background_color = arcade.color.BLACK
29
30        self.shapes = shape_list.ShapeElementList()
31
32        # This is a large rectangle that fills the whole
33        # background. The gradient goes between the two colors
34        # top to bottom.
35        color1 = (215, 214, 165)
36        color2 = (219, 166, 123)
37        points = (0, 0), (SCREEN_WIDTH, 0), (SCREEN_WIDTH, SCREEN_HEIGHT), (0, SCREEN_HEIGHT)
38        colors = (color1, color1, color2, color2)
39        rect = shape_list.create_rectangle_filled_with_colors(points, colors)
40        self.shapes.append(rect)
41
42        # Another rectangle, but in this case the color doesn't change. Just the
43        # transparency. This time it goes from left to right.
44        color1 = (165, 92, 85, 255)
45        color2 = (165, 92, 85, 0)
46        points = (100, 100), (SCREEN_WIDTH - 100, 100), (SCREEN_WIDTH - 100, 300), (100, 300)
47        colors = (color2, color1, color1, color2)
48        rect = shape_list.create_rectangle_filled_with_colors(points, colors)
49        self.shapes.append(rect)
50
51        # Two lines
52        color1 = (7, 67, 88)
53        color2 = (69, 137, 133)
54        points = (100, 400), (SCREEN_WIDTH - 100, 400), (SCREEN_WIDTH - 100, 500), (100, 500)
55        colors = [color2, color1, color2, color1]
56        shape = shape_list.create_lines_with_colors(points, colors, line_width=5)
57        self.shapes.append(shape)
58
59        # Triangle
60        color1 = (215, 214, 165)
61        color2 = (219, 166, 123)
62        color3 = (165, 92, 85)
63        points = (SCREEN_WIDTH // 2, 500), (SCREEN_WIDTH // 2 - 100, 400), (SCREEN_WIDTH // 2 + 100, 400)
64        colors = (color1, color2, color3)
65        shape = shape_list.create_triangles_filled_with_colors(points, colors)
66        self.shapes.append(shape)
67
68        # Ellipse, gradient between center and outside
69        color1 = (69, 137, 133, 127)
70        color2 = (7, 67, 88, 127)
71        shape = shape_list.create_ellipse_filled_with_colors(SCREEN_WIDTH // 2, 350, 50, 50,
72                                                             inside_color=color1, outside_color=color2)
73        self.shapes.append(shape)
74
75    def on_draw(self):
76        """
77        Render the screen.
78        """
79
80        # This command has to happen before we start drawing
81        self.clear()
82        self.shapes.draw()
83        # arcade.draw_rectangle_filled(500, 500, 50, 50, (255, 0, 0, 127))
84
85
86def main():
87
88    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
89    arcade.run()
90
91
92if __name__ == "__main__":
93    main()