Home > Software engineering >  Paddle object from class wont appear
Paddle object from class wont appear

Time:09-07

I want to make a flappy bird game and am currently trying to turn paddle a into a class but it wont appear while it should. Im also not getting any error messages and the code can run but without any signs of paddle x(the object from the class). this is the code:

class Paddle:
Paddle = turtle.Turtle()
Paddle.penup()
Paddle.goto(0,0)
def __init__(self,shape,color,stretch_wid,stretch_len):
    self.shape = shape
    self.color = color
    self.stretch_wid = stretch_wid
    self.stretch_len = stretch_len
paddle_x = Paddle("square","white",5,5)
paddle_a = turtle.Turtle()
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=20,stretch_len=5)
paddle_a.penup()
paddle_a.goto(150,-250)

I already tried putting the 3 lines of codes after the "class Paddle:" under the class and then also changing the "Paddle" into paddle_x but then it says "'Paddle' object has no attribute 'penup'". First time asking a question. Thanks in advance.

CodePudding user response:

Having worked with object inheritance with some polygon classes, I believe you will need to reference the "Turtle" object within the class definition of your "Paddle" object as in the following code snippet.

import turtle

class Paddle(turtle.Turtle):            # Define as a child of the Turtle object

    def __init__(self,shape,color,stretch_wid,stretch_len):     
        turtle.Turtle.__init__(self)    # Use the Turtle object's initialization
        self.shape = shape
        self.color = color
        self.stretch_wid = stretch_wid
        self.stretch_len = stretch_len
        
paddle_x = Paddle("square","white",5,5)
paddle_x.penup()
paddle_x.goto(0,0)
print(paddle_x.shape)

paddle_a = turtle.Turtle()
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=20,stretch_len=5)
paddle_a.penup()
paddle_a.goto(150,-250)

It kind of utilizes the C method of initializing a child object based upon the parent (or "super") class. When trying that code snippet out, the screen appears briefly with a turtle pointer and prints out the paddle value requested in the test.

@Una:~/Python_Programs/Paddle$ python3 Paddle.py 
square

You might give that a try.

Additional notes.

Referring to your comments and getting a better understanding of what you are trying to do with your "Paddle" class with a one-off initialization of the attributes, following is a slightly revised version of the sample code.

import turtle

class Paddle(turtle.Turtle):        # Define as a child of the Turtle object

    def __init__(self,shape,color,stretch_wid,stretch_len):     
        turtle.Turtle.__init__(self)    # Use the Turtle objects initialization
        self.shape(shape)
        self.color(color)
        self.shapesize(stretch_wid = stretch_wid, stretch_len=stretch_len)
        
paddle_x = Paddle("square","blue",5,5)
paddle_x.penup()
paddle_x.goto(0,0)

paddle_a = turtle.Turtle()
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=20,stretch_len=5)
paddle_a.penup()
paddle_a.goto(150,-250)

while True:
    pass

That produces a square as per your specifications.

Square Paddle

I made the square a blue color since my background happened to be white.

Give that a try.

  • Related