简体   繁体   中英

Using Turtle module in Python to move shrinking turtle up window screen

I am supposed to define a function, movingTurtle , that uses the Python turtle module, sets the turtle to an actual turtle shape, and moves that turtle up from the bottom of the screen towards the top, getting smaller as it moves along. Here is the code I currently have:

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()

(The reason for the main() part is because I actually have several other functions - this is for a project)

Even with the bottom four lines commented out, I can't even get the turtle to move from the top to the bottom. At first, I had:

for i in range(-height, height):
     mTurtle.setposition(0, i)
     etc.

But I realized that that made it so that the turtle started way further down than the actual size of the window, I needed to cut that size in half. But when I had that code, the turtle did move from bottom to top at least. I tried to put in for i in range(-height/2, height/2) and that's when my turtle stopped appearing at all.

So, then I tried to hold those values in the variables bottom and top, thinking that maybe for some reason I couldn't put them in the range parameters? For some reason this isn't working and I'm not sure why.

Before, when my turtle was moving from bottom to top, the last 4 lines were shrinking it, but it would get so small it would disappear by the time it reached the middle of the screen. I think this was because I didn't have the height divided by two.

With respect to turtle motion, I believe that @JasonYang's comment is spot on (+1) though lacking in explanation. Turtles wander a floating point plane, but the range() wants int values. We use integer division \\\\ to convert turtle's floating point values to what range() wants:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM