-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflowers.py
More file actions
76 lines (62 loc) · 2.01 KB
/
Copy pathflowers.py
File metadata and controls
76 lines (62 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import pygame
import random
import math
import sys
# Initialize Pygame
pygame.init()
# Canvas size
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flower Generator")
clock = pygame.time.Clock()
# Colors
WHITE = (255, 255, 255)
def random_color():
return random.randint(50, 255), random.randint(50, 255), random.randint(50, 255)
def draw_flower1(surface, x, y, size):
petals = random.randint(5, 8)
radius = size
color = random_color()
for i in range(petals):
angle = (2 * math.pi / petals) * i
petal_x = x + math.cos(angle) * radius
petal_y = y + math.sin(angle) * radius
pygame.draw.ellipse(surface, color, (petal_x - size//4, petal_y - size//2, size//2, size))
pygame.draw.circle(surface, (255, 255, 0), (x, y), size // 3)
def draw_flower2(surface, x, y, size):
spikes = random.randint(6, 10)
color = random_color()
for i in range(spikes):
angle = (2 * math.pi / spikes) * i
end_x = x + math.cos(angle) * size
end_y = y + math.sin(angle) * size
pygame.draw.line(surface, color, (x, y), (end_x, end_y), 2)
pygame.draw.circle(surface, (0, 0, 0), (x, y), size // 4)
def generate_flower(surface):
x = random.randint(50, WIDTH - 50)
y = random.randint(50, HEIGHT - 50)
size = random.randint(10, 30)
flower_type = random.choice([draw_flower1, draw_flower2])
flower_type(surface, x, y, size)
# Main loop
running = True
flower_count = 0
max_flowers = 300
screen.fill(WHITE)
try:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if flower_count < max_flowers:
generate_flower(screen)
flower_count += 1
pygame.display.flip()
clock.tick(60)
except Exception as e:
print("An error occurred:", e)
pygame.image.save(screen, "flower_garden_error_output.png")
finally:
print("Cleaning up and quitting.")
pygame.quit()
sys.exit()