属性错误:'Paddle'对象没有属性"屏幕"。你的意思是:"_screen"?



我正在使用Turtle,试图学习GameDev的基础知识。

当我跟随视频时,我得到了一个导师没有得到的错误。

我的代码基本相同,唯一的区别是我使用了更多的OOP。

错误:回溯(最近一次调用):文件"C:UsersWindowsDesktopJogosProgramming LanguagesPythonAulasGameDevPong Pong .py",第44行,在paddle_a =桨('白色',形状='正方形',坐标=(- 350,0))文件"C:UsersWindowsDesktopJogosProgramming LanguagesPythonAulasGameDevPong Pong .py",第6行,在initself.shape(形状)文件"C:UsersWindowsAppDataLocalProgramsPythonPython310libturtle.py",行2776,形状如果self.screen.getshapes()中没有name:AttributeError: 'Paddle'对象没有属性'screen'。你是说"_screen"吗?

视频:https://youtu.be/XGf2GcyHPhc?t=77 -时间轴:1:17至45:19

我代码:

import turtle
class Paddle(turtle.Turtle):
def __init__(self, color: str, shape: str =None, deltaMov=None, coordinates=None):
if shape:
self.shape(shape)
self.speed(0)
self.color(color)
self.penup()
match coordinates:
case [int(x), int(y)]:
self.goto(x, y)
case _:
self.goto(0, 0)
match deltaMov:
case [int(x), int(y)] | [float(x), float(y)]:
self.dx = x
self.dy = y
self.originals = (x, y)
case _:
self.dx = 0
self.dy = 0    

wn = turtle.Screen()
wn.title('Pong by Sun') 
wn.bgcolor('black') 
wn.setup(width=800, height=600)
wn.tracer(0)
paddle_a = Paddle('white',shape='square', coordinates=(-350, 0))
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_b = Paddle('white', shape='square', coordinates=(350, 0))
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
ball = Paddle('white', shape='square', deltaMov=(0.6, 0.6))
placar = Paddle('white', coordinates=(0, 360))
placar.hideturtle()
placar.write('Player A: 0     Player B: 0', align='center', font=('Courier', 24, 'normal'))
def move_paddleA_up():
# Gets the coordinates
y = paddle_a.ycor()

# If the paddle is off-screen, do nothing
if y > 300:
return

# Moves up
y += 20
paddle_a.sety(y)
def move_paddleA_down():
y = paddle_a.ycor()
y -= 20
if y < -300:
return
paddle_a.sety(y)
# Moving paddle_b
def move_paddleB_up():
# Gets the coordinates
y = paddle_b.ycor()

# If the paddle is off-screen, do nothing
if y > 300:
return

# Moves up
y += 20
paddle_b.sety(y)
def move_paddleB_down():
y = paddle_b.ycor()
y -= 20
if y < -300:
return
paddle_b.sety(y)
def isBallTouchingPaddle(ball: Paddle, paddle: Paddle, pos: str):
if (pos := pos.upper()) == 'R':
return (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle.ycor() + 40 and ball.ycor() > paddle.ycor() - 40)
elif pos == 'L':
return (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle.ycor() + 40 and ball.ycor() > paddle.ycor() - 40) 

def move_ball(ball: Paddle, a: Paddle, b: Paddle):
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)

# Border Checking:
if ball.ycor() > 290 or ball.ycor() < -290:
ball.dy *= -1
if ball.xcor() > 390 or ball.xcor() < -390:
ball.goto(0, 0)
ball.dx = ball.originals[0]
ball.dx *= -1

# Paddle checking:
if isBallTouchingPaddle(ball, b, 'r') or isBallTouchingPaddle(ball, a, 'l'):
if ball.dx > 0:
ball.setx(340)
ball.dx += 0.2
else:
ball.setx(-340)
ball.dx -= 0.2
ball.dx *= -1
wn.listen()
wn.onkeypress(move_paddleA_up, 'w')
wn.onkeypress(move_paddleA_down, 's')
wn.onkeypress(move_paddleB_up, 'Up')
wn.onkeypress(move_paddleB_down, 'Down')
while True:
wn.update()

# Moving the ball
move_ball(ball, paddle_a, paddle_b)

我认为你需要添加super:

super().__init__()

如:

class Paddle(turtle.Turtle):
def __init__(self, color: str, shape: str =None, deltaMov=None, coordinates=None):
super().__init__()

相关内容

  • 没有找到相关文章

最新更新