Draw a Triangle Using Arcade in Python


Arcade library is one of the built-in libraries of python. It is extensively used in creating a variety of multimedia objects, which can be used in making of 2D games and other applications where graphics are required. In this article, we will be using Arcade library in Python for completing a task of creating a triangle. We will start by a brief introduction of Arcade library, its functions and use.

What is Arcade Library?

Arcade library was developed for removing the limitations of the Py.game module, since it was the only module used by the python game programmers. This library was able to solve the purpose for which it was created that is the development of more realistic graphics for the 2D games. For using the functions under this library, first you need to import the library and then create a window where the object will appear.

For drawing different shapes Arcade has a different function for each shape.

  • 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()

Drawing a Triangle Using the Arcade Library

Example

In the following example, we have drawn a single triangle by using the arcade.draw_triangle_filled() function of the arcade library.

  • We have started the code by creating a window using ‘arcade.open_window()’ function and passed three arguments through it which are screen width, height, and the title.

  • Next, we have set the background color and started the rendering process. After the render has been started we have next defined our coordinates for the triangle which we want to make, along with the color of the triangle.

import arcade
SCR_WIDTH = 640
SCR_HEIGHT = 480

def draw_triangle():
    arcade.open_window(SCR_WIDTH, SCR_HEIGHT, "Drawing a Triangle")
    arcade.set_background_color(arcade.color.ANTIQUE_WHITE)
    arcade.start_render()
    arcade.draw_triangle_filled(
        320, 380,   # x, y coordinate of point A
        220, 180,   # x, y coordinate of point B
        420, 180,   # x, y coordinate of point C
        arcade.color.ROYAL_BLUE    
    )
    arcade.finish_render()
    arcade.run()
draw_triangle() 

Output

Drawing a Hollow Triangle

Similarly, you can make different changes to your objects which you want to create. In this example, we have not filled the triangle on the contrary we have used the arcade.draw_triangle_outline() function.

This function only draws the outline of the triangle. You can add graphics to this code for making the triangle more attractive.

There’s one more method of drawing a triangle that is by using arcade.ShapeElementList() function. Let’s write a code using this function.

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.

Example

import arcade

WIDTH = 640
HEIGHT = 480
TITLE = "Triangle Example"
arcade.open_window(WIDTH, HEIGHT, TITLE)
arcade.set_background_color(arcade.color.WHITE)
arcade.start_render()
# Create a ShapeElementList object
triangle_list = arcade.ShapeElementList()

# Add three line segments to the list to create a triangle
triangle_list.append(arcade.create_line(320, 100, 200, 300, arcade.color.BLUE, 3))
triangle_list.append(arcade.create_line(200, 300, 440, 300, arcade.color.BLUE, 3))
triangle_list.append(arcade.create_line(440, 300, 320, 100, arcade.color.BLUE, 3))
# Draw the ShapeElementList object
triangle_list.draw()

arcade.finish_render()
arcade.run()

Output

Example

Let’s take one more example where we can draw two triangles.

import arcade
WIDTH = 640
HEIGHT = 480
TITLE = "Two Triangles Example"
arcade.open_window(WIDTH, HEIGHT, TITLE)
arcade.set_background_color(arcade.color.WHITE)
arcade.start_render()

# Draw the first triangle
arcade.draw_triangle_outline(
    320, 100,    # Vertex 1 (x, y)
    200, 300,    # Vertex 2 (x, y)
    440, 300,    # Vertex 3 (x, y)
    arcade.color.PURPLE    
)
# Draw the second triangle
arcade.draw_triangle_outline(
    200, 100,    # Vertex 1 (x, y)
    80, 300,     # Vertex 2 (x, y)
    320, 300,    # Vertex 3 (x, y)
    arcade.color.VIVID_BURGUNDY
)
arcade.finish_render()
arcade.run()

Output

Other Shapes using Arcade Library

The arcade.ShapeElementList class is basically a class that handles a list of shape elements to be drawn on the window. Using this class, you can create various shapes by combining shapes to form a complex object.

For using this class—

  • You need to create an instance of the class.

  • Then add shape elements to the list by using append() method and pass the desired shape.

In the previous example of Shape element list objects we have taken three line segments for creating a triangle.

Limitations

But Arcade library has some limitations as well.

  • It does not have features which support the 3D graphics it is only limited to 2D graphics.

  • Many platforms of mobile network and web browsers does not support it, so it can become a problem while you are working on any project related to that.

  • So, this library is more helpful while working on smaller projects and for beginner level learning.

Conclusion

In this article, we have briefly explained what Arcade library is. We have discussed the functions of it which were used while writing the code for drawing a triangle using Arcade. Three different examples are given for a better understanding of the concept applied while working on the code.

Three different approaches includes three different functions which are arcade.draw_triangle_filled(), arcade.draw_triangle_outline, and arcade.ShapeElementList(). Each function draws triangle in a different way.

Updated on: 11-Oct-2023

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements