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:
1 |
pip install pygame |
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:
1 |
import pygame |
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:
1 |
pygame.init() |
Step 4: Create the window
We need a window to draw the filled circle. Create a window by adding the following lines of code:
1 2 3 4 5 6 7 8 |
# Window dimensions width, height = 800, 600 # Create the window screen = pygame.display.set_mode((width, height)) # Set window title pygame.display.set_caption("Filled Circle") |
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:
1 2 3 4 5 6 7 |
# Circle properties cx, cy = width // 2, height // 2 radius = 100 color = (255, 0, 0) # Red color (RGB) # Draw the filled circle pygame.draw.circle(screen, color, (cx, cy), radius) |
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:
1 2 3 4 5 6 7 8 9 10 |
# Main loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.display.flip() pygame.quit() |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import pygame pygame.init() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Filled Circle") cx, cy = width // 2, height // 2 radius = 100 color = (255, 0, 0) pygame.draw.circle(screen, color, (cx, cy), radius) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.display.flip() pygame.quit() |
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!