Draw a Tree Using Arcade Library in Python


Python is a modern programming language. The wide range of features, such as extensive libraries, simple syntax etc. has increased its popularity in different fields such as web development, data science, and many more. In this article, we will be using one of the libraries of python which is Arcade. This library will help us build a tree using different structures.

What is Arcade Library?

The arcade library was developed for creating more appealing graphics for games and other graphical applications. Less number of choices gave rise to a different library which became a modern python module.

This library offers a wide range of features since it is built on top of Pyglet, a multimedia library that provides a simple interface to the developers for creating new range of games and other applications.

Since this library is a built in library, there’s no need of installing it. You can just import it when you need it. After importing the library, you can take access of the functions under it.

For drawing different shapes Arcade has a different function for each for example

  • arcade.draw_circle_filled

  • arcade.draw_rectangle_filled

  • arcade.draw_polygon_filled

  • arcade.draw_line()

  • arcade.draw_point()

  • arcade.draw_triangle_filled()

  • arcade.draw_ellipse()

Sprites in arcade library have a unique feature, it is an object which can be moved and can create various animations which can be a part of games and other graphical applications. Hence, for adding sprite for performing the functions you can use arcade.Sprite, arcade.SpriteList for updating, etc. You can also create realistic motions using the arcade.PhysicsEngine, arcade.PhysicsEngineSimple.

For making your work more interactive you can add audio to it using the arcade.load_sound, arcade.play_sound, and arcade.set_sound_volume. By combining so many properties together you can create an attractive, useful, and fun application.

We have covered the basic topics now we understand what is arcade library and in what ways it can be used. Let’s draw a tree using this library.

Example

In this code, we have started by importing the arcade library. Now you can use the functions under the arcade library.

  • The first task is to setup the window where you want to draw the object. The coordinates for the screen width and screen height are 640 and 480 respectively.

  • For making the background we have used arcade.set_background_color() function. Before starting the drawing, we have used a render function. This function is called first because it prepares the window for the next step by clearing the previous contents and setting up the background. Similarly, when the drawing is complete you should call the finish render function of the arcade library that is arcade.finish_render().

  • Now, starting the drawing, here we have used arcade.draw_rectangle_filled() function to draw the trunk of the tree, where it takes four argument to finish the task as “arcade.draw_rectangle_filled(400, 200, 50, 200, arcade.color.BROWN_NOSE)”.

    Here, 400 is first argument the x-coordinate of the centre of the rectangle, 200 is second argument the y-coordinate of the centre of the rectangle, 50 is the width, 200 is height of the rectangle, and at last the fourth argument is the color of the trunk.

  • We have drawn the leaves using three circles which are drawn by arcade.draw_circle_filled(). This function also takes four argument as “arcade.draw_circle_filled(350, 300, 60, arcade.color.DARK_GREEN)”. Here, 350 is the x-coordinate of the centre of the circle, 300 is the y-coordinate, 60 is the radius, and last is the fourth argument which represents the color. At the end, arcade.run() function is used which runs the program until the user stops or closes the window.

  • In a similar way you can draw different types of trees where the leaves may be triangular or in other different shapes and can apply different features such as branches using the triangle function and many more.

import arcade
# For setting up the window
window = arcade.open_window(640, 480, "Drawing a Tree using Arcade Library")
# you can set up the background color
arcade.set_background_color(arcade.color.MELLOW_YELLOW)
# render function to be used before drawing starts
arcade.start_render()
# For drawing the trunk of the tree
arcade.draw_rectangle_filled(400, 200, 50,  200, arcade.color.BROWN_NOSE)
# Draw the leaves of the tree
arcade.draw_circle_filled(350, 300, 60, arcade.color.DARK_GREEN)
arcade.draw_circle_filled(400, 350, 60, arcade.color.DARK_GREEN)
arcade.draw_circle_filled(450, 300, 60, arcade.color.DARK_GREEN)

# Finish the render after the diagram completes
arcade.finish_render()
# Keep the window open
arcade.run()

Output

Note: For drawing a perfect object you must keep in mind the coordinates you are giving to the function because coordinates play an important role in adjusting the position of the object.

Conclusion

In this article, we have made a tree using different functions present in the Arcade library. We started the code by importing the library, and then a window was set up for the object. The code is fully explained in the article. In a similar way you can explore different functions of the Arcade library and draw different 2d graphics.

Updated on: 11-Oct-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements