python - 我想在turtlegraphics的墙上有一个弹跳球,但他们停下来,他们徘徊并发送错误
问题描述
我画了一些三角形,它们应该在墙上弹跳,但它们不弹跳,它们不直走,几秒钟后它们会打印一条错误消息。
当我让三角形仅以十字形移动时,三角形动摇并出现错误消息。
import turtle
import random
def draw_sq(t,distance=400):#Draw a box
t.penup()
t.goto(-200,200)
t.pendown()
t.forward(distance)
t.right(90)
t.forward(distance)
t.right(90)
t.forward(distance)
t.right(90)
t.forward(distance)
t.penup()
t.goto(0,0)
t.pendown()
def add_tri():
global l_tri
t=turtle.Turtle()
t.shape("triangle")
x=-200
y=random.randint(-200,200)
t.penup()
t.goto(x,y)
t.setheading(random.randint(-89,89))
# make triangle and start
l_tri.append(t)# add a triangle to list
def successMan():#write word succes
global playing
if playing==True:
t.write("success")
def turn_right():
global right
right = True
def turn_left():
global left
left = True
def turn_up():
global up
up = True
def turn_down():
global down
down = True
def _turn_right():
global right
right = False
def _turn_left():
global left
left = False
def _turn_up():
global up
up = False
def _turn_down():
global down
down = False
def is_touch(x,y,triangle,threshold=20):
global playing
if((x-triangle.position()[0])**2+(y-triangle.position()[1])**2<=threshold**2):
playing=False#if triangle touches the turtle make playing False
def timer_go():
global heading
global l_tri
global t
global playing
global up
global left
global down
global right# heading is where turtle heads
if up:
if right:
heading=45
elif left:
heading=135
elif down:
heading = heading
else:
heading=90
if down:
if right:
heading=315
elif left:
heading=225
elif up:
pass
else:
heading = 270
if left:
if right:
heading = heading
elif up:
pass
elif down:
pass
else:
heading = 180
if right:
if left:
pass
elif up:
pass
elif down:
pass
else:
heading = 0
t.setheading(heading)
t.forward(10)
# turtle controller
for i in range(3):#triangles move
if (l_tri[i].position()[0]<-200):
l_tri[i].setheading(180-l_tri[i].heading())
l_tri[i].forward(10)
if (l_tri[i].position()[0]>200):
l_tri[i].setheading(180-l_tri[i].heading())
l_tri[i].forward(10)
if (l_tri[i].position()[1]<-200):
l_tri[i].setheading(-l_tri[i].heading())
l_tri[i].forward(10)
if (l_tri[i].position()[1]>200):
l_tri[i].setheading(-l_tri[i].heading())
l_tri[i].forward(10)#triangle bounces on the wall
l_tri[i].forward(10)
is_touch(t.position()[0],t.position()[1],l_tri[i],threshold=20)
# check triangle and turtle
if (playing==False):
t.write("FAIL")
#Fail
up = False
down = False
right = False
left = False
playing = True
heading = 0
l_tri = []
t = turtle.Turtle()
t.shape('turtle')
t.speed(0)
draw_sq(t)
screen = t.screen
screen.onkeypress(turn_right, 'Right')
screen.onkeypress(turn_left, 'Left')
screen.onkeypress(turn_up, 'Up')
screen.onkeypress(turn_down, 'Down')
screen.onkeyrelease(_turn_right, 'Right')
screen.onkeyrelease(_turn_left, 'Left')
screen.onkeyrelease(_turn_up, 'Up')
screen.onkeyrelease(_turn_down, 'Down')
for i in range(3):
add_tri()
for i in range(1,200):
time = i*100
screen.ontimer(timer_go, time)
screen.ontimer(successMan,20000)
screen.listen()
screen.mainloop()
这应该是一个玩家避开盒子里的三角形的游戏。
解决方案
当我运行它时,我看到错误
RecursionError: maximum recursion depth exceeded while calling a Python object
但我没有看到代码中的递归。
也许问题是
for i in range(1,200):
time = i*100
screen.ontimer(timer_go, time)
可能它创建了太多功能。
你可以运行一次
screen.ontimer(timer_go, 100)
并在结束时再次使用它timer_go()
def timer_go():
# ... rest of code ...
screen.ontimer(timer_go, 100)
它会一直重复。
你甚至可以在官方文档中看到这个方法:turtle.ontimer
推荐阅读
- c# - 如何创建所有门对象的信息数组或列表?
- c# - 绑定列表时出现 XAML 标记错误
到 WPF 中的 ComboBox ItemSource - python - 在 Pandas 中查找日期范围的问题
- java - 为什么停止 MediaRecorder 时会出现 IllegalStateException?
- python-3.x - 筛选元组中的重复项 - Python
- python - 哪种(ML/DL)模型最适合较小数据集的多类分类?
- python - 在 PyTorch 中使用带有自调整优化器的调度程序
- python - 如何对数据框列上的多个短语应用正则表达式?
- java - 如何循环问题直到从扫描仪获得答案?
- ios - 应用程序在核心数据中崩溃,但无法找到崩溃的位置