将乌龟带到其他乌龟的 y 坐标后,程序崩溃。任何修复?

After taking turtle to other turtle's y coordinate, the program crashes. Any fixes?

这是完整的代码

import os

wn = turtle.Screen()
wn.title("Pong")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.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.color("white")
paddle_a.shapesize(stretch_wid=5,stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)

# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5,stretch_len=1)
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.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Player 1: 0  Player 2: 0", align="center", font=("Impact", 24, "normal"))


# Functions
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)


# Keyboard bindings
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "s")

# Main game loop
while True:
    wn.update()

     # Computer player
    if ball.xcor() > 250:
        paddle_b.goto (ball.ycor())

    # Move the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # Border checking

    # Top and bottom
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1
        os.system("afplay bounce.wav&")

    elif ball.ycor() < -290:
        ball.sety(-290)
        ball.dy *= -1
        os.system("afplay bounce.wav&")

    # Left and right
    if ball.xcor() > 350:
        score_a += 1
        pen.clear()
        pen.write("Player 1: {}  Player 2: {}".format(score_a, score_b), align="center", font=("Impact", 24, "normal"))
        ball.goto(0, 0)
        ball.dx *= -1

    elif ball.xcor() < -350:
        score_b += 1
        pen.clear()
        pen.write("Player 1: {}  Player 2: {}".format(score_a, score_b), align="center", font=("Impact", 24, "normal"))
        ball.goto(0, 0)
        ball.dx *= -1

    # Paddle and ball collisions
    if ball.xcor() < -340 and ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() - 50:
        ball.dx *= -1 
        os.system("afplay bounce.wav&")

    elif ball.xcor() > 340 and ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() - 50:
        ball.dx *= -1
        os.system("afplay bounce.wav&")

我正在尝试用 # Computer player if ball.xcor() > 250: paddle_b.goto (ball.ycor())

制作一个电脑播放器

每当球的 xcor 超过 250 时,程序就会崩溃。有任何修复吗?

它不会让我 post 直到我有更多的文本请忽略它 它不会让我 post 直到我有更多的文字请忽略这个 它不会让我 post 直到我有更多的文字请忽略这个

我总觉得

有人在看我

而且我没有隐私

哦哦哦哦

所以,您的主要问题是 paddle_b.goto 使用 x/y 点,而不仅仅是您从球输入的 y 坐标。

这应该是给你的错误:

Traceback (most recent call last):
  File "<Path>", line 85, in <module>
    paddle_b.goto(ball.ycor())
  File "<Path>\turtle.py", line 1774, in goto
    self._goto(Vec2D(*x))
TypeError: type object argument after * must be an iterable, not float

来自 goto ( https://docs.python.org/3.3/library/turtle.html?highlight=turtle#turtle.goto ) 的文档:

If y is None, x must be a pair of coordinates or a Vec2D (e.g. as returned by pos()).

你应该可以这样做: paddle_b.sety(ball.ycor()) 相反,它只需要 y 坐标并且你已经使用了。或者,解决此问题的另一种方法是通过以下方式使球拍的 X 坐标保持不变:paddle_b.goto(paddle_b.xcor(), ball.ycor()).