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