Search code examples
pythontypeerrormicropythonbbc-microbit

Why am I getting a TypeError: 'int' object not callable in a micro:bit MicroPython program?


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()

Solution

  • 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.