有人知道这些错误意味着什么吗?
import turtle
sc = turtle.Screen()
sc.setup(width=1000, height=1000)
sc.title("pong")
sc.bgcolor("black")
sc.tracer(0)
def paddle_a():
paddle = turtle.Turtle()
paddle.penup()
paddle.color("white")
paddle.shape("square")
paddle.shapesize(stretch_wid=8.5, stretch_len=1)
paddle.goto(450, 0)
paddle.speed(0)
def paddle_b():
paddle = turtle.Turtle()
paddle.penup()
paddle.color("white")
paddle.shape("square")
paddle.shapesize(stretch_wid=8.5, stretch_len=1)
paddle.goto(-450, 0)
paddle.speed(0)
def ball_ball():
ball = turtle.Turtle()
ball.penup()
ball.color("white")
ball.shape("circle")
ball.shapesize(stretch_wid=1.5, stretch_len=1.5)
ball.goto(0, 0)
ball.speed(0)
ball.dx = 5
ball.dy =- 5
def paddleup():
y = paddle_a.ycore()
y =+ 20
paddle_a.sety(y)
sc.listen()
sc.onkeypress(paddleup, "w")
paddle_a()
paddle_b()
ball_ball()
while True:
sc.update()
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1885, in __call__
return self.func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/turtle.py", line 702, in eventfun
fun()
File "/Users//Python Projects/poooong.py", line 39, in paddleup
y = paddle_a.ycore()
AttributeError: 'function' object has no attribute 'ycore'
Traceback (most recent call last):
File "/Users//Python Projects/poooong.py", line 52, in <module>
sc.update()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/turtle.py", line 1304, in update
t._update_data()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/turtle.py", line 2647, in _update_data
self.screen._incrementudc()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/turtle.py", line 1293, in _incrementudc
raise Terminator
turtle.Terminator
我试着重新安装乌龟,但运气不好
您的代码存在许多问题。最明显的是,您创建了两个球拍和球,但从未将它们分配给paddle_a
、paddle_b
和ball
。除了@MillerTime注意到的ycore
问题之外,另一个问题是你使用了这样的运算符:
ball.dy =- 5
y =+ 20
这些错误或令人困惑,应该是:
ball.dy = -5
y += 20
以下是我对解决这些和其他问题的代码的修改:
from turtle import Screen, Turtle
def make_paddle(side):
paddle = Turtle()
paddle.penup()
paddle.color("white")
paddle.shape("square")
paddle.shapesize(stretch_len=8.5)
paddle.setheading(90) # so we can use forward() and backward()
paddle.setx(450 * side)
paddle.speed('fastest')
return paddle
def make_ball():
ball = Turtle()
ball.penup()
ball.color("white")
ball.shape("circle")
ball.shapesize(1.5)
ball.speed('fastest')
ball.dx = 5 # user defined properties
ball.dy = -5
return ball
def paddle_a_up():
paddle_a.forward(20)
screen.update()
def paddle_a_down():
paddle_a.backward(20)
screen.update()
screen = Screen()
screen.setup(width=1000, height=1000)
screen.bgcolor("black")
screen.title("pong")
screen.tracer(0)
paddle_a = make_paddle(-1)
paddle_b = make_paddle(1)
ball = make_ball()
screen.onkeypress(paddle_a_up, "w")
screen.onkeypress(paddle_a_down, "s")
screen.listen()
screen.update()
screen.mainloop()
最后,您使用了一个while True:
,它不属于像turtle这样的事件驱动环境。最终,你会想要使用屏幕ontimer()
事件来保持你的球在比赛中。