【发布时间】: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