Logic error in python turtle

前端 未结 1 1130
有刺的猬
有刺的猬 2021-01-15 00:23

I am coding in python 3.2 turtle and I have this beautiful drawing of a tank. and I know how to move it left and write. However, when trying to make the tank move up and dow

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 00:46

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

    0 讨论(0)
提交回复
热议问题