绘制自定义光标时,除非有理由不这样做,否则请使光标设计以原点(0,0)为中心。如果您绘制到原点的一侧,那么当您的海龟向右或向左转时,它不会对称地这样做。就是说,当它朝一个方向转动时,它看起来会很快,但是朝相反的方向,它将看起来“走很远”。
我已经处理了坦克光标下方的多边形,以使其中心为炮塔的中心,并从绘制例程中删除了setheading()调用。
这个示例使用了我认为是一致的运动:无论油箱方向如何,“上”是向前,“下”是后向,“左”是左90度,“右”是右90度。我还精简了示例,以演示运动:
import turtle# Defining shapesdef 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 shapeturtle.shape("tank")turtle.up() # get rid of the ink# Movement of tankscreen = 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条)