Build a natural looking tree
treeN
, a natural looking tree shouldn't have solid leaves. Instead, there are random empty spaces between leaves.
We will use randint()
in random
module, check to make sure to import randint()
, add this line to the beginning of the program if it's not already there.
from random import randint
Tree leave lines
First we need a vertical leaves line that has random empty spots, Y_tree_line
# Y_tree_line, 'leaves':8
def Y_tree_line(x,y,z,h,m):
for j in range(y, (y+h)):
block(x,randint(y,y+h),z, h, m )
Build 3 y_tree_lines
, these lines break at random locations just like natural tree leaves. Compare these tree leave lines with a solid green bar_y
at the very left.
# three tree lines
Y_tree_line(20, 15, -10, 12, 8)
Y_tree_line(22, 15, -10, 12, 8)
Y_tree_line(24, 15, -10, 12, 8)
# bar_y, at the left
bar_y(1, 15, -10, 12, 8)
(pic, 4-3-3A)
treeN()
Let's use y_tree_line
to build a natural looking tree:
# treeN, total height, h
def treeN(x,z,h):
column(x,z,h, 22)
for i in range(x-1, x+2):
for k in range(z-1,z+2):
Y_tree_line(i, (int(h/2)+2), k, int(h/2), 8)
Here, h
is the total height of the tree, tree top has about half of the total height, h/2
might be a floating number, so I use type conversion int(h/2)
.
treeN(20, -10, 20)
treeN(25, -10, 20)
treeN(30, -10, 20)
(pic here)