2012년 4월 7일 토요일

Exercise 3.1–Answer

from: http://openbookproject.net/thinkcs/python/english3e/hello_little_turtles.html#exercises

Write a program to draw a shape like this:

_images/star.png

Hints:

  • Try this on a piece of paper, moving and turning your cellphone as if it was a turtle. Watch how many complete rotations your cellphone makes before you complete the star. Since each full rotation is 360 degrees, you can figure out the total number of degrees that your phone was rotated through. If you divide that by 5, because there are five points to the star, you’ll know how many degrees to turn the turtle at each point.
  • You can hide a turtle behind its invisibility cloak if you don’t want it shown. It will still draw its lines if its pen is down. The method is invoked as tess.hideturtle() . To make the turtle visible abain, use tess.showturtle() .

 

  1: # -*- coding: UTF-8 -*-
  2: #!/usr/bin/env python
  3: 
  4: import turtle           # turtle을 사용할 수 있도록 한다.
  5: 
  6: wn = turtle.Screen()
  7: wn.title("Star")
  8: 
  9: alex = turtle.Turtle()
 10: alex.pensize(5)
 11: alex.speed(1)
 12: 
 13: for i in range(5):
 14:     alex.forward(200)
 15:     alex.right(144)
 16: 
 17: alex.hideturtle()
 18: wn.mainloop()           # 사용자가 윈도우를 닫을 때까지 기다린다.
 19: 

댓글 없음:

댓글 쓰기