Search code examples
pythongraphicsturtle-graphics

Python Turtle Graphics : Created class error when using goto


I am attempting to recreate the breakout game using turtle graphics.

however, whenever I try and move the bricks I've created from a custom class in my main script, I get an Attribute error - AttributeError: 'Brick' object has no attribute '_drawing'.

I've created paddles that work fine using the same method, however creating the bricks is causing an error, I don't know why this is occurring as I'm not attempting to draw, just move the brick into a certain position to start the game.

Codeflow is as follows :

  1. create the paddle class which inherits all turtle attributes and used to control the paddle at bottom of screen
  2. create the brick class which will be 3 rows of 5 bricks
  3. create a ball which will bounce around -- not complete
  4. have a main script which runs all the creation and game logic

paddle class code:


import turtle as tt

movespeed = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0



class Paddle(tt.Turtle):
    def __init__(self):
        self.paddle = tt.Turtle(shape='square')
        self.paddle.color('white')
        self.paddle.penup()
        self.paddle.shapesize(1, 6, 1)

        starting_x = self.paddle.xcor()
        starting_y = -350

        self.paddle.goto(starting_x, starting_y)





    def paddle_left(self):
        self.paddle.setheading(LEFT)
        new_x = self.paddle.xcor()
        new_y = self.paddle.ycor()
        self.paddle.goto(new_x, new_y)
        self.paddle.forward(15)

    def paddle_right(self):
        self.paddle.setheading(RIGHT)
        new_x = self.paddle.xcor()
        new_y = self.paddle.ycor()
        self.paddle.goto(new_x, new_y)
        self.paddle.forward(15)

brick class code:

import turtle as tt
import random
color_list = ['red','green','blue', 'yellow','orange', 'purple']



class Brick(tt.Turtle):
    def __init__(self):
        self.brick = tt.Turtle(shape='square')
        self.brick.color(random.choice(color_list))
        self.brick.penup()
        self.brick.shapesize(1, 6, 1)

    # def pop(self):
    #     self.remove()

main screen / brains code:

import turtle as tt
import random
import time
import paddle
import brick



screen = tt.Screen()
screen.setup(width=800, height=800)
screen.bgcolor('black')
screen.title('Breakout - An Atari classic')
screen.tracer(0)


pad = paddle.Paddle()
br = brick.Brick()
br.goto(-400,-400)



screen.onkey(pad.paddle_left, "Left")
screen.onkey(pad.paddle_right, "Right")

screen.onkeypress(pad.paddle_left, "Left")
screen.onkeypress(pad.paddle_right, "Right")
screen.onkeyrelease(pad.paddle_left, "Left")
screen.onkeyrelease(pad.paddle_right, "Right")

screen.listen()


game_on = True

while game_on:
    screen.update()
    time.sleep(0.1)


screen.mainloop()

I tried to invoke

super().__init__()

when creating the brick object, however this just puts the brick in the middle of my screen and I can't move it anywhere.

I cant find specific information related to this error. I understand it tells me that there is no attribute associated to the object, however i think ive passed in the turtle class so it should inherit it no? additionally, the same method for the paddle ive created is fine and working as intended, gets placed on canvas and moves and updates.


Solution

  • I think i might have figured out my answer.

    I think I am attempting to access the instance of the class i have created and not the brick object which is created as part of the class.

    if i do brick.brick.goto then i think it should work.