"""蓝天排松鸟飞图_演示arcade装饰器的用法。 这是画一幅很多鸟在蓝天上飞的动画。蓝天下面是两排松树。 """ # 导入模块 import arcade import random SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 def draw_background(window): """ 本函数画地面和蓝天 """ # 在屏幕三分之二的上面区域画蓝天 arcade.draw_rectangle_filled(SCREEN_WIDTH / 2, SCREEN_HEIGHT * 2 / 3, SCREEN_WIDTH - 1, SCREEN_HEIGHT * 2 / 3, arcade.color.SKY_BLUE) # 在屏幕靠下三分之一画绿地 arcade.draw_rectangle_filled(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 6, SCREEN_WIDTH - 1, SCREEN_HEIGHT / 3, arcade.color.DARK_SPRING_GREEN) def draw_bird(x, y): """ 画一只鸟 """ arcade.draw_arc_outline(x, y, 20, 20, arcade.color.BLACK, 0, 90) arcade.draw_arc_outline(x + 40, y, 20, 20, arcade.color.BLACK, 90, 180) def draw_pine_tree(center_x, center_y): """ 在指定坐标画一颗松树 Args: :center_x: x position of the tree center. :center_y: y position of the tree trunk center. """ # 画树杆 arcade.draw_rectangle_filled(center_x, center_y, 20, 40, arcade.color.DARK_BROWN) tree_bottom_y = center_y + 20 # 画三角形 point_list = ((center_x - 40, tree_bottom_y), (center_x, tree_bottom_y + 100), (center_x + 40, tree_bottom_y)) arcade.draw_polygon_filled(point_list, arcade.color.DARK_GREEN) def draw_birds(window): # 画所有的鸟 for bird in window.bird_list: # 画这个鸟 draw_bird(bird[0], bird[1]) def draw_trees(window): # 画上排松树 for x in range(45, SCREEN_WIDTH, 90): draw_pine_tree(x, SCREEN_HEIGHT / 3) # 画下排松树 for x in range(65, SCREEN_WIDTH, 90): draw_pine_tree(x, (SCREEN_HEIGHT / 3) - 120) @arcade.decorator.setup def create_birds(window): """这里只是创建一个列表,列表里装着一些坐标对。这些坐标都是随机的,给鸟用。 This, and any function with the arcade.decorator.init decorator, is run automatically on start-up.用了这个装饰器后会在启动时自动运行。 """ window.bird_list = [] for bird_count in range(10): x = random.randrange(SCREEN_WIDTH) y = random.randrange(SCREEN_HEIGHT / 2, SCREEN_HEIGHT) window.bird_list.append([x, y]) @arcade.decorator.update def animate_birds(window, delta_time): """ 每60分之一秒运行一次,这里只是改变坐标。 Do not draw anything in this function. """ change_y = 0.3 for bird in window.bird_list: bird[0] += change_y if bird[0] > SCREEN_WIDTH + 20: bird[0] = -20 @arcade.decorator.draw def draw(window): """ 每60分之一秒运行一次,这里重画所有图形,并不改变坐标。 """ # 调用所有画的函数。 draw_background(window) draw_birds(window) draw_trees(window) if __name__ == "__main__": arcade.decorator.run(SCREEN_WIDTH, SCREEN_HEIGHT, title="蓝天排松鸟飞图_带装饰器的绘画_译者:李兴球")
李兴球
李兴球的博客是Python创意编程原创博客