Home  >  Article  >  Backend Development  >  Draw a circle using Arcade in Python3

Draw a circle using Arcade in Python3

WBOY
WBOYforward
2023-09-14 18:57:02771browse

Arcade is a Python library for creating 2D games and applications. It is an easy-to-use library that provides various functions to create interfaces for drawing shapes and images on the screen. In this article, we will use Arcade and draw a circle in Python3.

Install Arcade

Before we start drawing circles, we need to install the Arcade library. You can install it using Python's package manager pip -

Pip install arcade

Draw a circle in Arcade

Method 1: Use arcade.draw_circle() method

We can use the draw_circle method of the Arcade module to draw circles on the screen. The algorithm below explains the steps for drawing a circle.

grammar

arcade.draw_circle_filled(x, y, radius, color)

The parameters passed to the arcade.draw_circle function are -

  • x - The x-coordinate of the center point of the circle.

  • y - The y-coordinate of the center point of the circle.

  • radius - The radius of the circle.

  • color - Color of the circle, specified as an arcade.color constant, a tuple of RGB values, or a tuple of RGBA values. For example, you can use arcade.color.RED to specify a red circle, or (255, 0, 0) to specify the same color using an RGB value.

algorithm

  • Import the arcade library.

  • Set the window width and height.

  • Use the open_window function to create a window and pass in the width, height and title of the window.

  • Use the set_background_color function to specify the background color of the window. In this case, we set it to white.

  • Use the start_render function to start the rendering process.

  • Define the center point, radius and color of the circle we want to draw.

  • Use the draw_circle_filled function to draw a circle, passing in the center point, radius and color.

  • Use the finish_render function to complete the rendering process.

  • Use the run function to start the event loop, which displays the window and waits for user input.

Example

In the example below, the arcade library is used to create a window and draw a red circle in the center of the window. First, the width and height of the window are set to 640 and 480 pixels respectively. The background color is set to white and the rendering process begins. Then use the arcade.draw_circle_filled() function to draw a red circle, specifying center coordinates, radius, and color. Finally, the rendering process completes and the window is displayed until the user closes it using arcade.run().

import arcade

# Set up the window
WIDTH = 640
HEIGHT = 480
window = arcade.open_window(WIDTH, HEIGHT, "Drawing a Circle")

# Set the background color
arcade.set_background_color(arcade.color.WHITE)

# Start the render process
arcade.start_render()

# Draw a red circle in the center of the screen
x = WIDTH / 2
y = HEIGHT / 2
radius = 100
color = arcade.color.RED
arcade.draw_circle_filled(x, y, radius, color)

# Finish the render process and display the window
arcade.finish_render()
arcade.run()

Output

Draw a circle using Arcade in Python3

Method 2: Use arcade.create_ellipse_filled() method

arcade.create_ellipse_filled() function can be used to draw a filled ellipse on the screen (can be used to draw circles).

grammar

arcade.draw_ellipse_filled(x, y, width, height, color)

The parameters passed to the arcade.draw_ellipse_filled() function are -

  • x - The x-coordinate of the center point of the ellipse.

  • y - The y-coordinate of the center point of the ellipse.

  • Width - The width of the ellipse.

  • Height - The height of the ellipse.

  • color - Color of the ellipse, specified as an arcade.color constant, a tuple of RGB values, or a tuple of RGBA values.

algorithm

  • Use the import arcade statement to import the Arcade library.

  • Set the width and height of the window by creating WIDTH and HEIGHT constants.

  • Use the arcade.open_window() function to create a new window and pass in WIDTH, HEIGHT and window title as parameters.

  • Use the arcade.set_background_color() function and pass in the Arcade color constant to set the background color of the window.

  • Use the arcade.start_render() function to start the rendering process.

  • Define the center point of the ellipse by calculating the x-coordinate as WIDTH / 2 and the y-coordinate as HEIGHT / 2.

  • Define the width and height of the ellipse as width = 100 and height = 100.

  • Define the color of the ellipse as arcade.color.BLUE.

  • Use the arcade.draw_ellipse_filled() function to draw a filled ellipse, passing in the center point, width, height and color as parameters.

  • Use the arcade.finish_render() function to end the rendering process.

  • Use the arcade.run() function to start the event loop, which will keep the window open until the user closes it.

Example

In the example below, the arcade library is used to create a window and draw a blue oval in the center of the window. First, the width and height of the window are set to 640 and 480 pixels respectively. The background color is set to white and the rendering process begins. Then use the arcade.draw_ellipse_filled() function to draw a blue ellipse, specifying the center coordinates, width, height, and color. Finally, the rendering process completes and the window is displayed until the user closes it using arcade.run().

import arcade

# Set up the window
WIDTH = 640
HEIGHT = 480
window = arcade.open_window(WIDTH, HEIGHT, "Drawing a Circle")

# Set the background color
arcade.set_background_color(arcade.color.WHITE)

# Start the render process
arcade.start_render()

# Draw a red circle using the create_ellipse_filled function
x = WIDTH / 2
y = HEIGHT / 2
width = 100
height = 100
color = arcade.color.BLUE
arcade.draw_ellipse_filled(x, y, width, height, color)

# Finish the render process and display the window
arcade.finish_render()
arcade.run()

Output

Draw a circle using Arcade in Python3

in conclusion

In this article, we discussed how to create a circle using the arcade library in Python. We need to create a window and background color and use the draw_circle_filled function to draw circles on the screen. Arcade helps create 2D games, circles, and other shapes in Python.

The above is the detailed content of Draw a circle using Arcade in Python3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete