如何在Python中单独更新turtles
我目前正在尝试用python分别更新多个海龟。在下面的例子中,我试图让底部的乌龟根据玩家的输入移动并不断更新,而顶部的乌龟则前后移动并根据一定的间隔进行更新如何在Python中单独更新turtles,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我目前正在尝试用python分别更新多个海龟。在下面的例子中,我试图让底部的乌龟根据玩家的输入移动并不断更新,而顶部的乌龟则前后移动并根据一定的间隔进行更新 import turtle from time import sleep from turtle import Screen, Turtle screen = turtle.Screen() screen.title("Turtle Test") screen.bgcolor("grey") screen.setup(width=630,
import turtle
from time import sleep
from turtle import Screen, Turtle
screen = turtle.Screen()
screen.title("Turtle Test")
screen.bgcolor("grey")
screen.setup(width=630, height=630)
screen.tracer(0)
turtle_a = turtle.Turtle()
turtle_a.speed(0)
turtle_a.shape("square")
turtle_a.color("white")
turtle_a.penup()
turtle_b = turtle.Turtle()
turtle_b.speed(0)
turtle_b.shape("square")
turtle_b.color("black")
turtle_b.penup()
turtle_b_speed = 10
def go_left():
x = turtle_b.xcor()
x -= turtle_b_speed
turtle_b.setx(x)
def go_right():
x = turtle_b.xcor()
x += turtle_b_speed
turtle_b.setx(x)
screen.listen()
screen.onkeypress(go_left, "Left")
screen.onkeypress(go_right, "Right")
direct = 5
while True:
turtle_a.goto(turtle_a.xcor() + direct, turtle_a.ycor())
sleep(0.5)
if turtle_a.xcor() >= 310:
direct = -5
if turtle_a.xcor() <= -310:
direct = 5
# update function
导入海龟
从时间上导入睡眠
从海龟导入屏幕,海龟
screen=turtle.screen()
屏幕标题(“海龟测试”)
屏幕颜色(“灰色”)
屏幕设置(宽度=630,高度=630)
屏幕跟踪器(0)
海龟
乌龟速度(0)
海龟形状(“方形”)
乌龟颜色(“白色”)
乌龟
海龟
乌龟速度(0)
龟形(“方形”)
乌龟颜色(“黑色”)
乌龟(b.penup)
乌龟速度=10
def go_left():
x=海龟_b.xcor()
x-=乌龟速度
海龟_b.setx(x)
def go_right():
x=海龟_b.xcor()
x+=乌龟速度
海龟_b.setx(x)
screen.listen()
屏幕上的按键(向左,“左”)
屏幕上的按键(向右,“向右”)
直接=5
尽管如此:
turtle_a.goto(turtle_a.xcor()+直接,turtle_a.ycor())
睡眠(0.5)
如果turtle_a.xcor()>=310:
直接=-5
如果turtle_a.xcor()您知道在两次导入turtle时已经走错了路,那么有两种不同的方法,以及在基于事件的环境(如turtle)中使用sleep()
:
import turtle
from time import sleep
from turtle import Screen, Turtle
您也不应该在基于事件的环境中使用while True:
循环,而应该使用计时器事件。下面是修复这些问题的代码重写。由于您从未为两只海龟设置Y坐标,因此没有“顶部”或“底部”,只有两只海龟相互移动:
from turtle import Screen, Turtle
def go_left():
x = turtle_b.xcor() - turtle_b_speed
if -300 <= x <= 300:
turtle_b.setx(x)
def go_right():
x = turtle_b.xcor() + turtle_b_speed
if -300 <= x <= 300:
turtle_b.setx(x)
def move_a():
global turtle_a_direct
turtle_a.setx(turtle_a.xcor() + turtle_a_direct)
if not -300 <= turtle_a.xcor() <= 300:
turtle_a.undo()
turtle_a_direct *= -1
screen.ontimer(move_a, 500)
screen = Screen()
screen.title("Turtle Test")
screen.bgcolor('grey')
screen.setup(width=640, height=640)
turtle_a = Turtle('square')
turtle_a.speed('fastest')
turtle_a.color('white')
turtle_a.penup()
turtle_a_direct = 5
turtle_b = Turtle('square')
turtle_b.speed(0)
turtle_b.color('black')
turtle_b.penup()
turtle_b_speed = 10
screen.onkeypress(go_left, 'Left')
screen.onkeypress(go_right, 'Right')
screen.listen()
move_a()
screen.mainloop()
从海龟导入屏幕,海龟
def go_left():
x=海龟b.xcor()-海龟b_速度
如果是-300那么你的代码出了什么问题?你现在在做什么?我试图让底部的乌龟不断移动,因为当我插入screen.update()其中的#update函数时,每次按下right或left时,底部的乌龟只会在顶部乌龟移动时移动。这一行:如果乌龟_a.xcor()==310:direct=-5
我假设您正在检查是否已到达屏幕的一个极端并掉头。不要使用==
而是使用=
这样,如果你的乌龟没有准确地降落在310点,它仍然会转身。与类似,谢谢,我修复了这些行,但是你知道如何修复更新循环吗?不幸的是,不,我对海龟不是非常熟悉。也许打印出turtle\u a.xcor()
在你的中,而True
循环,以确保它按照你期望的方式运行。我知道这已经晚了,但是你有一个解决方案可以为大约50只turtle工作吗?