Sun
Sun

Reputation: 1

AttributeError: 'Paddle' object has no attribute 'screen'. Did you mean: '_screen'?

I'm using Turtle, trying to learn the basics of GameDev.

As I'm following the video, I got an error that the tutor didn't get.

My code is basically the same, the only diference is that I'm using more OOP.

Error: Traceback (most recent call last): File "C:\Users\Windows\Desktop\Jogos\Programming Languages\Python\Aulas\GameDev\Pong\pong.py", line 44, in paddle_a = Paddle('white',shape='square', coordinates=(-350, 0)) File "C:\Users\Windows\Desktop\Jogos\Programming Languages\Python\Aulas\GameDev\Pong\pong.py", line 6, in init self.shape(shape) File "C:\Users\Windows\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 2776, in shape if not name in self.screen.getshapes(): AttributeError: 'Paddle' object has no attribute 'screen'. Did you mean: '_screen'?

The video: https://youtu.be/XGf2GcyHPhc?t=77 - Timelines: 1:17 to 45:19

My code:

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)

Upvotes: 0

Views: 881

Answers (1)

UnoMo
UnoMo

Reputation: 1

I think you need to add the super:

super().__init__()

eg.:

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

Upvotes: 0

Related Questions