绘制龟模块

时间:2016-09-19 17:22:29

标签: turtle-graphics

self.navigationController!.isNavigationBarHidden = true

我不明白这部分代码。 from turtle import * color('red', 'yellow') begin_fill() while True: forward(200) left(170) if abs(pos()) < 1: break end_fill() done() 这是什么意思?

3 个答案:

答案 0 :(得分:1)

abs(pos())表示绝对位置。 if abs(pos())<1:意味着你回到起点。希望它向你澄清。

答案 1 :(得分:0)

此代码用红色线条绘制星星,并用黄色填充。 abs(pos()) < 1语句用于在执行while语句的每次迭代后将当前乌龟位置与原始起始乌龟位置进行比较。如果乌龟位置距离不足1个单位,则while语句终止,并且执行end_fill()语句以完成黄色填充。

注释if语句并观察会发生什么,还尝试在abs(pos())<1表达式中使用不同的数字,包括10、20、30等,以查看效果。

答案 2 :(得分:0)

# Another option is to use 'if t.heading() == 0:'.
# If my understanding is not too far wrong,
# when 'turtle.heading == 0' then it's facing 'east',
# the direction it started drawing from.
# This has worked for all the angles I've tried so far.
# Using  'if t.heading() == 0:'  I've been able to draw
# images anywhere on the screen,
# not just at the origin (0,0).
# Maybe there's a way to draw images at different
# locations using 'if abs(pos()) < 1:',
# but I couldn't figure out how.

# Yes, I know! It'd be better to use objects!
# But I wanted to stay as close as possible to
# the original post.

##############################
import turtle

wn = turtle.Screen()
wn.title("Drawing Geometric Shapes")

t = turtle.Turtle()

t.color('red', 'yellow')
t.speed(0)

#############################
x1 = -470
y1 = 300
length = 100
angle = 120

t.penup()
t.goto(x1, y1)
t.pendown()

t.begin_fill()
while True:
    t.forward(length)
    t.left(angle)
    if t.heading() == 0: ###
        break
t.end_fill()
#############################
x2 = 360
y2 = 320
length = 100
angle = 160

t.penup()
t.goto(x2, y2)
t.pendown()

t.begin_fill()
while True:
    t.forward(length)
    t.left(angle)
    if t.heading() == 0: ### 
        break
t.end_fill()
#############################
x3 = -450
y3 = -340
length = 100
angle = 100

t.penup()
t.goto(x3, y3)
t.pendown()

t.begin_fill()
while True:
    t.forward(length)
    t.left(angle)
    if t.heading() == 0: ###
        break
t.end_fill()
#############################
x4 = 360
y4 = -340
length = 100
angle = 170

t.penup()
t.goto(x4, y4)
t.pendown()

t.begin_fill()
while True:
    t.forward(length)
    t.left(angle)
    if t.heading() == 0: ###
        break
t.end_fill()
#############################
x5 = -360
y5 = 0
length = 750
angle = 178

t.penup()
t.goto(x5, y5)
t.pendown()

t.begin_fill()
while True:
    t.forward(length)
    t.left(angle)
    if t.heading() == 0: ###
        break
t.end_fill()
#############################