Draw a Sun Using Arcade Library Python


Python is a highly multipurpose programming language with a vast network of libraries and frameworks that has capabilities beyond the standard language features. Arcade is one of the libraries used for creating graphics and handling user input. It uses basic shapes like circles and lines, a gradient approach, and many other techniques for creating attractive graphics. In this article, we will learn different methods for drawing a sun using the Arcade library in Python.

Arcade Library and Its Features

Arcade library is a Python library built on top of Pyglet, a multimedia library for python. It is a python library used for developing 2D games and other graphical applications. It provides beginner-friendly designs while also offering advanced features for experienced developers. It offers many features such as:

Wide ranges of drawing functions are offered in the arcade library where you can start from basic shapes, such as lines, circles, rectangles, and polygons, as well as more complex shapes like Bezier curves.

  • It provides an easy way to create a window and set up the graphics context.

  • It uses modern graphics hardware to change graphics quickly and efficiently.

  • It also has a built-in function for adding sound effects to make the graphics more appealing.

  • Provides easy file loading and other resources in your games.

  • For adding realistic physics to your game or simulation, the Arcade library provides a simple physics engine for the same.

  • You can change the fonts and styles using tools provided by the Arcade library.

  • It has the unique feature of handling the user input by using the mouse and keyboard events.

Hence, Arcade is a complete library that provides a variety of tools for creating an interactive, appealing, and engaging graphical application in Python.

Drawing A Sun Using the Arcade Library

The sun is a common graphical element often used in games and applications. You can draw the sun with Arcade Library in several ways, such as using basic shapes like circles and lines but for making it more real and attractive you can use various functions of Arcade Library. So, let’s start with a basic sun.

You need to install the Arcade library. You can do this by giving a command in the command prompt of your system that is

pip install arcade 

Example

In this example, we have created a basic and simple sun where we have first created a window where the sun will be displayed.

  • The window is created by using the arcade.open_window() function.

  • Then we have defined a function [def draw_sun():] to draw the sun, where arcade.draw_circle_filled() method is used to draw a filled circle in the middle of the screen.

You can customize the object by changing its size and color by adjusting the parameters passed to it in the arcade.draw_circle_filled() method.

import arcade
# Create a window, for the sun to be displayed in 
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 500
window = arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT,"SUN")
# define the function to draw a sun 
def draw_sun():
    arcade.draw_circle_filled(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, 200, arcade.color. ORANGE_RED) # the circle has radius of 200 pixels
@window.event()
def on_draw():
    arcade.start_render()
    draw_sun() # this function is called to add the sun to the window
arcade.run()

Output

Example 2

In this second example, we have imported the math module with the arcade module in Python.

  • This math module will help us to access various mathematical functions, such as trigonometry, logarithms, etc. Here, we have used math.cos(), math.sin() functions to calculate the x and y coordinates of each ray based on the angle.

  • Hence it helps to calculate the endpoints of each ray and each ray comes out from the centre of the sun.

  • The main() function is used to initialize the Arcade window and calls draw_sun() to draw the sun in the centre. The effects, colors, and size, can be changed according to the needs of the user.

import arcade
import math
# defined values for the screen
SCR_WIDTH = 640
SCR_HEIGHT = 400
RAYS_NUM = 50
RAY_LENGTH = 200
RAY_WIDTH = 5
def draw_sun(center_x, center_y, rays_num, ray_length, ray_width):
    angle_step = 2 * math.pi / rays_num
    for i in range(rays_num):
        angle = i * angle_step
        x1 = center_x + ray_length * math.cos(angle)
        y1 = center_y + ray_length * math.sin(angle)
        x2 = center_x + ray_length * math.cos(angle + math.pi)
        y2 = center_y + ray_length * math.sin(angle + math.pi)
# rays can be drawn using arcade.draw_line
        arcade.draw_line(x1, y1, x2, y2, arcade.color.ORANGE_RED, ray_width)
def main():
# create new window
    arcade.open_window(SCR_WIDTH, SCR_HEIGHT, "Spiraling Rays Method")
    arcade.set_background_color(arcade.color.SKY_BLUE)
    arcade.start_render()

    draw_sun(SCR_WIDTH // 2, SCR_HEIGHT // 2, RAYS_NUM, RAY_LENGTH, RAY_WIDTH)

    arcade.finish_render()
    arcade.run()
if __name__ == '__main__':
    main()

Output

Points to be noted

While drawing such objects you should keep certain points in mind such as –

  • Use of appropriate window size.

  • Correct coordinates, for positioning the objects in the window.

  • Use comments while writing the code, it helps you remember, makes it easier for others to understand, and you can debug it later.

Conclusion

In this article, we have used two different approaches for drawing a sun using the Arcade library in Python. In the first method, we simply imported the arcade library gave the constants for the window, opened a new window, and then used functions to draw the sun.

While for the second method, we have used the math module to draw the rays as well which makes the sun look more appealing. In a similar way you can make changes to the styles, and rays, add new graphics, and try to create different graphical applications in Python.

Updated on: 13-Sep-2023

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements