Home > Software engineering >  How can I call a function from inside another class and use it in a new file?
How can I call a function from inside another class and use it in a new file?

Time:10-07

I am trying to make it so I can call paddle_up and paddle_down in the second file but I can only get the function "move".

class PaddleOne(Turtle):

    def __init__(self):
        super().__init__()
        self.segments = [0]
        self.head = self.segments[0]
        self.Paddle_One()

    def Paddle_One(self):
        self.move()
        self.penup()
        self.color("white")
        self.shape("square")
        self.shapesize(stretch_wid=5, stretch_len=1)
        self.goto(x=350, y=0)
        self.head = self.segments[0]

    def move(self):
        # moving the paddle:
        def paddle_up():
            new_y = self.ycor()   20
            self.goto(self.xcor(), new_y)

        def paddle_down():
            new_y = self.ycor() - 20
            self.goto(self.xcor(), new_y)

This is the second file that I am trying to use the functions paddle_up and paddle_down however when I run the code I get the error message "AttributeError: type object 'PaddleOne' has no attribute 'paddle_up'".

from turtle import Screen
from UserPaddles import PaddleOne as PO
import time

# screen creation:
screen = Screen()
paddle = PO()

# screen setting:
screen.title("Pong Game")
screen.setup(width=800, height=600)
screen.bgcolor("black")

# key presses:
screen.listen()
screen.onkey(PO().paddle_up(), "w")
screen.onkey(PO().paddle_down(), "s")

I've looked for the answer elsewhere but no one seems to have the exact issue of calling a function within a function.

CodePudding user response:

Get rid of the move (don't think you need it), and then PaddleOne will have accessible attributes paddle_up & paddle_down

class PaddleOne(Turtle):

    def __init__(self):
        super().__init__()
        self.segments = [0]
        self.head = self.segments[0]
        self.Paddle_One()

    def Paddle_One(self):
        self.move()
        self.penup()
        self.color("white")
        self.shape("square")
        self.shapesize(stretch_wid=5, stretch_len=1)
        self.goto(x=350, y=0)
        self.head = self.segments[0]

  
  # moving the paddle:
  def paddle_up(self):
        new_y = self.ycor()   20
        self.goto(self.xcor(), new_y)

  def paddle_down():
       new_y = self.ycor() - 20
       self.goto(self.xcor(), new_y)

CodePudding user response:

paddle_up and paddle_down were scoped under the move() function but you are calling as instance. Just make paddle_Up and paddle_down instance functions

class PaddleOne(Turtle):

    def __init__(self):
        super().__init__()
        self.segments = [0]
        self.head = self.segments[0]
        self.Paddle_One()

    def Paddle_One(self):
        self.move()
        self.penup()
        self.color("white")
        self.shape("square")
        self.shapesize(stretch_wid=5, stretch_len=1)
        self.goto(x=350, y=0)
        self.head = self.segments[0]


    def paddle_up(self):
        new_y = self.ycor()   20
        self.goto(self.xcor(), new_y)

    def paddle_down(self):
        new_y = self.ycor() - 20
        self.goto(self.xcor(), new_y)
  • Related