Python街机模块画渐变图形透明度例子,译者:李兴球

"""
画渐变图形例子
"""
import arcade

# 常量定义
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Python街机模块画渐变图形透明度例子,译者:李兴球"


class MyGame(arcade.Window):
    """
    继承自窗口的游戏类
    """

    def __init__(self, width, height, title):
        """
        初始化方法
        """

        super().__init__(width, height, title) # 调用父类方法开窗口

        arcade.set_background_color(arcade.color.BLACK)# 设置背景颜色

        self.shapes = arcade.ShapeElementList()        # 形状项目列表

        # 这个最大的矩形做为背景
        # 颜色在两色之间渐变
        # 自顶向下渐变
        color1 = (215, 0, 50)
        color2 = (0, 255, 0)
        points = (0, 0), (SCREEN_WIDTH, 0), (SCREEN_WIDTH, SCREEN_HEIGHT), (0, SCREEN_HEIGHT)
        colors = (color1, color1, color2, color2)
        rect = arcade.create_rectangle_filled_with_colors(points, colors)
        self.shapes.append(rect)

        # 下面颜色不变,但是透明度改变,从左到右 
        color1 = (0, 255, 255, 255) # 青色,不透明
        color2 = (0, 255, 255, 0)   # 青色, 透明
        points = (100, 100), (SCREEN_WIDTH - 100, 100), (SCREEN_WIDTH - 100, 300), (100, 300)
        colors = (color2, color1, color1, color2)
        rect = arcade.create_rectangle_filled_with_colors(points, colors)
        self.shapes.append(rect)

        # 两条线
        color1 = (7, 67, 88)
        color2 = (69, 137, 133)
        points = (100, 400), (SCREEN_WIDTH - 100, 400), (SCREEN_WIDTH - 100, 500), (100, 500)
        colors = (color2, color1, color2, color1)
        shape = arcade.create_lines_with_colors(points, colors, line_width=5)
        self.shapes.append(shape)

        # 三角形
        color1 = (215, 214, 165)
        color2 = (219, 166, 123)
        color3 = (165, 92, 85)
        points = (SCREEN_WIDTH // 2, 500), (SCREEN_WIDTH // 2 - 100, 400), (SCREEN_WIDTH // 2 + 100, 400)
        colors = (color1, color2, color3)
        shape = arcade.create_triangles_filled_with_colors(points, colors)
        self.shapes.append(shape)

        # 椭圆从里到外渐变,(模拟3D效果)
        color1 = (69, 137, 133, 127)
        color2 = (7, 67, 88, 127)
        shape = arcade.create_ellipse_filled_with_colors(SCREEN_WIDTH // 2, 350, 50, 50,
                                                         inside_color=color1, outside_color=color2)
        self.shapes.append(shape)

    def on_draw(self):
        """
        渲染屏幕
        """

        # 开始渲染
        arcade.start_render()
        self.shapes.draw()
        # arcade.draw_rectangle_filled(500, 500, 50, 50, (255, 0, 0, 127))


def main():

    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    arcade.run()


if __name__ == "__main__":
    main()