我的代码有一个乌龟在随机位置绘制一个点。绘制后,另一只乌龟前进到相同的坐标。应该发生的是,当第二只乌龟到达该点时,该点应该消失并立即在其他位置重绘,但是由于某种原因if
语句不起作用:
import turtle, random
t = turtle.Turtle()
t.speed(1)
dot = turtle.Turtle()
dot.hideturtle()
dot.speed(0)
dx = random.randint(1,100)
dy = random.randint(1,100)
tx = t.xcor()
ty = t.ycor()
def createDot(dx, dy):
dot.penup()
dot.goto(dx, dy)
dot.pendown()
dot.circle(5)
createDot(dx, dy)
t.goto(dx,dy)
if tx == dx and ty == dy:
dot.clear()
createDot(dx, dy)
答案 0 :(得分:1)
用...移动乌龟
searchMe(searchTerm: string, eachObject) {
let replacedKey = searchTerm.replace(/[,\.-\s]/g, '')
let newRegEx = new RegExp(replacedKey, 'gi');
let purgedName = eachObject.name.replace(/[,\.-\s]/g, '')
if (newRegEx.test(purgedName)) {
return true
}
return false
}
<ng-select [items]="cities"
bindLabel="name"
[searchFn]="searchMe"
placeholder="Select city"
[(ngModel)]="selectedCity">
</ng-select>
不会更改t.goto(dx,dy)
和tx
的值。尝试重做
ty
在if语句之前。
答案 1 :(得分:0)
这是一个脆弱的策略,始于:
if tx == dx and ty == dy:
当海龟在浮点平面上徘徊而很少降落在完全相同的位置时。让我们重新编写这段代码,以实际利用Turtle的方法并完全消除tx, ty
和dx, dy
:
from turtle import Screen, Turtle
from random import randint
def moveDot():
dot.goto(randint(-100, 100), randint(-100, 100))
def chaseDot():
if turtle.distance(dot) < 1:
moveDot()
turtle.setheading(turtle.towards(dot))
turtle.forward(2)
screen.ontimer(chaseDot, 50)
screen = Screen()
turtle = Turtle()
turtle.speed('slowest')
dot = Turtle('circle')
dot.shapesize(0.5)
dot.speed('fastest')
dot.penup()
chaseDot()
screen.exitonclick()
这使海龟不断追逐点-当海龟到达时,点会重新定位。