Generative art: let the code create it
Generative art is art the code makes. You write the rules. Then a loop and a little randomness paint something new every run.
The big idea
Generative art uses loops and random numbers, so the computer makes a fresh piece each time.
See it in code
Generative art starts with a loop. A loop repeats your steps for you. Here it draws six stars in a neat row — you wrote the star line just once:
from art import Canvas, star
screen = Canvas.create()
Canvas.fill(screen)
for i in range(6):
star(screen, 60 + i * 75, 250)
The loop did the drawing. You made the rule, it made the row.
Now add a little randomness. random.randint picks a surprise x each time around. We seed it so you see this exact spread:
import random
from art import Canvas, star
screen = Canvas.create()
Canvas.fill(screen)
random.seed(1)
for i in range(12):
x = random.randint(20, 480)
star(screen, x, 250, size=30)
A loop plus randomness. That's the whole recipe for generative art.
Now the full starfield. The loop runs 40 times. Each time, randomness picks a star's spot, size, and color. We seed it so you see this exact sky — take the seed out, and every run is different:
import random
from art import Canvas, PALETTE, star
screen = Canvas.create()
Canvas.fill(screen)
random.seed(3)
for i in range(40):
x = random.randint(20, 480)
y = random.randint(20, 480)
size = random.randint(15, 45)
star(screen, x, y, size=size, color=random.choice(PALETTE))
You didn't place a single star by hand. The loop and the random numbers did it. That's the magic: you make the rules, and the computer surprises you.
Generative ideas show up in real art and games. A game builds random worlds. A website makes patterns that never repeat. You set the rules, add randomness, and let the machine create.
Try it yourself
Change range(40) to range(120) for a denser galaxy. Then change the seed from 3 to any number and discover a brand-new sky.
The common mistake
Making it too random, so it looks like a mess. Good art has limits — a size from 15 to 45, colors from a set PALETTE. The rules are what make it look nice.
What it unlocks
Generative art combines for loops, the random module, and randomness in art.