【问题标题】:Why does my code print the pattern in different places when I repeat the pattern?当我重复模式时,为什么我的代码会在不同的地方打印模式?
【发布时间】:2020-11-15 12:20:09
【问题描述】:

当你重复图案时,它总是在不同的地方打印雪人,只是雪人的身体到处都是,但眼睛、纽扣等都是正确的。我认为是因为构成身体的圆圈大小不同,而且是对角线打印的。请帮忙,非常感谢。

# Import required module 
import turtle 
  
# Create turtle object 
t = turtle.Turtle() 
  
# Create a screen  
screen =turtle.Screen() 
  
# Set background color 
screen.bgcolor("sky blue") 
      
# Function to draw body of snowman 
def draw_circle(color, radius, x, y):
    t.penup() 
    t.fillcolor (color) 
    t.goto (x, y) 
    t.pendown() 
    t.begin_fill() 
    t.circle (radius) 
    t.end_fill() 

# Function to draw arms  
def create_line(x, y, length, angle): 
    t.penup() 
    t.goto(x, y) 
    t.setheading(angle) 
    t.pendown() 
    t.forward(length) 
    t.setheading(angle + 20) 
    t.forward(20) 
    t.penup() 
    t.back(20)
    t.pendown() 
    t.setheading(angle - 20) 
    t.forward(20) 
    t.penup() 
    t.home() 

# Illustrating snowman  
# Drawing snowman body 
def snowman(x):
    t.pensize(3)
    draw_circle ("#ffffff", 30 , 0 + x*100, -40) 
    draw_circle ("#ffffff", 40 , 0 + x*100, -100) 
    draw_circle ("#ffffff", 60 , 0 + x*100, -200) 
    # Drawing left eye 
    draw_circle ("#ffffff", 2 , -10 + x*100, -10)  
    # Drawing right eye 
    draw_circle ("#ffffff", 2 , 10 + x*100, -10)  
    # Drawing nose 
    draw_circle ("#FF6600", 3 , 0 + x*100, -15)  
    # Drawing buttons on 
    draw_circle ("#ffffff", 2 , 0 + x*100, -35) 
    draw_circle ("#ffffff", 2 , 0 + x*100, -45) 
    draw_circle ("#ffffff", 2 , 0 + x*100, -55) 
    # Drawing left arm 
    create_line(-35 + x*100, -50, 30, 160)  
    # Drawing right arm 
    create_line(35 + x*100, -50, 30, 20)  
    # Drawing hat 
    t.penup() 
    t.goto (-35 + x*100, 8) 
    t.color ("black") 
    t.pensize (6) 
    t.pendown() 
    t.goto (35 + x*100, 8) 
    t.goto (30 + x*100, 8) 
    t.fillcolor ("black") 
    t.begin_fill() 
    t.left (90) 
    t.forward (15) 
    t.left (90) 
    t.forward (60) 
    t.left (90) 
    t.forward (15) 
    t.end_fill()

repeat = int(input("how many times would you like to repeat the pattern?"))

def penPattern():
    for x in range (repeat):
        snowman(x)
        
penPattern()

【问题讨论】:

    标签: python turtle-graphics python-turtle


    【解决方案1】:

    与开始第二个雪人时的方向相比,开始第一个雪人时,您的乌龟面对的方向不同。

    对于第一个雪人,开始画每个球时,您的乌龟都面向右侧。这将使您传递给draw_circle 的起始位置成为圆的最底点。对于第二个雪人,你的乌龟朝下。这将使您传递给draw_circle 的起始位置成为圆的最左侧点。请参阅此处指示的起始位置和方向:

    要解决这个问题,您应该在完成第一个雪人后立即恢复海龟的方向。您可以通过在 snowman 函数的末尾添加 t.left (90) 来做到这一点:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多