海龟绘制程序与用户输入:窗口消失。我做错什么了?

2024-04-26 10:35:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使一个形状,需要为我的变量之一的用户输入,我很困惑如何使size1等于x,并使我的用户输入工作。我做错什么了吗?你知道吗

import turtle
import math


def drawSquareFromCenter(turtle, x): ...


    turtle.penup()
    turtle.forward(-x / 2)
    turtle.right(90)
    turtle.forward(x / 2)
    turtle.left(90)
    turtle.pendown()
    turtle.forward(x)
    turtle.left(90)
    turtle.forward(x)
    turtle.left(90)
    turtle.forward(x)
    turtle.left(90)
    turtle.forward(x)
    turtle.left(90)
    turtle.penup()
    turtle.forward(x / 2)
    turtle.left(90)
    turtle.forward(x / 2)
    turtle.right(90)


def main():
    # Create turtle
    bob = turtle.Turtle()

    # Get user input
    size1 = int(input('Enter size fore top square'))
    size2 = size1 * 2
    size3 = size1 * 3
    size4 = size1 * 4

    # Draw graphics
    bob.forward(size4 / 2)
    bob.right(90)
    bob.forward(-1 * (size4 + size3 + size2 + (size1 / 2)))
    drawSquareFromCenter(bob, size1)

    bob.forward((size1 + size2) / 2)
    drawSquareFromCenter(bob, size2)

    bob.forward((size2 + size3) / 2)
    drawSquareFromCenter(bob, size3)

    bob.forward((size3 + size4) / 2)
    drawSquareFromCenter(bob, size4)

    bob.right(45)
    bob.forward(math.sqrt(size4 / 2) ** 2 + (size4 / 2) ** 2)
    # Press any key to exit
    input()


main()

Tags: 用户importrightinputdefmathleftforward
1条回答
网友
1楼 · 发布于 2024-04-26 10:35:33

您的控制台输入窗口可能隐藏在turtle draw窗口后面。你知道吗

首先要求尺寸,然后做绘图,以避免海龟表面隐藏您的控制台窗口。你知道吗

打印时:

  • 倒立+朝着你的方向走一半的距离+倒立
  • 旋转90,画一半
  • 3次:
    • 旋转90度,画一条边
  • 旋转90度,画出缺失的一半
  • 旋转90+走半个边回到中心
  • 开始时旋转180度至面

可以这样做:

import math
import turtle

def squareFromCurrPosAndRotationAsCenter(bob, s):
    """Starting from current position and rotation, draw a square of sidelength s.
    End on same position and rotation you began with."""
    # goto first side
    bob.penup()
    bob.forward(s/2)
    bob.pendown()
    # draw a half side
    bob.right(90)
    bob.forward(s/2)
    # draw three full sides
    for k in range(3):
        bob.right(90)
        bob.forward(s)
    # draw last half side
    bob.right(90)
    bob.forward(s/2)

    # goto back to origin
    bob.penup()
    bob.right(90)
    bob.forward(s/2)
    # turn back in original direction
    bob.right(180) 

def getInt(text, default):
    """Try to parse input to int, return default if not possible"""
    try:
        return int(input(text))
    except:
        return default

主要功能:

def main():

    # Get user input or (when error) use default
    size        = getInt('Enter size for top square: ', 50)
    num_squares = getInt('Enter the amount of squares: ', 4)
    angle       = getInt('Enter increase of starting angle: ', 60)

    # Create turtle
    bob = turtle.Turtle()

    # adjust speed based on workload
    bob.speed(max(5,(num_squares * 360/angle)//10)) 

    # outer loop changes starting angle
    for startAngle in range(0,360-angle+1,angle):
        bob.setheading(startAngle)


        # we use a list comp to create the desired square sizes
        # you could also do [size, size*2, size*3, size*4] if 
        # you want always 4 circles

        for s in [size*(n+1) for n in range(num_squares)]:
            squareFromCurrPosAndRotationAsCenter(bob, s)

    turtle.mainloop()

main()

layered squares with different starting angles

相关问题 更多 >