If anyone is curious how to draw a "flower" using rotated ellipses, the following might help with that.
def flower(width: int, color: tuple, edges: bool=False):
W, H = width, width
# create a flower surface with an alpha channel on which to draw
# the petals
flower = pygame.Surface((W, H), pygame.SRCALPHA, 32).convert_alpha()
R = flower.get_rect()
cx, cy = R.center
# assuming petal height should be half their width
petal_size = (width//2, width//4)
pw, ph = petal_size
radius = pw/2
center_radius = width//10
center_color = (255-color[0], 255-color[1], 255-color[2])
def draw_petal(S, x, y, w, h, angle):
# Create surface for drawing an individual petal
surface = pygame.Surface((w, h), pygame.SRCALPHA, 32).convert_alpha()
# Draw the un-rotated petal
pygame.draw.ellipse(surface, color, (0, 0, w, h), 0)
if edges:
pygame.draw.ellipse(surface, BLACK, (0, 0, w, h), 1)
# Create a new surface with the petal rotated by angle
rot_surface = pygame.transform.rotate(surface, angle)
# Need center of rotated surface to blit (draw) the rotated
# petal at the given (x, y) coordinate
rcx, rcy = rot_surface.get_rect().center
# Draw the center of the rotated petal at (x, y)
S.blit(rot_surface, (x - rcx, y - rcy))
# Petals are drawn at diagonals first, then the horizontal petals,
# then the vertical petals
angles = [
45, 135, 225, 315, # diagonals
0, 180, # horizontal
90, 270 # vertical
]
for a in angles:
# placing petal centers onto circle of radius (petal_width/2)
x, y = map(int, (
radius*math.cos(math.radians(a)), radius*math.sin(math.radians(a))
))
draw_petal(flower, cx+x, cy+y, pw, ph, -a)
# draw flower center (don't remember what it's called)
pygame.draw.circle(flower, center_color, (cx, cx), center_radius)
if edges:
pygame.draw.circle(flower, BLACK, (cx, cx), center_radius, 1)
def draw_flower(S, x, y, flower=flower):
S.blit(flower, (x - cx, y - cy))
return draw_flower
To use this code:
import math
import pygame
BLACK = ( 0, 0, 0)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
pygame.init()
size = (800, 800)
SW, SH = size
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Flower Demo")
done = False
clock = pygame.time.Clock()
# insert above flower code
draw_green_flower = flower(100, GREEN)
draw_red_flower = flower(100, RED)
try:
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
screen.fill(WHITE)
draw_green_flower(screen, SW/2,SH/2)
draw_red_flower(screen, SW/2-100,SH/2-100)
pygame.display.flip()
clock.tick(60)
finally:
pygame.quit()