Python turtle object wont show in window

Viewed 53

I am currently trying to make a copy of Atari's breakout using python and turtle. Previously I have created a pong copy which does not use OOP and it worked perfectly fine. However due to the bricks in breakout I decided to use OOP and create an object class for the bricks. Once I run the program, it wont display the brick. Any ideas to why?

import turtle

window = turtle.Screen()
window.title('Atari Breakout')
window.bgcolor('black')
window.setup(width=800, height=600)
window.tracer(0)


class Brick(Turtle):
    def __init__(self):
        super().__init__(shape='square', visible=True)
        self.myturtle = turtle.Turtle()
        self.color = 'white'
        self.shapesize(stretch_wid=10, stretch_len=4)
        self.pendown()
        self.goto(-350, 200)


board1 = Brick()
window.update()
1 Answers

There are a few problems in this code - firstly, you need turtle.Turtle not just Turtle as otherwise this causes an error. Secondly, the line self.myturtle = turtle.Turtle() is unnecessary as super().__init__(shape='square', visible=True) already creates a turtle, and thirdly self.color = 'white' should be changed to self.color('white'). Also, I'm pretty sure you meant self.penup() not self.pendown() to stop the brick from drawing a line from the center to its position.

Completed code:

import turtle

window = turtle.Screen()
window.title('Atari Breakout')
window.bgcolor('black')
window.setup(width=800, height=600)
window.tracer(0)


class Brick(turtle.Turtle):
    def __init__(self):
        super().__init__(shape='square', visible=True)
        self.color('white')
        self.shapesize(stretch_wid=10, stretch_len=4)
        self.penup()
        self.goto(-350, 200)


board1 = Brick()
window.update()
Related