CrazyKanav
CrazyKanav

Reputation: 45

Python turtle game str' object is not callable

I am reading from a book, and then I was following the instructions but this error occurred

  File "C:\Users\kanav_i4cko4c\OneDrive\Desktop\python\New python tutorial\Caterrpillar.py", line 125, in start_game
    game_over()
  File "C:\Users\kanav_i4cko4c\OneDrive\Desktop\python\New python tutorial\Caterrpillar.py", line 78, in game_over
    leaf.color('yellow')
TypeError: 'str' object is not callable

The error is in this line : leaf.color('yellow')

The full code is :

import random
import turtle as t

# Making background yellow

t.bgcolor('yellow')


# Code for creating the turtle

# Create the turtle
caterpillar = t.Turtle()

# It's shape is square
caterpillar.shape('square')

# Color is red
caterpillar.color('red')

# We don't want the turtle to move before the game starts
caterpillar.speed(0)

caterpillar.penup()
# This will hide the turtle
caterpillar.hideturtle()

      
# Create the leaf

# This will create the entity
leaf = t.Turtle()

#  shape size and length width
leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))

# This line tells the turtle about the leaf shape
t.register_shape('leaf', leaf_shape)
leaf.shape('leaf')
leaf.color = ('green')

leaf.penup()

leaf.hideturtle()

leaf.speed(0)


# Add the text screen

game_started = False
text_turtle = t.Turtle()
text_turtle.write('Press SPACE to start', align='center',font=('Times', 16, 'bold'))
text_turtle.hideturtle()
score_turtle = t.Turtle()
score_turtle.hideturtle()
score_turtle.speed(0)

# Checks if the caterpillar is outside the window
# It checks by finding the coordinates of the left,right,top and bottom wall and the check
# it with the coordinates of the caterpillar
def outside_window():
      left_wall = -t.window_width() / 2
      right_wall = t.window_width() / 2
      top_wall = t.window_height() / 2
      bottom_wall = -t.window_height() / 2
      
      (x, y) = caterpillar.pos()
      outside = \
              x< left_wall or \
              x> right_wall or \
              y< bottom_wall or \
              y> top_wall
      return outside

# Display "GAME OVER" when the turtle is outside window
def game_over():
      caterpillar.color('yellow')
      leaf.color('yellow')
      t.penup()
      t.hideturtle()
      t.write('GAME OVER!', align='center', font=('Arial', 30, 'normal'))

# Function to display the score
def display_score(current_score):
      score_turtle.clear()
      score_turtle.penup()
      x = (t.window_width() / 2) - 50
      y = (t.window_height() / 2) - 50
      score_turtle.setpos(x,y)
      score_turtle.write(str(current_score), align='right', \
                        font=('Arial', 40, 'bold'))
      

def place_leaf():
      pass

def start_game():
      global game_started
      if game_started:
            return
      game_started = True

      score = 0
      # Clear the text
      text_turtle.clear()

      caterpillar_speed = 2
      caterpillar_length = 3
      caterpillar.shapesize(1, caterpillar_length, 1)
      caterpillar.showturtle()
      display_score(score)
      place_leaf()

      while True:
            caterpillar.forward(caterpillar_speed)
            # caterpillar eats the leaf when 20 pixels away from it
            if caterpillar.distance(leaf) < 20:
                  place_leaf()
                  caterpillar_length = caterpillar_length + 1
                  caterpillar.shapesize(1, caterpillar_length, 1)
                  caterpillar_speed = caterpillar_speed + 1
                  score = score + 10
                  display_score(score)
            if outside_window():
                  game_over()
                  break

# Code for the situation when the player presses a key
t.onkey(start_game, 'space')
t.listen()
t.mainloop()
      

##
##  File "C:\Users\kanav_i4cko4c\OneDrive\Desktop\python\New python tutorial\Caterrpillar.py", line 125, in start_game
##    game_over()
##  File "C:\Users\kanav_i4cko4c\OneDrive\Desktop\python\New python tutorial\Caterrpillar.py", line 78, in game_over
##    leaf.color('yellow')
##TypeError: 'str' object is not callable

Upvotes: 0

Views: 627

Answers (2)

Chris Doyle
Chris Doyle

Reputation: 12002

The issue here is that color is a method of leaf which is a turtle object. you then reassign that from being a method to being a str.

leaf.color = ('green')

Now leaf.color is just the string "green" instaed of the method. You should set the color for the leaf using its method like you do for caterpillar

caterpillar.color('red')

So it should be

leaf.color('green')

Upvotes: 1

CrazyKanav
CrazyKanav

Reputation: 45

The answer is actually very simple, Because

leaf.color('yellow') can also be put as leaf.color = 'yellow'

Upvotes: 0

Related Questions