The pygame python module does not implement an antialiased filled circle and this is the scope of this tutorial.
The pygame module for drawing shapes are:
- pygame.draw.rect - to draw a rectangle
- pygame.draw.polygon - to draw a polygon
- pygame.draw.circle - to draw a circle
- pygame.draw.ellipse - to draw an ellipse
- pygame.draw.arc - to draw an elliptical arc
- pygame.draw.line - to draw a straight line
- pygame.draw.lines - to draw multiple contiguous straight line segments
- pygame.draw.aaline - to draw a straight antialiased line
- pygame.draw.aalines - to draw multiple contiguous straight antialiased line segments.
Let's install pygame python module.
pip install pygame
Collecting pygame
Downloading pygame-2.5.2-cp312-cp312-win_amd64.whl.metadata (13 kB)
...
Installing collected packages: pygame
Successfully installed pygame-2.5.2
Let's see the source code:
import pygame
import pygame.gfxdraw
TARGET_SIZE = 200
BG_ALPHA_COLOR = (0, 0, 0, 100)
class Target(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((TARGET_SIZE, TARGET_SIZE), pygame.SRCALPHA)
self.rect = self.image.get_rect()
self.color = (255, 0, 0)
self.filled = False
self.width = 1
def DrawTarget(self):
pygame.gfxdraw.aacircle(self.image, int(self.rect.width/2), int(self.rect.height/2),\
int(self.rect.width/2 - 1), self.color)
pygame.gfxdraw.filled_ellipse(self.image, int(self.rect.width/2), \
int(self.rect.height/2), int(self.rect.width/2 - 1), int(self.rect.width/2 - 1), self.color)
temp = pygame.Surface((TARGET_SIZE, TARGET_SIZE), pygame.SRCALPHA)
if not self.filled:
pygame.gfxdraw.filled_ellipse(temp, int(self.rect.width/2), int(self.rect.height/2), \
int(self.rect.width/2 - self.width), int(self.rect.width/2 - self.width), BG_ALPHA_COLOR)
pygame.gfxdraw.aacircle(temp, int(self.rect.width/2), int(self.rect.height/2), \
int(self.rect.width/2 - self.width), BG_ALPHA_COLOR)
self.image.blit(temp, (0, 0), None, pygame.BLEND_ADD)
pygame.init()
screen = pygame.display.set_mode((400, 400))
target = Target()
target.DrawTarget()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255, 255, 255))
screen.blit(target.image, (100, 100))
pygame.display.flip()
pygame.quit()