📜  在Python中使用 Arcade 库绘制一棵树

📅  最后修改于: 2022-05-13 01:55:50.838000             🧑  作者: Mango

在Python中使用 Arcade 库绘制一棵树

在Python中使用乌龟模块绘制一棵树并不是一项艰巨的任务。但是,如果您也可以使用 Arcade 模块绘制它呢? Arcade 是一个面向对象的库。它可以像使用导入拱廊的任何其他Python包一样安装。

方法:

  • 进口街机。
  • 定义一个函数来绘制树。在这里,我们正在绘制一棵由矩形和三角形组成的松树。因此,您可以使用内置的街机函数来绘制矩形和三角形。
def draw_tree(x, y):

   # 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)
  • 现在,既然我们已经定义了树的函数,那么让我们定义主函数,并在其下定义 arcade.open_window() 来指定屏幕宽度、高度和标题。此外,使用 arcade.start_render() 和 arcade.finish_render 指示街机模块何时开始和停止绘图。最后,添加 arcade.run() 以指定结尾。
def main():
   # Open the window
   arcade.open_window(600, 600,"TREE")
   arcade.set_background_color(arcade.color.SKY_BLUE)
   
# Start the render process. 
   arcade.start_render()
  
 # Call our drawing functions.
   draw_tree(50, 250)
 
  # Finish the render.
   arcade.finish_render()
  
 # keep the window up .
   arcade.run()
   main()



示例 1:

Python3
import arcade
 
 
def draw_tree(x, y):
 
    # 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():
 
    # Open the window
    arcade.open_window(600, 600, "TREE")
    arcade.set_background_color(arcade.color.SKY_BLUE)
 
    # Start the render process.
    arcade.start_render()
 
    # Call our drawing functions.
    draw_tree(50, 250)
 
    # Finish the render.
    arcade.finish_render()
 
    # Keep the window up.
    arcade.run()
 
 
main()


Python3
#import module
import arcade
 
# specigy spacing
Column_spacing = 20
Row_spacing = 20
Left_margin = 110
Bottom_margin = 400
 
# Open the window and set the background
arcade.open_window(700, 700, "BOX")
 
# set background color
arcade.set_background_color(arcade.color.BABY_PINK)
 
# Start the render process. This must be done before any drawing commands.
arcade.start_render()
 
# Loop for each row
for row in range(8):
    # Loop for each column
    for column in range(8):
        # Calculate our location
        x = column * Column_spacing + Left_margin
        y = row * Row_spacing + Bottom_margin
 
        # Draw the item
    arcade.draw_triangle_filled(x + 40, y,
                                x, y - 100,
                                x + 80, y - 100,
                                arcade.color.DARK_GREEN)
 
arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, 300, 230,
                                      arcade.color.DARK_BROWN)
 
# Finish the render.
arcade.finish_render()
 
# Keep the window up until someone closes it.
arcade.run()
 
# This code is contributed by pulkitagarwal03pulkit


输出:

示例 2:

蟒蛇3

#import module
import arcade
 
# specigy spacing
Column_spacing = 20
Row_spacing = 20
Left_margin = 110
Bottom_margin = 400
 
# Open the window and set the background
arcade.open_window(700, 700, "BOX")
 
# set background color
arcade.set_background_color(arcade.color.BABY_PINK)
 
# Start the render process. This must be done before any drawing commands.
arcade.start_render()
 
# Loop for each row
for row in range(8):
    # Loop for each column
    for column in range(8):
        # Calculate our location
        x = column * Column_spacing + Left_margin
        y = row * Row_spacing + Bottom_margin
 
        # Draw the item
    arcade.draw_triangle_filled(x + 40, y,
                                x, y - 100,
                                x + 80, y - 100,
                                arcade.color.DARK_GREEN)
 
arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, 300, 230,
                                      arcade.color.DARK_BROWN)
 
# Finish the render.
arcade.finish_render()
 
# Keep the window up until someone closes it.
arcade.run()
 
# This code is contributed by pulkitagarwal03pulkit

输出:-