1

This is my code where I get the error:

# main loop
while True:
    events()
    keys = pygame.key.get_pressed()
    if not keys(K_SPACE): continue

    if player_y <= platform_y <= player_y + falling_velocity:
        player_y = platform_y
    else:
        player_y += falling_velocity

    pygame.draw.circle(DS, WHITE, (player_y, player_x - 25), 25, 0)
    pygame.draw.line(DS, WHITE, (0, platform_y), (W, platform_y), 1)

    pygame.display.update()
    CLOCK.tick(FPS)
    DS.fill(BLACK)

I think the error is in this line of code:

if not keys(K_SPACE): continue

Error: 
tuple object not callable
Lambda Fairy
  • 13,814
  • 7
  • 42
  • 68
Inlowik
  • 59
  • 6
  • 2
    As the error says, `keys` isn't a function. Do you mean `if K_SPACE not in keys:?` – Carcigenicate Jun 30 '20 at 22:17
  • Or perhaps `not keys[K_SPACE]` ? – Kingsley Jun 30 '20 at 22:21
  • @Kingsley Since `keys` is a tuple, not a dictionary, that seems unlikely. – Barmar Jun 30 '20 at 22:52
  • @Barmar - Python `type()` might call it a tuple, but that's the standard way of using results from `pygame.key.get_pressed()`. The clause `not keys[pygame.K_SPACE]` is perfectly valid. Probably it's more than *only* a tuple. – Kingsley Jun 30 '20 at 23:12

2 Answers2

2

pygame.key.get_pressed() returns a sequence of of boolean values representing the state of every key.

keys = pygame.key.get_pressed()

Regardless if the return value is a list or a tuple, then elements can be get by Subscription (keys[K_SPACE]):

keys = pygame.key.get_pressed()
if not keys[K_SPACE]: continue

Note, keys(K_SPACE) is a Call. And keys would have to be a callable object like a function.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
-1

I suggest changing this:

keys = pygame.key.get_pressed()
if not keys(K_SPACE): continue

to:

keys = ''
while (str(keys) == '' or keys == None): keys = pygame.key.get_pressed()
Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29
  • It looks like he wants to wait for the space key to be pressed. I think your code is waiting for any key. – Barmar Jun 30 '20 at 22:53
  • Why convert `keys` to a string? Just check its length `while keys is None or len(keys) == 0:` – Barmar Jun 30 '20 at 22:55
  • Did you down vote my answer just for that?!! I'm supposed to give him a hint not to write the exact code for him and your code is wrong because it will give an error since you didn't initialize keys..that's why I wrote keys = '' – Chadee Fouad Jul 01 '20 at 05:23
  • Oh then I apologize then. Sorry about that :-) – Chadee Fouad Jul 02 '20 at 15:35