Forest
Build a few trees
Use list of tuples Using list of tuples, we can put trees at selected locations with varying height and width (x,z,h,w):
data = [(-10,-20, 10,3), (-2,-18, 8,4), (15,-15, 20, 6), (20, -25, 12, 3)]
for i in data:
tree( i[0], i[1], i[2], i[3])
Build a line of trees with for loop
Another example to build 10 trees along x-axis:
for i in range(-40, 40, 10):
tree( i, -30, 20, 4 )
Put random material blocks at random locations
Remember in previous lesson, using randint()
to generate random integers, we can put 10 blocks of different materials at random (x,y,z)
locations?
Remind yourself:
# 10 different material blocks at various (x,y,z) locations
for i in range(10):
block(randint(1,20), randint(1,20), randint(-20, -10), randint(1,64))
Many Trees together is a Forest
Build random size trees at random locations
Similarly, we can use randint()
in tree()
to generate a lot of trees at random locations, with random height and width.
This program is to generate 20 random sized trees at random locations:
# Forest
for n in range(1,20):
tree(randint(-20,40), randint(-40, -10), randint(10,20), randint(2,4))
Every time we run the program Forest
, we get a different looking forest,
(see pic 4-03-5
)
A Fantasy Forest:
Call treeColor
with colorful materials such as from 67 to 76, you can build a fantasy forest:
(pic, forestcolor)
# color forest
for n in range(1,20):
treeColor(randint(-40,40), randint(-40, 30), randint(10,20), randint(2,4), 22, randint(67,76))
The world is getting better!