Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading