如何解决如何使乌龟对象在python中位于另一个乌龟对象后面?
我希望能够轻松地将一个正方形放在一组行后面,而不会使程序滞后或导致任何重大错误。我想使用turtle模块完成所有这些,因为我目前正在使用该模块制作游戏。我已经尝试了几次,但它从来没有像我想要的那样工作。我也在使用 Python 3.8。
这是我对这个问题的尝试:
import turtle
#The Setup
wn = turtle.Screen()
global WIDTH
global HEIGHT
WIDTH = 800
HEIGHT = 650
wn.setup(width = WIDTH,height = HEIGHT)
turtle.speed(0)
wn.title("Game by iAmInfernal")
wn.bgcolor('white')
wn.tracer(0)
#I create a turtle object
t = turtle. Turtle()
#I create a player object
player = turtle.Turtle()
t.stamp()
down = 0
player.shape('square')
player.color('red')
player.penup()
player.speed(0)
player.stamp()
#I make a function that will make 150 lines with a space of 10 units
def makeLines():
global down
for item in range(150):
t.penup()
t.goto(-400,300 - down)
t.stamp()
t.pendown()
t.hideturtle()
t.pensize(3)
t.forward(800)
down += 10
while True:
makeLines()
wn.update()
如果您执行这段代码,您将能够看到许多行的中心带有红色方块。然而,正如我多次尝试的那样,红色方块从未落后于那些线组。我究竟如何让红色方块位于这些线条组的后面?
解决方法
海龟不能出现背后他们绘制的东西,无论是通过绘图还是冲压。但是,海龟可以出现在其他海龟的后面,因此一种解决方法可能是使线条本身成为海龟:
from turtle import Screen,Turtle
WIDTH,HEIGHT = 800,650
SPACING = 10
CURSOR_SIZE = 20
def makeLines():
down = 0
for _ in range(HEIGHT // SPACING):
turtle = prototype.clone()
turtle.sety(HEIGHT/2 - down)
turtle.showturtle()
down += SPACING
screen = Screen()
screen.setup(width=WIDTH,height=HEIGHT)
screen.tracer(0)
player = Turtle()
player.shape('square')
player.color('red')
player.penup()
player.stamp()
prototype = Turtle()
prototype.hideturtle()
prototype.shape('square')
prototype.shapesize(2 / CURSOR_SIZE,WIDTH / CURSOR_SIZE,1)
prototype.penup()
makeLines()
screen.update()
screen.mainloop()
然后问题就变成了控制海龟的分层,基本的经验法则是“最后移动的东西在上面”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。