How To Fill A Circle With Color In Python

In this tutorial, we will learn how to fill a circle with color using the Python programming language. We will use the popular graphics library Pygame to create a window and draw a circle filled with color in it.

Step 1: Install Pygame

Before we begin, make sure you have Python installed on your computer. If you don’t, you can download it from the official Python website.

Next, you need to install the Pygame library. You can do this using the Python package manager, pip. Open your terminal (command prompt on Windows) and run the following command:

If you’re on Linux, you might need to use pip3 instead.

Step 2: Import the required libraries

For this tutorial, we’ll only need the Pygame library. Begin by creating a new Python file and add the following import statement at the beginning:

Step 3: Initialize Pygame

Before using any of the functionality provided by Pygame, it needs to be initialized. Add the following line of code after the import statement:

Step 4: Create the window

We need a window to draw the filled circle. Create a window by adding the following lines of code:

Here, we’re defining the window dimensions as 800×600 pixels, and setting the window title to “Filled Circle”.

Step 5: Draw a filled circle

To draw a filled circle, we need to specify the center coordinates, radius, and the color. Add the following lines of code:

We’ve set the circle’s center to be the center of the window, with a radius of 100 pixels, and filled it with red color.

Step 6: Main loop

Now we need to create a main loop that keeps the window open and updates the screen. Add the following lines of code:

This loop keeps running until the user closes the window. Every iteration of the loop updates the screen using pygame.display.flip().

Full Code

Here is the complete code for this tutorial:

Output

Conclusion

In this tutorial, we learned how to fill a circle with color in Python using the Pygame library. You can now experiment with different colors and sizes, or even animate the circle by changing its position inside the main loop. Have fun exploring the capabilities of Pygame!