<p>我会避免这样的代码:</p>
<pre><code>dodx = dot.xcor()
dody = dot.ycor()
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:
hit = True
dot.clear()
elif abs(t.xcor() - tx) < 5 and abs(t.ycor() - ty) < 5:
end()
</code></pre>
<p>而是使用turtle的<code>distance()</code>方法:</p>
<pre><code>if t.distance(dot) < 5:
hit = True
dot.clear()
elif t.distance(ball1) < 5:
end()
</code></pre>
<blockquote>
<p>However, this only works sometimes. Sometimes the dot will randomly
disappear and spawn somewhere else even though the turtle isn't even
close to it.</p>
</blockquote>
<p>你在假装这个代码运行吗?由于两个不同的<code>import</code>问题,它甚至无法启动。修好这些,海龟控制根本不起作用。而且球不会在箱子周围弹跳!你知道吗</p>
<p>这似乎是一个随机收集的位代码借用其他程序补丁在一起一厢情愿。它根本不起作用。你知道吗</p>
<p>下面我把你的代码拆开,重新组合起来,使它基本上可以运行。蓝色的球会在盒子周围弹跳;你可以用箭头键移动海龟;当海龟碰到红点时,红点会消失并重新出现在其他地方:</p>
<pre><code>from turtle import Screen, Turtle
from random import randint, choice
CURSOR_SIZE = 20
def tDirection(direct):
ball.right(direct)
def turtleMove():
turtle.forward(speed)
screen.ontimer(turtleMove, 10)
def createDot():
x, y = randint(CURSOR_SIZE - 200, 200 - CURSOR_SIZE), randint(CURSOR_SIZE - 200, 200 - CURSOR_SIZE)
dot.goto(x, y)
def end():
marker.write("GAME OVER", align='center', font=("Arial", 40, "normal"))
turtle.hideturtle()
ball.hideturtle()
dot.hideturtle()
screen = Screen()
screen.bgcolor("light blue")
score = 0
speed = 2
# CONTROLLABLE TURTLE
turtle = Turtle("turtle")
turtle.color('black')
turtle.pensize(10)
turtle.speed("fastest")
turtle.penup()
turtle.goto(-200, -200)
turtle.pendown()
# MAKE SQUARE
for i in range(4):
turtle.forward(400)
turtle.left(90)
turtle.penup()
turtle.goto(50, 0)
# BALL
ball = Turtle("circle")
ball.color("blue")
ball.speed('fastest')
ball.penup()
A = randint(30, 60)
B = randint(120, 150)
C = randint(210, 240)
D = randint(300, 330)
directions = [A, B, C, D]
direct = choice(directions)
tDirection(direct)
angle = 90
# DOT TURTLE
dot = Turtle('circle')
dot.color("red")
dot.speed('fastest')
dot.pensize(3)
dot.penup()
createDot()
marker = Turtle(visible=False)
marker.penup()
marker.sety(-20)
screen.onkey(lambda: turtle.setheading(0), 'Right')
screen.onkey(lambda: turtle.setheading(90), 'Up')
screen.onkey(lambda: turtle.setheading(180), 'Left')
screen.onkey(lambda: turtle.setheading(270), 'Down')
screen.listen()
turtleMove()
while True:
# moving ball
tx, ty = ball.position()
if ty < CURSOR_SIZE - 200:
angleCurr = ball.heading()
if 270 > angleCurr > 180:
ball.right(angle)
else:
ball.left(angle)
ball.forward(2)
elif ty > 200 - CURSOR_SIZE:
angleCurr = ball.heading()
if 0 < angleCurr < 90:
ball.right(angle)
else:
ball.left(angle)
ball.forward(2)
elif tx < CURSOR_SIZE - 200:
angleCurr = ball.heading()
if 180 < angleCurr < 270:
ball.left(angle)
else:
ball.right(angle)
ball.forward(2)
elif tx > 200 - CURSOR_SIZE:
angleCurr = ball.heading()
if 0 < angleCurr < 90:
ball.left(angle)
else:
ball.right(angle)
ball.forward(2)
ball.forward(9)
if turtle.distance(dot) < CURSOR_SIZE/2:
score += 1
dot.hideturtle()
createDot()
dot.showturtle()
elif turtle.distance(ball) < CURSOR_SIZE/2:
end()
if not CURSOR_SIZE - 200 < turtle.xcor() < 200 - CURSOR_SIZE or not CURSOR_SIZE - 200 < turtle.ycor() < 200 - CURSOR_SIZE:
end()
screen.mainloop()
</code></pre>
<p>当你请求帮助时,请诚实的说出你的代码状态。你知道吗</p>
<p>上面的代码还不完整,还有工作要做。例如,<code>while True:</code>循环应转换为函数和<code>ontimer()</code>事件;分数需要显示;游戏应可重新启动。你知道吗</p>