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
 11WINDOW_WIDTH = 1280
 12WINDOW_HEIGHT = 720
 13WINDOW_TITLE = "Gradients Example"
 14
 15
 16class GameView(arcade.View):
 17    """
 18    Main application class.
 19    """
 20
 21    def __init__(self):
 22        """
 23        Set up the application.
 24        """
 25
 26        super().__init__()
 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 = (
 38            (0, 0),
 39            (WINDOW_WIDTH, 0),
 40            (WINDOW_WIDTH, WINDOW_HEIGHT),
 41            (0, WINDOW_HEIGHT),
 42        )
 43        colors = (color1, color1, color2, color2)
 44        rect = shape_list.create_rectangle_filled_with_colors(points, colors)
 45        self.shapes.append(rect)
 46
 47        # Another rectangle, but in this case the color doesn't change. Just the
 48        # transparency. This time it goes from left to right.
 49        color1 = (165, 92, 85, 255)
 50        color2 = (165, 92, 85, 0)
 51        points = (
 52            (100, 100),
 53            (WINDOW_WIDTH - 100, 100),
 54            (WINDOW_WIDTH - 100, 300),
 55            (100, 300),
 56        )
 57        colors = (color2, color1, color1, color2)
 58        rect = shape_list.create_rectangle_filled_with_colors(points, colors)
 59        self.shapes.append(rect)
 60
 61        # Two lines
 62        color1 = (7, 67, 88)
 63        color2 = (69, 137, 133)
 64        points = (
 65            (100, 400),
 66            (WINDOW_WIDTH - 100, 400),
 67            (WINDOW_WIDTH - 100, 500),
 68            (100, 500),
 69        )
 70        colors = [color2, color1, color2, color1]
 71        shape = shape_list.create_lines_with_colors(points, colors, line_width=5)
 72        self.shapes.append(shape)
 73
 74        # Triangle
 75        color1 = (215, 214, 165)
 76        color2 = (219, 166, 123)
 77        color3 = (165, 92, 85)
 78        points = (
 79            (WINDOW_WIDTH // 2, 500),
 80            (WINDOW_WIDTH // 2 - 100, 400),
 81            (WINDOW_WIDTH // 2 + 100, 400),
 82        )
 83        colors = (color1, color2, color3)
 84        shape = shape_list.create_triangles_filled_with_colors(points, colors)
 85        self.shapes.append(shape)
 86
 87        # Ellipse, gradient between center and outside
 88        color1 = (69, 137, 133, 127)
 89        color2 = (7, 67, 88, 127)
 90        shape = shape_list.create_ellipse_filled_with_colors(
 91            WINDOW_WIDTH // 2, 350, 50, 50,
 92            inside_color=color1, outside_color=color2,
 93        )
 94        self.shapes.append(shape)
 95
 96    def on_draw(self):
 97        """
 98        Render the screen.
 99        """
100
101        # This command has to happen before we start drawing
102        self.clear()
103        self.shapes.draw()
104        # arcade.draw_rectangle_filled(500, 500, 50, 50, (255, 0, 0, 127))
105
106
107def main():
108    """ Main function """
109    # Create a window class. This is what actually shows up on screen
110    window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
111
112    # Create the GameView
113    game = GameView()
114
115    # Show GameView on screen
116    window.show_view(game)
117
118    # Start the arcade game loop
119    arcade.run()
120
121
122if __name__ == "__main__":
123    main()