AttributeError:“Paddle”对象没有属性“screen”。您的意思是:“_screen”吗?

问题描述 投票:0回答:1

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

当我关注视频时,我遇到了导师没有得到的错误。

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

错误: 回溯(最近一次调用最后一次): 文件“C:\Users\Windows\Desktop\Jogos\Programming Languages\Python\Aulas\GameDev\Pong\pong.py”,第 44 行,位于 paddle_a = Paddle('白色',形状='方形',坐标=(-350, 0)) 文件“C:\Users\Windows\Desktop\Jogos\Programming Languages\Python\Aulas\GameDev\Pong\pong.py”,第 6 行,位于 init 中 自我形状(形状) 文件“C:\Users\Windows\AppData\Local\Programs\Python\Python310\lib urtle.py”,第 2776 行,形状 如果 self.screen.getshapes() 中没有名称: 属性错误:“桨”对象没有属性“屏幕”。您的意思是:“_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)
python turtle-graphics python-turtle pong
1个回答
0
投票

我认为你需要添加超级:

super().__init__()

例如:

    class Paddle(turtle.Turtle):
        def __init__(self, color: str, shape: str =None, deltaMov=None, coordinates=None):
        super().__init__()
© www.soinside.com 2019 - 2024. All rights reserved.