-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5-gridColorsAndRandomness.py
63 lines (53 loc) · 1.88 KB
/
5-gridColorsAndRandomness.py
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
# import our special functions from the random library
from random import randrange, random
# define the width and height of the new page
# w h
newPage(1000, 1000)
# define x and y as where we start? (0, 0) is in the bottom left
x = 0
y = 0
# define how many rows and columns
rows = 10
cols = 10
# define the width and height of each unit of the grid
gridWidth = 100
gridHeight = 100
# define the width and height of the shape we will draw in that grid
shapeWidth = 100
shapeHeight = 100
# ok it’s time to loop
# first we will loop through each row
# and then WITHIN that loop we will loop through each column
# for theThing in theRangeOfThings
for row in range(rows):
# everything indented this much runs once for each row
for col in range(cols):
# everything indented this much
# runs once for each column within
# each row (one grid unit)
# before we draw our shape, let’s pick a color for it
# we can create 3 variables
r = random() # red
g = random() # green
b = random() # blue
fill(r, g, b)
# okay, now we finally draw our shape
# we can make a conditional
# if this condition is true:
# then run this code
# if not:
# then run this code
if random() > .5:
rect(x, y, shapeWidth, shapeHeight+randrange(-10, 10))
else:
oval(x, y, shapeWidth, shapeHeight+randrange(-10, 10))
# at the end of the column, advance X to the next column
x += gridWidth
# we are OUTDENTED, which means we are back
# to code that only runs once per row
# now that we’ve reached the end of the row
# we want to advance y to move us up to the next row
y += gridHeight
# and we want to return x to its original position
# (a carriage return, essentially)
x -= cols*gridWidth