python 3 在列表中找到
python 3 find in list
我正在创建游戏,但无法在此列表中检测海龟位置
我正在使用 python3 和乌龟。
此代码的 objective 是在乌龟与其自己的路径相交时创建填充形状
import turtle
t=turtle.Turtle()
t.fillcolor("red")
t.begin_fill()
s=turtle.Screen()
t.penup()
status=True
penstatus=False
t.speed(1)
x=[]
def go1():
t.left(-(t.heading()))
t.left(90)
def go2():
t.left(-(t.heading()))
# t.left(90)
def go3():
t.left(-(t.heading()))
t.left(270)
def go4():
t.left(-(t.heading()))
t.left(180)
def paint():
global penstatus
if penstatus==False:
penstatus=True
t.down()
else:
t.up()
def detect():
global status
a=t.position()
if a in x:
status=False
print("yes")
while status:
global x
s.onkeypress(go1, "Up")
s.onkeypress(go2, "Right")
s.onkeypress(go3, "Down")
s.onkeypress(go4, "Left")
s.onkeypress(paint,"space")
s.listen()
x.append(t.position())
t.fd(5)
detect()
t.end_fill()
s.mainloop()
有时可以,但填充结果也会出错
您无法检测当前职位是否在过去职位列表中的原因有两个。第一个是你一次 "hop" 五个像素,所以你可能在 "filled in" 线段越线,而不是你实际定位的线段。
第二个是乌龟的位置是浮点数,当你回到同一个地方时可能会略有不同。我们可以通过不直接比较而是询问点之间的距离是否小于我们的 "hop" 距离来解决这两个问题。
我对以下代码的修改实现了这种方法。它还会稍微改变按键的工作方式;将逻辑更改为仅在填充图形中包含可见线;并且完全基于事件。它还有一个重置 "r" 键来开始新的绘图。您可以取消任何您不喜欢的更改,后退位置检测仍然适用:
from turtle import Turtle, Screen
DISTANCE = 3
def go_up():
turtle.setheading(90)
def go_right():
turtle.setheading(0)
def go_down():
turtle.setheading(270)
def go_left():
turtle.setheading(180)
def toggle_pen():
if turtle.isdown():
turtle.penup()
else:
turtle.pendown()
turtle.begin_fill() # ignore pending begin_fill, start anew
def reset_drawing():
global positions
turtle.reset()
turtle.fillcolor('red')
turtle.speed('fastest')
turtle.penup()
positions = []
move()
def move():
for position in positions:
if turtle.distance(position) < DISTANCE:
turtle.end_fill()
return
if turtle.isdown():
positions.append(turtle.position())
turtle.forward(DISTANCE)
screen.ontimer(move, 100)
screen = Screen()
screen.onkeypress(go_up, 'Up')
screen.onkeypress(go_right, 'Right')
screen.onkeypress(go_down, 'Down')
screen.onkeypress(go_left, 'Left')
screen.onkeypress(toggle_pen, 'space')
screen.onkeypress(reset_drawing, 'r')
screen.listen()
turtle = Turtle()
positions = None # make sure global is defined
reset_drawing()
screen.mainloop()
我正在创建游戏,但无法在此列表中检测海龟位置 我正在使用 python3 和乌龟。 此代码的 objective 是在乌龟与其自己的路径相交时创建填充形状
import turtle
t=turtle.Turtle()
t.fillcolor("red")
t.begin_fill()
s=turtle.Screen()
t.penup()
status=True
penstatus=False
t.speed(1)
x=[]
def go1():
t.left(-(t.heading()))
t.left(90)
def go2():
t.left(-(t.heading()))
# t.left(90)
def go3():
t.left(-(t.heading()))
t.left(270)
def go4():
t.left(-(t.heading()))
t.left(180)
def paint():
global penstatus
if penstatus==False:
penstatus=True
t.down()
else:
t.up()
def detect():
global status
a=t.position()
if a in x:
status=False
print("yes")
while status:
global x
s.onkeypress(go1, "Up")
s.onkeypress(go2, "Right")
s.onkeypress(go3, "Down")
s.onkeypress(go4, "Left")
s.onkeypress(paint,"space")
s.listen()
x.append(t.position())
t.fd(5)
detect()
t.end_fill()
s.mainloop()
有时可以,但填充结果也会出错
您无法检测当前职位是否在过去职位列表中的原因有两个。第一个是你一次 "hop" 五个像素,所以你可能在 "filled in" 线段越线,而不是你实际定位的线段。
第二个是乌龟的位置是浮点数,当你回到同一个地方时可能会略有不同。我们可以通过不直接比较而是询问点之间的距离是否小于我们的 "hop" 距离来解决这两个问题。
我对以下代码的修改实现了这种方法。它还会稍微改变按键的工作方式;将逻辑更改为仅在填充图形中包含可见线;并且完全基于事件。它还有一个重置 "r" 键来开始新的绘图。您可以取消任何您不喜欢的更改,后退位置检测仍然适用:
from turtle import Turtle, Screen
DISTANCE = 3
def go_up():
turtle.setheading(90)
def go_right():
turtle.setheading(0)
def go_down():
turtle.setheading(270)
def go_left():
turtle.setheading(180)
def toggle_pen():
if turtle.isdown():
turtle.penup()
else:
turtle.pendown()
turtle.begin_fill() # ignore pending begin_fill, start anew
def reset_drawing():
global positions
turtle.reset()
turtle.fillcolor('red')
turtle.speed('fastest')
turtle.penup()
positions = []
move()
def move():
for position in positions:
if turtle.distance(position) < DISTANCE:
turtle.end_fill()
return
if turtle.isdown():
positions.append(turtle.position())
turtle.forward(DISTANCE)
screen.ontimer(move, 100)
screen = Screen()
screen.onkeypress(go_up, 'Up')
screen.onkeypress(go_right, 'Right')
screen.onkeypress(go_down, 'Down')
screen.onkeypress(go_left, 'Left')
screen.onkeypress(toggle_pen, 'space')
screen.onkeypress(reset_drawing, 'r')
screen.listen()
turtle = Turtle()
positions = None # make sure global is defined
reset_drawing()
screen.mainloop()