Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用Turtle模块将缩小的Turtle向上移动到窗口屏幕_Python_Loops_Turtle Graphics_Python Turtle - Fatal编程技术网

在Python中使用Turtle模块将缩小的Turtle向上移动到窗口屏幕

在Python中使用Turtle模块将缩小的Turtle向上移动到窗口屏幕,python,loops,turtle-graphics,python-turtle,Python,Loops,Turtle Graphics,Python Turtle,我应该定义一个函数,movingTurtle,它使用Python turtle模块,将海龟设置为实际的海龟形状,并将海龟从屏幕底部向上移动到顶部,随着移动而变小。以下是我目前拥有的代码: def movingTurtle(mTurtle, window): ''' Create turtle that is the shape of an actual turtle, then have it move from the bottom of screen to t

我应该定义一个函数,
movingTurtle
,它使用Python turtle模块,将海龟设置为实际的海龟形状,并将海龟从屏幕底部向上移动到顶部,随着移动而变小。以下是我目前拥有的代码:

def movingTurtle(mTurtle, window):
    '''
    Create turtle that is the shape of an actual
    turtle, then have it move from the bottom of screen
    to the top, getting smaller as it moves along its path
    '''
    width = window.window_width()
    height = window.window_height()

    bottom = -height/2
    top = height/2
   
    mTurtle.shape("turtle")
    mTurtle.penup()
    mTurtle.setposition(0, bottom)
    x = int(height/10)
    y = int(height/10)
    z = int(height/10)
    for i in range(bottom, top):
        mTurtle.setposition(0, i)
        #x -= .1
        #y -= .1
        #z -= .1
        #mTurtle.shapesize(x, y, z)

def main():
    # set window size
    width = int(input('Enter the width of the screen: '))
    height = int(input('Enter the height of the screen: '))
    turtle.setup(width,height)
    print('='*50)
    #========================================================
    # get reference to turtle window
    window = turtle.Screen()
    # set window title bar
    window.title('Lab20 - Turtle Object')
    #========================================================    
    # Moving turtle
    mTurtle = turtle.Turtle()
    # function call
    try:
        movingTurtle(mTurtle,window)
    except:
        print('movingTurtle is not either defined or there is a',
              'problem with the function')
    #========================================================

main()

(之所以使用main()部分,是因为我实际上还有几个其他函数——这是用于项目的)

即使最后四行被注释掉了,我甚至不能让海龟从顶部移动到底部。起初,我有:

for i in range(-height, height):
     mTurtle.setposition(0, i)
     etc.
但我意识到这使得海龟开始比窗户的实际尺寸低很多,我需要把这个尺寸减半。但是当我有了这个密码,海龟至少从底部移动到了顶部。 我试着输入
,因为我在范围内(-height/2,height/2)
,这时我的乌龟就不再出现了

所以,我试着把这些值保存在变量的底部和顶部,我想也许因为某种原因我不能把它们放在范围参数中? 出于某种原因,这不起作用,我不知道为什么


以前,当我的乌龟从下到上移动时,最后4行正在缩小它,但它会变得非常小,当它到达屏幕中央时就会消失。我想这是因为我没有把高度除以二。

关于海龟运动,我相信@JasonYang的评论是正确的(+1),尽管缺乏解释。海龟在浮点平面上游荡,但是
range()
需要
int
值。我们使用整数除法
\\
将海龟的浮点值转换为
range()
想要的值:

import sys
from turtle import Screen, Turtle

def movingTurtle(mTurtle, window):
    height = window.window_height()

    top, bottom = height // 2, -height // 2  # use // for range() below, turtle doesn't care

    mTurtle.shape('turtle')
    mTurtle.setheading(90)  # turtle faces direction of motion
    mTurtle.penup()
    mTurtle.sety(bottom)

    for y in range(bottom + 1, top):
        mTurtle.sety(y)

def main():
    width = int(input("Enter the width of the screen: "))
    height = int(input("Enter the height of the screen: "))

    screen = Screen()
    screen.setup(width, height)
    screen.title("Lab20 - Turtle Object")

    try:
        movingTurtle(Turtle(), screen)
    except:
        print("movingTurtle is not either defined or there is a problem with the function", file=sys.stderr)

    screen.exitonclick()

main()

顶部,底部=高度//2,-高度//2