Logic error in python turtle

会有一股神秘感。 提交于 2019-12-02 06:30:26

When drawing a custom cursor, unless you have a reason not to do so, keep your cursor design centered about the origin, (0, 0). If you draw to one side of the origin, then when your turtle turns right or left, it won't do so symmetrically. That is, as it turns in one direction, it will appear to do so quickly but in the opposite direction, it will appear to "take the long way around".

I've worked the polygons underlying the tank cursor so that it's center is the center of the turret and removed the setheading() calls from the drawing routines.

This example uses what I think is consistent motion: regardless of tank orientation, 'up' is forward, 'down' is backward, 'left' is 90 degrees to the left and 'right' is 90 degrees to the right. I've also stripped down the example just to demonstrate the motion:

import turtle

# Defining shapes
def polySquare(t, x, y, length):
    t.goto(x, y)
    t.begin_poly()
    for count in range(4):
        t.forward(length)
        t.left(90)
    t.end_poly()
    return t.get_poly()

def polyRectangle(t, x, y, length1, length2):
    t.goto(x, y)
    t.begin_poly()
    for count in range(2):
        t.forward(length1)
        t.left(90)
        t.forward(length2)
        t.left(90)
    t.end_poly()
    return t.get_poly()

def tankCursor():
    """
    Create the tank cursor.
    """
    temporary = turtle.Turtle()
    temporary.hideturtle()
    temporary.penup()

    screen = turtle.getscreen()
    delay = screen.delay()
    screen.delay(0)

    tank = turtle.Shape("compound")
    tire1 = polyRectangle(temporary, -65, -75, 30, 75)  # Tire #1
    tank.addcomponent(tire1, "gray", "black")
    tire2 = polyRectangle(temporary, 35, -75, 30, 75)  # Tire #2
    tank.addcomponent(tire2, "gray", "black")
    tire3 = polyRectangle(temporary, 35, 0, 30, 75)  # Tire #3
    tank.addcomponent(tire3, "gray", "black")
    tire4 = polyRectangle(temporary, -65, 0, 30, 75)  # Tire #4
    tank.addcomponent(tire4, "gray", "black")
    bodyTank = polyRectangle(temporary, -55, -65, 110, 130)
    tank.addcomponent(bodyTank, "black", "gray")
    gunTank = polyRectangle(temporary, -10, 25, 20, 100)   # Gun
    tank.addcomponent(gunTank, "black", "gray")
    exhaustTank = polyRectangle(temporary, -25, -75, 10, 20)
    tank.addcomponent(exhaustTank, "black", "gray")
    turretTank = polySquare(temporary, -25, -25, 50)  # Turret
    tank.addcomponent(turretTank, "red", "gray")
    turtle.addshape("tank", shape=tank)

    del temporary
    screen.delay(delay)

tankCursor()  # creates and registers the "tank" cursor shape
turtle.shape("tank")
turtle.up()  # get rid of the ink

# Movement of tank
screen = turtle.Screen()
screen.onkeypress(lambda : turtle.right(90), "Right")
screen.onkeypress(lambda : turtle.left(90), "Left")
screen.onkeypress(lambda : turtle.forward(40), "Up")
screen.onkeypress(lambda : turtle.backward(40), "Down")

turtle.listen()

turtle.mainloop()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!