Home > Back-end >  Python (Turtle) Remove turtle / arrow
Python (Turtle) Remove turtle / arrow

Time:12-17

I am trying to make a basic Pong and I don't understand why the arrow/turtle in the middle of the screen displays.

The paddle on the screen consists of 6x turtle objects stored in self.paddle. I know the problem has something to do with the p = Paddle() but I don't understand where the arrow-object is since it seems to not be in the list mentioned above (print(self.paddle)). Can someone enlighten me?

from turtle import Turtle, Screen

screen = Screen()
screen.setup(width=1000, height=700)

class Paddle(Turtle):

    def __init__(self):
        super().__init__()
        self.paddle = []
        self.create_player_paddle()

    def create_player_paddle(self):
        for pad in range(0, 6):
            x = -480
            y = 30
            p = Turtle(shape="square")
            p.turtlesize(0.5)
            p.penup()
            p.goto(x, y)
            self.paddle.append(p)

        for part in range(len(self.paddle)):
            self.paddle[part].goto(x, y)
            y -= 10
            


p = Paddle()
screen.update()
screen.exitonclick()

CodePudding user response:

if you need to hide the Turtle pointer, put Turtle.hideturtle(self) in your constructor init function

CodePudding user response:

I don't understand where the arrow-object is since it seems to not be in the list mentioned above ... Can someone enlighten me?

Normally, a class like Paddle() either isa Turtle or contains one or more instances of Turtle. But you've done both, it is both a Turtle (the source of your mystery arrow) and it contains a list of turtles. The simple fix is to remove Turtle as its superclass and remove the super().__init__() code:

class Paddle():

    def __init__(self):
        self.paddle = []
        self.create_player_paddle()

Below is my rework of your code addressing the above and other issues I saw:

from turtle import Turtle, Screen

class Paddle():

    def __init__(self):
        self.segments = []
        self.create_player_paddle()

    def create_player_paddle(self):
        x = -480
        y = 30

        for _ in range(6):
            segment = Turtle(shape='square')
            segment.turtlesize(0.5)
            segment.penup()
            segment.goto(x, y)

            self.segments.append(segment)

            y -= 10

screen = Screen()
screen.setup(width=1000, height=700)

paddle = Paddle()

screen.exitonclick()

The usual approach to a Pong game in turtle is to make the paddle a subclass of Turtle and then use shapesize() to stretch the turtle to be the rectangle you desire. But this has issues with collision detection. Your approach makes the paddle somewhat more complicated, but might work better collision-detection-wise as you can test each segment.

  • Related