AtassiCodes
AtassiCodes

Reputation: 92

TypeError: 'pygame.Surface' object is not callable (Problem with the arrays, in Pygame)

def outputting():
       screen.blit(backgroundE, [0, 0])
       z = random.randint(0, 3000)
       text1 = words[z]
       screen.blit(font400.render(text1, False, (255, 255, 255)), (100, 500))
       pygame.display.update()
       pygame.time.wait(10000)
...
outputting()

I couldn't find a solution on Stackoverflow related to my issue. I realized the problem is with my array. (The array contains a bunch of words). I couldn't post my entire code as it's almost 1000 lines now. Everything is working perfectly except when I introduced the array into the code I keep getting the error:

    text1 = words(z)
TypeError: 'pygame.Surface' object is not callable

I tried almost everything and am stuck. I need there to be an array or something similar, where I can store a set of (3000)words and I can randomly output one of them.

Any type of help would be great,

Upvotes: 1

Views: 180

Answers (1)

sloth
sloth

Reputation: 101042

text1 = words(z)
TypeError: 'pygame.Surface' object is not callable

words seems to be a Surface object, which is in fact not callable; you can't use () on that object. That's what the error message tells you.

But you also have this code:

 z = random.randint(0, 3000)
 text1 = words[z]

It seems here words should be a list of strings.

First, make sure you don't override your words list with another object (in your case, a Surface).

Second, to get a random element from a list, simply use random.choice.

Also you should not just call pygame.time.wait(10000), since during this time, your window does not get updated and does not respond. You should create an event loop and also handle the QUIT event in that loop.

Upvotes: 3

Related Questions