# # program to draw a box with a hat using turtle # nake it fancy # import turtle # allows us to use turtles import math # need this for the hat :-) # # size of the rectangle's sides # side = 100 # # set up the drawing # # window stuff first wn = turtle.Screen() # draw the window for the picture wn.bgcolor("red") # make the background color red wn.title("A Box With A Hat")# put up a title # turtle (or pen) stuff next box = turtle.Turtle() # create a turtle, called "box" box.shape("turtle") # let's make it a real turtle box.speed(3) # a turtle is slow (10 fastest, 1 slowest) box.color("white") # white contrasts nicely with red box.pensize(3) # make the lines 3 pixels # # draw the box # box.forward(side) # tell box to move forward some box.right(90) # tell box to turn right box.forward(side) # tell box to move forward some box.right(90) # tell box to turn right box.forward(side) # tell box to move forward some box.right(90) # tell box to turn right box.forward(side) # tell box to move forward some # # now let's draw the hat # the length of each side of the hat is calculated using # the Pythagorean theorem for a right triangle; the hat # side lengths x are equal, and form the legs of the triange; # the box length is the hypotenuse c, so x**2 + x**2 = c**2; # solving for x gives sqrt((c**2)/2) # box.right(45) # tell box to turn right half a turn box.forward(math.sqrt((side**2)/2)) # tell box to move forward some box. right(90) # tell box to turn right box.forward(math.sqrt((side**2)/2)) # tell box to move forward some # # now hide the turtle and just sit there, displaying the result # box.hideturtle() # make the turtle invisible wn.mainloop() # wait until the user closes the window