Draw a tree using arcade library in Python
Last Updated :
15 Nov, 2021
Drawing a tree isn’t a tough task in Python using the turtle module. But, what if you can draw it using Arcade Module as well. Arcade is an object-oriented library. It can be installed like any other Python Package using an import arcade.
Approach:
- Import arcade.
- Define a function to draw trees. Here, we are drawing a pine tree made up of a rectangle and a triangle. So, you can use the inbuilt arcade function to rectangle and triangle.
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)
- Now, since we have defined the function to tree, let’s define the main function and under which define arcade.open_window() to specify the screen width, height, and title. Also, use arcade.start_render() and arcade.finish_render to instruct arcade module when to start and stop drawing. Finally, add the arcade.run() to specify the ending.
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()
Example 1:
Python3
import arcade
def draw_tree(x, y):
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 , y - 100 , y - 140 ,
arcade.color.DARK_BROWN)
def main():
arcade.open_window( 600 , 600 , "TREE" )
arcade.set_background_color(arcade.color.SKY_BLUE)
arcade.start_render()
draw_tree( 50 , 250 )
arcade.finish_render()
arcade.run()
main()
|
Output:
Example 2:
Python3
import arcade
Column_spacing = 20
Row_spacing = 20
Left_margin = 110
Bottom_margin = 400
arcade.open_window( 700 , 700 , "BOX" )
arcade.set_background_color(arcade.color.BABY_PINK)
arcade.start_render()
for row in range ( 8 ):
for column in range ( 8 ):
x = column * Column_spacing + Left_margin
y = row * Row_spacing + Bottom_margin
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)
arcade.finish_render()
arcade.run()
|
Output:-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...