Search code examples
pythontypeerrorturtle-graphics

Error: TypeError: 'list' object is not callable Using turtle graphics


I am attempting to code space invaders. I am doing some finishing touches however when trying to create three hearts in the top corner to represent lives I have this error: TypeError: 'list' object is not callable

The relevant code in main.py is:

from scoreboard import Scoreboard
scoreboard = Scoreboard()
scoreboard.lives()

In the scoreboard file the relevant lines are:

class Scoreboard(Turtle):
def __init__(self):
    super().__init__()
    self.lives = []
    self.score = 0
    self.lives_start_x = 0
    with open("High_Score.txt") as data:
        self.high_score = int(data.read())
    self.color("white")
    self.penup()
    self.update_scoreboard()
    self.hideturtle()

def lives(self):
    heart = Turtle()
    heart.penup()
    heart.goto(x=self.lives_start_x, y=0)
    heart.color('white')
    heart.hideturtle()
    heart.pendown()
    d = 10
    r = d / math.tan(math.radians(67.5))
    heart.up()
    heart.seth(45)
    heart.begin_fill()
    heart.down()
    heart.fd(d)
    heart.circle(r, 225)
    heart.left(180)
    heart.circle(r, 225)
    heart.fd(d)
    heart.end_fill()
    self.lives.append(heart)
    self.lives_start_x += 35

Any ideas?

the code in the lives function also worked fine on a separate file on it's own while I was testing it.

Also I need to create a alien in turtle that actually look like an alien so any help with that would be appreciated as well.

Thanks


Solution

  • Rename the lives list so that it does not replace the lives method.

    For example, you could change it to _lives:

    def __init__(self):
        super().__init__()
        self._lives = []
        self.score = 0
        self.lives_start_x = 0
        with open("High_Score.txt") as data:
            self.high_score = int(data.read())
        self.color("white")
        self.penup()
        self.update_scoreboard()
        self.hideturtle()
    
    def lives(self):
        heart = Turtle()
        heart.penup()
        heart.goto(x=self.lives_start_x, y=0)
        heart.color('white')
        heart.hideturtle()
        heart.pendown()
        d = 10
        r = d / math.tan(math.radians(67.5))
        heart.up()
        heart.seth(45)
        heart.begin_fill()
        heart.down()
        heart.fd(d)
        heart.circle(r, 225)
        heart.left(180)
        heart.circle(r, 225)
        heart.fd(d)
        heart.end_fill()
        self._lives.append(heart)
        self.lives_start_x += 35