Color Flats & Butterfly
Growing Flats
Similar to growing bars, we can make a growing flat:
grow_flat, one color
# grow_flat, black
def grow_flat(x, y, z, d):
for i in range(-d, d+1):
for j in range(-d, d+1):
block((x+i),(y+j),z, 1) # box_black
if d < 5:
game.set_timer(1, grow_flat, x, y, z, d+1)
grow_flat(0, 10, -30, 0)
This is fixed color(black) square that keeps growing in size.
grow_flat2, changing color
Add the flexibility of material number m
, we have a color changing square that grow bigger, each square is single color.
# grow_flat2, color starts at black ends with green
def grow_flat2(x,y,z,d, m):
for i in range(-d, d+1):
for j in range(-d, d+1):
block((x+i), (y+j), z, m)
if d < 5:
game.set_timer(1, grow_flat2, x, y, z, d+1, m+1)
grow_flat2(20, 10, -30, 0, 1)
Run them and check out the video.
Butterfly
Manipulate the variables, let's have some fun:
# butterfly
def butterfly(x,y,z,d,m):
for j in range(-d, d+1):
block(x-d, y+j, z, m)
block(x+d, y+j, z, m)
if d < 10:
game.set_timer(1, butterfly, x, y, z, d+1, m+1)
butterfly(-25, 10, -40, 0, 48)
# butterfly2
def butterfly2(x,y,z,d,m):
for i in range(-d, d+1):
block(x+i, y+d, z, m)
block(x+i, y-d, z, m)
if d < 10:
game.set_timer(1, butterfly2, x, y, z, d+1, m+1)
butterfly2(-25, 40, -40, 0, 48)
See a still picture.
====end here
color_quilt
Fit two butterflies together, we get a color quilt on top of the black flat:
# quilt, color square
def quilt(x, y, z, d, m):
for i in range(-d, d+1):
block(x+i, y+d, z, m)
block(x+i, y-d, z, m)
for j in range(-d, d+1):
block(x-d, y+j, z, m)
block(x+d, y+j, z, m)
if d < 10:
game.set_timer(1, quilt, x, y, z, d+1, m+1)
quilt(0, 40, -40, 0, 48)
quilt evolution
Remember how quilt is generated? Each second a larger frame is made outside the previous one. So in the timer, if I increase z
value, the new frame will use the updated z
after each time delay. Then all the different colored frames won't be in one x-y
plane anymore, they will seperate along z-axis, let's check it out:
frames along z
# frames along z (increase z in timer)
# framesZ
def framesZ(x,y,z,size,m):
for i in range(-size,size+1):
game.set_block(Position((x+i),(y+size),z),m)
game.set_block(Position((x+i),(y-size),z),m)
for j in range(-size,size+1):
game.set_block(Position((x-size),(y+j),z),m)
game.set_block(Position((x+size),(y+j),z),m)
if size<10:
game.set_timer(1,framesZ,x,y,z+1,size+1,m+1)
framesZ(0,20,-40,0,67)
# frames along x (increase x in timer)
# framesX
def framesX(x,y,z,size,m):
for i in range(-size,size+1):
game.set_block(Position((x+i),(y+size),z),m)
game.set_block(Position((x+i),(y-size),z),m)
for j in range(-size,size+1):
game.set_block(Position((x-size),(y+j),z),m)
game.set_block(Position((x+size),(y+j),z),m)
if size<10:
game.set_timer(1,framesX,x+10,y,z,size+1,m+1)
framesX(0,20,-40,0,67)
Split Quilt
Thoughtfully manipulate arguments, loops and recursion, we can produce amazing structures in CodeCraft.
Also based on quilt()
, there are 4 lines of game.set_block()
under for
loops. We change z
to be z+i
or z+j
one by one, so we can see how these argument values affect the structure.
def split_quilt1(x,y,z,size,m):
for i in range(-size,size+1):
game.set_block(Position((x+i),(y+size),z+i),m) # updated z
game.set_block(Position((x+i),(y-size),z),m)
for j in range(-size,size+1):
game.set_block(Position((x-size),(y+j),z),m)
game.set_block(Position((x+size),(y+j),z),m)
if size<10:
game.set_timer(1,split_quilt1,x,y,z,size+1,m+1)
split_quilt1(0,20,-40,0,67)
We can replace z in each line one by one, to check how this argument affect the shape of the structure. Here is the final split_quilt()
:
# split_quilt
def split_quilt1(x,y,z,size,m):
for i in range(-size,size+1):
game.set_block(Position((x+i),(y+size),z+i),m) # updated z
game.set_block(Position((x+i),(y-size), z+i),m)
for j in range(-size,size+1):
game.set_block(Position((x-size),(y+j), z+j),m)
game.set_block(Position((x+size),(y+j), z+j),m)
if size<10:
game.set_timer(1,split_quilt1,x,y,z,size+1,m+1)