当我在 python pong 中超过 __instancecheck__ 中的最大递归深度时,我该怎么办?
What do I do when I exceed maximum recursion depth in __instancecheck__ in python pong?
我在 youtube 教程的帮助下创建了 pong https://www.youtube.com/watch?v=XGf2GcyHPhc&ab_channel=freeCodeCamp.org , and everything works, except the ball goes really fast, and then really slow and so on. The speed of the ball is inconsistent and this makes it hard to play. I did some research and found this, How to fix inconsistent frame rate (speed) in python turtle ,我想我做了我必须做的,但现在程序运行了几秒钟,然后停止并给出了这个错误: RecursionError: maximum recursion depth exceeded in instancecheck ,我该怎么办?
这是我现在的代码:
import turtle
window = turtle.Screen()
window.title("Pong by @Ubevg")
window.bgcolor("black")
window.setup(width=800, height=600)
window.tracer(0)
# score
score_A = 0
score_B = 0
# paddle A
paddle_A = turtle.Turtle()
paddle_A.speed(0)
paddle_A.shape("square")
paddle_A.shapesize(stretch_wid=5, stretch_len=1)
paddle_A.color("white")
paddle_A.penup()
paddle_A.goto(-350, 0)
# paddle B
paddle_B = turtle.Turtle()
paddle_B.speed(0)
paddle_B.shape("square")
paddle_B.shapesize(stretch_wid=5, stretch_len=1)
paddle_B.color("white")
paddle_B.penup()
paddle_B.goto(350, 0)
# ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 0.5
ball.dy = 0.5
# pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Player A: " + str(score_A) + " " + "Player B: " + str(score_B), align="center", font=("Courier", 24, "normal"))
# function
def paddle_A_up():
y = paddle_A.ycor()
y += 20
paddle_A.sety(y)
def paddle_A_down():
y = paddle_A.ycor()
y -= 20
paddle_A.sety(y)
def paddle_B_up():
y = paddle_B.ycor()
y += 20
paddle_B.sety(y)
def paddle_B_down():
y = paddle_B.ycor()
y -= 20
paddle_B.sety(y)
# keybind
window.listen()
window.onkeypress(paddle_A_up, ("w"))
window.onkeypress(paddle_A_down, ("s"))
window.onkeypress(paddle_B_up, ("Up"))
window.onkeypress(paddle_B_down, ("Down"))
# main game loop
def move_balls():
global score_A, score_B
window.update()
# move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
elif ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
if ball.xcor() > 390:
ball.goto(0, 0)
ball.dx *= -1
score_A += 1
pen.clear()
pen.write("Player A: " + str(score_A) + " " + "Player B: " + str(score_B), align="center",
font=("Courier", 24, "normal"))
elif ball.xcor() < -390:
ball.goto(0, 0)
ball.dx *= -1
score_B += 1
pen.clear()
pen.write("Player A: " + str(score_A) + " " + "Player B: " + str(score_B), align="center",
font=("Courier", 24, "normal"))
# paddle and ball collisions
if ball.xcor() > 340 and ball.xcor() < 350 and (ball.ycor() < paddle_B.ycor() + 40 and ball.ycor() > paddle_B.ycor() -40):
ball.setx(340)
ball.dx *= -1
elif ball.xcor() < -340 and ball.xcor() > -350 and (ball.ycor() < paddle_A.ycor() + 40 and ball.ycor() > paddle_A.ycor() -40):
ball.setx(-340)
ball.dx *= -1
window.ontimer(move_balls(), 100)
move_balls()
move_balls
里面有window.ontimer(move_balls(), 100)
,就是这个问题。您需要将 window.ontimer
调用放在该函数之外,以避免无限递归。否则该函数将每 100 毫秒调用一次自身,然后每次调用将每 100 毫秒再次调用自身,并且这种情况会成倍增加,直到程序崩溃。
我在 youtube 教程的帮助下创建了 pong https://www.youtube.com/watch?v=XGf2GcyHPhc&ab_channel=freeCodeCamp.org , and everything works, except the ball goes really fast, and then really slow and so on. The speed of the ball is inconsistent and this makes it hard to play. I did some research and found this, How to fix inconsistent frame rate (speed) in python turtle ,我想我做了我必须做的,但现在程序运行了几秒钟,然后停止并给出了这个错误: RecursionError: maximum recursion depth exceeded in instancecheck ,我该怎么办? 这是我现在的代码:
import turtle
window = turtle.Screen()
window.title("Pong by @Ubevg")
window.bgcolor("black")
window.setup(width=800, height=600)
window.tracer(0)
# score
score_A = 0
score_B = 0
# paddle A
paddle_A = turtle.Turtle()
paddle_A.speed(0)
paddle_A.shape("square")
paddle_A.shapesize(stretch_wid=5, stretch_len=1)
paddle_A.color("white")
paddle_A.penup()
paddle_A.goto(-350, 0)
# paddle B
paddle_B = turtle.Turtle()
paddle_B.speed(0)
paddle_B.shape("square")
paddle_B.shapesize(stretch_wid=5, stretch_len=1)
paddle_B.color("white")
paddle_B.penup()
paddle_B.goto(350, 0)
# ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 0.5
ball.dy = 0.5
# pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Player A: " + str(score_A) + " " + "Player B: " + str(score_B), align="center", font=("Courier", 24, "normal"))
# function
def paddle_A_up():
y = paddle_A.ycor()
y += 20
paddle_A.sety(y)
def paddle_A_down():
y = paddle_A.ycor()
y -= 20
paddle_A.sety(y)
def paddle_B_up():
y = paddle_B.ycor()
y += 20
paddle_B.sety(y)
def paddle_B_down():
y = paddle_B.ycor()
y -= 20
paddle_B.sety(y)
# keybind
window.listen()
window.onkeypress(paddle_A_up, ("w"))
window.onkeypress(paddle_A_down, ("s"))
window.onkeypress(paddle_B_up, ("Up"))
window.onkeypress(paddle_B_down, ("Down"))
# main game loop
def move_balls():
global score_A, score_B
window.update()
# move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
elif ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
if ball.xcor() > 390:
ball.goto(0, 0)
ball.dx *= -1
score_A += 1
pen.clear()
pen.write("Player A: " + str(score_A) + " " + "Player B: " + str(score_B), align="center",
font=("Courier", 24, "normal"))
elif ball.xcor() < -390:
ball.goto(0, 0)
ball.dx *= -1
score_B += 1
pen.clear()
pen.write("Player A: " + str(score_A) + " " + "Player B: " + str(score_B), align="center",
font=("Courier", 24, "normal"))
# paddle and ball collisions
if ball.xcor() > 340 and ball.xcor() < 350 and (ball.ycor() < paddle_B.ycor() + 40 and ball.ycor() > paddle_B.ycor() -40):
ball.setx(340)
ball.dx *= -1
elif ball.xcor() < -340 and ball.xcor() > -350 and (ball.ycor() < paddle_A.ycor() + 40 and ball.ycor() > paddle_A.ycor() -40):
ball.setx(-340)
ball.dx *= -1
window.ontimer(move_balls(), 100)
move_balls()
move_balls
里面有window.ontimer(move_balls(), 100)
,就是这个问题。您需要将 window.ontimer
调用放在该函数之外,以避免无限递归。否则该函数将每 100 毫秒调用一次自身,然后每次调用将每 100 毫秒再次调用自身,并且这种情况会成倍增加,直到程序崩溃。