【发布时间】:2017-11-29 01:26:45
【问题描述】:
如何设置屏幕边界,以便当海龟到达边缘时它会停止或转身
import turtle
t=turtle.Turtle()
s=turtle.Screen()
p=t.xcor()
p1=t.ycor()
x=300
y=300
s.setup(x,y)
t.color("white")
s.bgcolor("black")
def up():
player=False
while player==False:
t.speed(1)
t.fd(10)
def right():
t.speed(0)
t.right(90)
def left():
t.speed(0)
t.left(90)
s.onkey(up,"up")
s.onkey(right,"right")
s.onkey(left,"left")
s.listen()
我认为这会将它停在屏幕边缘
下面的代码还没有完全完成,但我不知道要改成什么
while p and p1 != x and y:
t.right(90)
if p and p1 == x and y:
t.speed(0)
t.right(180)
【问题讨论】:
-
在其他图形系统/模块中,当您必须移动 100 像素时,您会这样做:移动几个像素(即 2 个像素),检查碰撞(即与屏幕边框),再次移动几个像素,检查再次发生碰撞,等等 - 所以你循环运行它。
标签: python-3.x turtle-graphics