4

我正在用 python 3.2.2 龟编程。当我发出咔哒声时,我可以让乌龟跟随我的光标,但现在我无法改变乌龟的外观。在我的代码中,你会看到一个坦克,我希望坦克的图像是我的乌龟。

这是我的代码:

#importing modules
from turtle import Turtle
from turtle import *

#Setting up variables
unVar1 = 25
unVar2 = 100
unVar3 = 90
unVar4 = 150
unVar5 = -30
unVar6 = 75
unVar7 = 50
screen = Screen() # create the screen

#first part in making the turtle move
turtle = Turtle()
t = Turtle() # create the first turtle
t2 = Turtle() # create the second turtle
screen.onscreenclick(turtle.goto) # set up the callback for moving the first turtle

#defining shapes and objects
def drawSquare(t , xPrime, yPrime, sideLength):
    t.up()
    t.hideturtle()
    t.goto(xPrime, yPrime)
    t.setheading(270)
    t.down()
    for count in range(4):
        t.forward(sideLength)
        t.left(90)
    t.end_fill()
def drawRectangle(t, x2, y2, sideLength1, sideLength2):
    t.up()
    t.hideturtle()
    t.goto(x2, y2)
    t.setheading(270)
    t.down()
    for count in range(2):
        t.forward(sideLength1)
        t.left(90)
        t.forward(sideLength2)
        t.left(90)
    t.end_fill()
def drawTank():
    t.pencolor("black")
    t.fillcolor("gray")
    t.begin_fill()
    tire1 = drawRectangle(t, int("10"), unVar1, unVar6, int("30")) #Tire
    t.begin_fill()
    tire2 = drawRectangle(t, int("110"), unVar1, unVar6, int("30"))    #Tire
    t.begin_fill()
    tire3 = drawRectangle(t, int("110"), unVar2, unVar6, int("30"))  #Tire
    t.begin_fill()
    tire4 = drawRectangle(t, int("10"), unVar2, unVar6, int("30"))   #Tire
    t.pencolor("gray")
    t.fillcolor("black")
    t.begin_fill()
    bodyTank = drawRectangle(t, int("20"), unVar3, int("130"), int("110")) 
    t.begin_fill()
    gunTank = drawRectangle(t, int("65"), unVar4, int("100"), int("20"))   #Gun
    t.begin_fill()
    exhaustTank = drawRectangle(t, int("50"), unVar5, int("20"), int("10"))   
    t.fillcolor("red")
    t.begin_fill()
    turretTank = drawSquare(t, int("50"), unVar7, int("50"))  #Turret
    t.end_fill()
drawTank()
screen.mainloop() # start everything running
4

2 回答 2

2

您可以使用您拥有的代码创建一个坦克光标,您只需要重新排列它。绘图函数需要创建多边形而不是直接绘制到屏幕上。以下将创建您的坦克光标并在屏幕上稍微移动一下。请参阅tankCursor()有关如何也可以使用固定多边形的评论——您不必制作位图图像来执行此操作:

import turtle

unVar1 = 25
unVar2 = 100
unVar3 = 90
unVar4 = 150
unVar5 = -30
unVar6 = 75
unVar7 = 50

def polySquare(t, x, y, length):
    t.goto(x, y)
    t.setheading(270)

    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.setheading(270)

    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.  An alternate solution is to toss the temporary turtle
    and use the commented out polygon assignments instead of the poly* function calls
    """

    temporary = turtle.Turtle()

    screen = turtle.getscreen()

    delay = screen.delay()

    screen.delay(0)

    temporary.hideturtle()
    temporary.penup()

    tank = turtle.Shape("compound")

    # tire1 = ((10, unVar1), (10, unVar1 - unVar6), (10 + 30, unVar1 - unVar6), (10 + 30, unVar1))
    tire1 = polyRectangle(temporary, 10, unVar1, unVar6, 30)  # Tire #1
    tank.addcomponent(tire1, "gray", "black")

    # tire2 = ((110, unVar1), (110, unVar1 - unVar6), (110 + 30, unVar1 - unVar6), (110 + 30, unVar1))
    tire2 = polyRectangle(temporary, 110, unVar1, unVar6, 30)  # Tire #2
    tank.addcomponent(tire2, "gray", "black")

    # tire3 = ((110, unVar2), (110, unVar2 - unVar6), (110 + 30, unVar2 - unVar6), (110 + 30, unVar2))
    tire3 = polyRectangle(temporary, 110, unVar2, unVar6, 30)  # Tire #3
    tank.addcomponent(tire3, "gray", "black")

    # tire4 = ((10, unVar2), (10, unVar2 - unVar6), (10 + 30, unVar2 - unVar6), (10 + 30, unVar2))
    tire4 = polyRectangle(temporary, 10, unVar2, unVar6, 30)  # Tire #4
    tank.addcomponent(tire4, "gray", "black")

    # bodyTank = ((20, unVar3), (20, unVar3 - 130), (20 + 110, unVar3 - 130), (20 + 110, unVar3))
    bodyTank = polyRectangle(temporary, 20, unVar3, 130, 110)
    tank.addcomponent(bodyTank, "black", "gray")

    # gunTank = ((65, unVar4), (65, unVar4 - 100), (65 + 20, unVar4 - 100), (65 + 20, unVar4))
    gunTank = polyRectangle(temporary, 65, unVar4, 100, 20)   # Gun
    tank.addcomponent(gunTank, "black", "gray")

    # exhaustTank = ((50, unVar5), (50, unVar5 - 20), (50 + 10, unVar5 - 20), (50 + 10, unVar5))
    exhaustTank = polyRectangle(temporary, 50, unVar5, 20, 10)
    tank.addcomponent(exhaustTank, "black", "gray")

    # turretTank = ((50, unVar7), (50, unVar7 - 50), (50 + 50, unVar7 - 50), (50 + 50, unVar7))
    turretTank = polySquare(temporary, 50, unVar7, 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

# show our tank in motion

turtle.setheading(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(45)
turtle.forward(100)

turtle.done()
于 2016-03-07T06:33:42.203 回答
1

由于坦克是您使用海龟创建的形状,您可以随时简单地截取您的坦克的屏幕截图,然后使用海龟模块register_shapeshape函数将屏幕截图设置为海龟的形状,如下所示:

turtle.register_shape("INSERT PATH OF SCREENSHOT HERE")
t.shape("INSERT PATH OF SCREENSHOT HERE")

但是,如果你想做一些更难的事情,你可以使用turtle.register_shape()上面显示的方法,但不是提供某些图像的路径,而是必须提供一个名称,然后提供一些坐标来创建形状。简而言之,它的语法是:

turtle.register_shape(name, (coordinates))
t.shape(name)

但是,鉴于您的水箱已经如此复杂,这将是非常乏味的,因此我建议您继续使用上述方法,您只需截取水箱的屏幕截图并将该屏幕截图用作海龟的形状。

另外,我在您的代码中看到了一些问题:

  1. 以下:

    from turtle import Turtle
    from turtle import *
    

    您正在两次导入海龟模块。没有必要这样做!from turtle import *已经导入了Turtle()函数,所以不需要第一个导入命令。因此,将导入命令更改为:

    from turtle import *
    # The rest of your code...
    
  2. 当用户点击屏幕去某个地方时,你的海龟无论走到哪里都会创建一条线。有一种方法可以避免这种情况。但是,我不知道你是否想要这个,但如果你这样做并决定保留它,它确实会导致很多行到他们不应该去的地方,并且最终可能会开始占用大量内存,直到你的程序崩溃了。所以我确实建议采取这一步,但你是否这样做取决于你。但是,如果您确实决定采取此步骤,那么首先,在 之后定义以下函数t2 = Turtle()

    def Go(x, y):
        # Turn the animation completely off
        screen.tracer(0, 0)
        # Pick the pen up so no lines are created as the turtle moves
        turtle.penup()
        # Go to the clicked position on the canvas
        turtle.goto(x, y)
        # Put the pen back down
        turtle.pendown()
        # Turn animation back on
        screen.tracer(1, 1)
    

    然后改变:

    screen.onscreenclick(turtle.goto)
    

    到:

    screen.onscreenclick(Go)
    

就是这样!我希望这有帮助!:)

于 2016-03-07T04:49:38.233 回答