-
Notifications
You must be signed in to change notification settings - Fork 32
Purogo MyFirstTower
Thomas E. Enebo edited this page May 20, 2012
·
1 revision
In Purogo/MyFirst3DDrawing we noticed that our cube's bottom and top had very similiar logic. Let's refactor this a little bit:
turtle("tower") do |*args|
dimension = (args[0] || 5).to_i
stories = (args[1] || 3).to_i
block_type = (args[1] || :stone).to_sym
# Draw base of cube
square do
4.times do |i|
mark i
forward dimension
turnleft 90
end
end
pillars do
4.times do |i|
goto i
turnup 90
forward dimension
end # Still at top of last pillar
turndown 90
end
stories.times do
square
pillars
end
square
end
This looks much different than cube but this is only because we wrapped the logic for our square into a named block called square and the logic for our pillars into pillars (See Purogo/MakingNamedBlocks to reacquiant yourself with named blocks).
The main algorithm now for building a tower is becoming pretty concise:
stories.times do
square
pillars
end
square
For each story we draw a square and then pillars from that square. So long as we are building new stories the sqare of the next floor will be the top of the previous floor. Except for the very top-most floor of the tower. So we add one extra square to cap off the tower.