Draw an Arc Using Arcade in Python


Python is an extensively used programming language. The presence of wide range of libraries and tools makes it a popular language. One of the libraries of python is Arcade. This library works as a multimedia library and provides graphic tools for the creation of 2D games, objects used in graphic applications and many more. In this article, we will be drawing an arc using Arcade library in Python. We will be using two different methods for completing the task provided to us. Let’s start by understanding the basics of Arcade library.

Arcade Library

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.

Features of Arcade library

Following are the main features of the Arcade library –

  • This library provides a wide range of shapes, such as lines, circle, rectangle, etc which can be modified using different effects and can add more beauty to the normal shape.

  • You can create games and add realistic views by using different functions in this library.

  • This library can work on more than one platform that is it has cross-platform compatibility.

  • This library eases the path of creating animated objects due to the presence of sprites. Sprites are objects which can be moved, and interacted.

Draw an arc using the Arcade library

There are two methods of drawing arc using Arcade library. Let’s use both of them for completing the task. Since, Arcade is a built in library, it does not need to be installed in the system. You just have to import it in your code.

Using the draw_arc_filled() Method

The arc drawn by using this method will be filled.

  • We have to start the code by importing the Arcade library. By import a library the user can access the functions and methods of it. If the library is not imported it will show ‘NameError’ which means the library is not defined.

  • After importing the library we define the dimensions of the screen as ‘SCR_WIDTH’ and ‘SCR_HEIGHT’. A window is created by using ‘arcade.open_window()’ function and a background color is set.

  • For drawing an arc we need to define certain points such as center point (center_x, center_y), radius, angles (start and end angle), and width. Each point is defined in the code using different functions.

  • After defining all the parameters for drawing the arc, the next step is to use the parameters and draw the arc. This is done by using the ‘on_draw()’ function. This is a predefined function in Arcade library which gets called automatically by the arcade.run() function.

Here the delta_time parameter represents the time that has passed sin the last on_draw() function was called. This delta_time parameter generates consistency in between the use. This is how an arc is drawn by using the Arcade library.

Example

Let’s see an example –

import arcade
# give dimensions of the screen
SCR_WIDTH = 640
SCR_HEIGHT = 480
# for creating window
window = arcade.open_window(SCR_WIDTH, SCR_HEIGHT, "Drawing an Arc")
arcade.set_background_color(arcade.color.WHITE)
# Define the center point of the arc
center_x = SCR_WIDTH // 2
center_y = SCR_HEIGHT // 2

# Define the radius of the arc
radius = 400
# Define the start and end angles of the arc in degrees
start_angle = 60
end_angle = 150
def on_draw(delta_time):
    # Clear the screen
    arcade.start_render()

    # Draw the arc
    arcade.draw_arc_filled(center_x, center_y, radius, radius, arcade.color.BLUE, start_angle, end_angle)

# Run the game loop
arcade.schedule(on_draw, 1 / 60)
arcade.run()

Output

Using the draw_arc_outline() Method

In this method the arc drawn will be outlined arc. All the parameters taken in the earlier method will be same in this but the exception lies in the line_width of the arc.

Example

import arcade
# give dimensions of the screen
SCR_WIDTH = 640
SCR_HEIGHT = 480
# for creating window
window = arcade.open_window(SCR_WIDTH, SCR_HEIGHT, "Drawing an Arc")
arcade.set_background_color(arcade.color.WHITE)

# Define the center point of the arc
center_x = SCR_WIDTH // 2
center_y = SCR_HEIGHT // 2

# Define the radius of the arc
radius = 400

# Define the start and end angles of the arc in degrees
start_angle = 60
end_angle = 150

# Set the width of the arc
line_width = 5
def on_draw(delta_time):
    # Clear the screen
    arcade.start_render()

    # Draw the arc
    arcade.draw_arc_outline(center_x, center_y, radius, radius, arcade.color.BLUE, start_angle, end_angle, line_width)

# Run the game loop
arcade.schedule(on_draw, 1 / 60)
arcade.run()

Output

Both the methods are providing different ways of creating an arc. You can add more features to your arc by exploring other functions present in the Arcade library.

You may face some common errors while drawing an arc. When you use the arcade.draw_arc() function, you pass several parameters under it. Make sure the parameters are not incorrect and are in the correct order. Next, coordinates play an important role while drawing any object. Sometimes outdated version of the library may cause errors.

Conclusion

In this article, we have started from the basics of Arcade library. For using a library first and most important thing understand the basic concept of it. This helps in using the functions properly since every function takes some parameters which are important for creating an object. Arcade library eases the process of creating 2D objects and offers different methods and functions for completing the task.

Updated on: 11-Oct-2023

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements