52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
import pygame
|
|
import math
|
|
import sys
|
|
|
|
# --- Pygame setup ---
|
|
pygame.init()
|
|
WIDTH, HEIGHT = 800, 800
|
|
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
clock = pygame.time.Clock()
|
|
|
|
# --- Colors ---
|
|
BLACK = (0, 0, 0)
|
|
colors = [(255, 100, 100), (100, 255, 150), (100, 150, 255), (255, 255, 100)]
|
|
|
|
# --- Fibonacci sequence generator ---
|
|
def fibonacci(n):
|
|
fibs = [0, 1]
|
|
for i in range(2, n):
|
|
fibs.append(fibs[-1] + fibs[-2])
|
|
return fibs
|
|
|
|
# --- Spiral drawing ---
|
|
def draw_fibonacci_spiral(n, angle_offset):
|
|
fibs = fibonacci(n)
|
|
cx, cy = WIDTH//2, HEIGHT//2
|
|
scale = 0.05 # scale down Fibonacci numbers to fit screen
|
|
|
|
for i, f in enumerate(fibs[1:], 1): # skip the first 0
|
|
angle = i * 137.5 + angle_offset # golden angle in degrees
|
|
rad = math.radians(angle)
|
|
x = cx + f * math.cos(rad) * scale
|
|
y = cy + f * math.sin(rad) * scale
|
|
color = colors[i % len(colors)]
|
|
pygame.draw.circle(screen, color, (int(x), int(y)), max(int(f*scale*0.5)+2, 2))
|
|
|
|
# --- Main loop ---
|
|
angle_offset = 0
|
|
running = True
|
|
while running:
|
|
screen.fill(BLACK)
|
|
draw_fibonacci_spiral(50, angle_offset)
|
|
angle_offset += 1 # rotate the spiral over time
|
|
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
running = False
|
|
|
|
pygame.display.flip()
|
|
clock.tick(60)
|
|
|
|
pygame.quit()
|
|
sys.exit() |