【问题标题】:Using Turtle module in Python to move shrinking turtle up window screen在 Python 中使用 Turtle 模块将缩小的海龟向上移动窗口屏幕
【发布时间】:2021-02-23 18:01:01
【问题描述】:

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

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.

但我意识到这使得海龟开始的位置比窗口的实际大小要低得多,我需要把那个大小减半。但是当我有那个代码时,乌龟至少从底部移动到顶部。 我尝试输入for i in range(-height/2, height/2),然后我的乌龟就完全停止出现了。

所以,然后我尝试将这些值保存在变量底部和顶部中,认为可能由于某种原因我不能将它们放入范围参数中? 由于某种原因,这不起作用,我不确定为什么。

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

【问题讨论】:

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

标签: python loops turtle-graphics python-turtle


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多