Modern art

Want to try making some computer-generated modern art? Just for fun, try the code in listing 16.6. You can start with what you had from listing 16.5 and modify it, or just start from scratch.

Listing 16.6 Using draw.rect to make art import pygame, sys, random pygame.init()

screen = pygame.display.set_mode([64 0,48 0]) screen.fill([255, 255, 255]) for i in range (100):

width = random.randint(0, 250) height = random.randint(0, 100) top = random.randint(0, 4 00) left = random.randint(0, 500)

pygame.draw.rect(screen, [0,0,0], [left, top, width, height], 1) pygame.display.flip()

while True:

for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()

Run this and see what you get. It should look something like this:

Modern Art Random Lines

Do you understand how the program works? It draws one hundred rectangles with random sizes and positions. To make it even more "artsy," add some color and make the line width random too, as in listing 16.7.

Listing 16.7 Modern art with color import pygame, sys, random from pygame.color import THECOLORS

pygame.init()

screen = pygame.display.set_mode([64 0,48 0])

screen.fill([255, 255, 255])

for i in range (100):

width = random.randint(0, 250)

height = random.randint(0, 100)

top = random.randint(0, 4 00)

Don't worry about

left = random.randint(0, 500)

how this line works

color name = random.choice(THECOLORS.keys()) •a ""

for now

color = THECOLORS[color name]

line width = random.randint(1, 3)

pygame.dra¥.rect(screen, color, [left, top, width,

height], line width)

pygame.display.flip()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

When you run this, you'll get something that looks different every time. If you get one that looks really nice, give it a fancy title like "Voice of the Machine" and see if you can sell it to your local art gallery!

+3 0

Responses

  • Baldo
    How to make modern art?
    6 years ago
  • JANA
    How to do artwork on lines?
    6 years ago

Post a comment