Learning LibraryDigital Art LibraryKids

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

1Start simple

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:

python
from art import Canvas, star

screen = Canvas.create()
Canvas.fill(screen)

for i in range(6):
    star(screen, 60 + i * 75, 250)
Run it — one loop, six stars:
A row of six small yellow stars across a dark canvas, drawn by a loop.

The loop did the drawing. You made the rule, it made the row.

2A step further

Now add a little randomness. random.randint picks a surprise x each time around. We seed it so you see this exact spread:

python
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)
Run it — twelve stars, scattered by chance:
Twelve yellow stars scattered at random spots along the middle of a dark canvas.

A loop plus randomness. That's the whole recipe for generative art.

3In our world

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:

python
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))
Run it — a whole galaxy from one short loop:
Forty stars of different sizes and colors scattered across a dark canvas like a galaxy.

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.

The same idea, everywhere

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.