使用函数进行绘图#
此示例说明如何定义绘制对象的函数。比如 draw_tree(x, y)
和 draw_bird(x, y)
。
有关更多信息,请参见 Chapter 9 of Arcade Academy - Learn Python 或者观看下面的视频:
Drawing with Functions from Paul Vincent Craven on Vimeo.
drawing_with_functions.py#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | """ Example "Arcade" library code. This example shows how to use functions to draw a scene. It does not assume that the programmer knows how to use classes yet. A video walk-through of this code is available at: https://vimeo.com/167296062 If Python and Arcade are installed, this example can be run from the command line with: python -m arcade.examples.drawing_with_functions """ # Library imports import arcade # Constants - variables that do not change SCREEN_WIDTH = 600 SCREEN_HEIGHT = 600 SCREEN_TITLE = "Drawing With Functions Example" def draw_background(): """ This function draws the background. Specifically, the sky and ground. """ # Draw the sky in the top two-thirds arcade.draw_lrtb_rectangle_filled(0, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_HEIGHT * (1 / 3), arcade.color.SKY_BLUE) # Draw the ground in the bottom third arcade.draw_lrtb_rectangle_filled(0, SCREEN_WIDTH, SCREEN_HEIGHT / 3, 0, arcade.color.DARK_SPRING_GREEN) def draw_bird(x, y): """ Draw a bird using a couple arcs. """ 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(x, y): """ This function draws a pine tree at the specified location. """ # Draw the triangle on top of the trunk arcade.draw_triangle_filled(x + 40, y, x, y - 100, x + 80, y - 100, arcade.color.DARK_GREEN) # Draw the trunk arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, y - 100, y - 140, arcade.color.DARK_BROWN) def main(): """ This is the main program. """ # Open the window arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) # Start the render process. This must be done before any drawing commands. arcade.start_render() # Call our drawing functions. draw_background() draw_pine_tree(50, 250) draw_pine_tree(350, 320) draw_bird(70, 500) draw_bird(470, 550) # Finish the render. # Nothing will be drawn without this. # Must happen after all draw commands arcade.finish_render() # Keep the window up until someone closes it. arcade.run() if __name__ == "__main__": main() |