-1

I was trying to make some kind of Boxel Rebound game for the micro:bit, and I was coding at the online web editor. It all worked fine, until I tried to implement the jumping. When I start the program, it works fine, and I get my debugging messages in the REPL:

Updating...
  Isn't jumping
Updating...
  Isn't jumping
Updating...
  Isn't jumping
...

But when I press button A, I get

Traceback (most recent call last):
  File "main.py", line 57, in <module>
TypeError: 'int' object isn't callable

This is my code:

from microbit import *

def scroll(*args, **kwargs):
    for arg in args:
        print(arg, **kwargs)
        display.scroll(arg)

#scroll('Boxel rebound')

WIDTH  = 4
HEIGHT = 4

class Player:
    b = 9
    
    def __init__(self):
        self.x = 1
        self.y = HEIGHT
        
        self.is_jumping = False
        self.jump       = 0
    
    def update(self):
        print('Updating...')
        if self.is_jumping:
            print('  Is jumping')
            self.jump += 1
            self.x    += 1
        else:
            print('  Isn\'t jumping')
            if self.y > HEIGHT:
                self.y += 1
        if self.jump >= 2:
            self.is_jumping = False
        
    def show(self):
        display.set_pixel(
            self.x,
            self.y,
            self.__class__.b
        )
    
    def jump(self):
        if not self.is_jumping:
            self.is_jumping = True

player = Player()

while True:
    display.clear()
    player.update()
    player.show()
    
    if button_b.get_presses() > 0:
        break
    elif button_a.get_presses() > 0:#button_a.is_pressed():
        player.jump() # This raises the error
    
    sleep(200)
    
display.clear()
LuisAFK
  • 846
  • 4
  • 22

2 Answers2

2

In class Player you defined member variable and function named jump. When calling jump method you are trying to call an integer which is not callable type. Just carefully rename one of those two members.

APM
  • 66
  • 3
1

Your Player Object has both an attribute and a method called jump (in your __init__ you have self.jump = 0). This is what your player.jump() is using (while you expect it to use the method) and you obviously cannot call an int as a method.

Change the name of one of the two (attribute or method) and it should work.

dm2
  • 4,053
  • 3
  • 17
  • 28