0

i'm trying to code a simple game inside micro:bit with python, this is the code:

def on_button_pressed_a():
    global imput_navicella22, navicella2
    if imput_navicella22 == 0:
        imput_navicella22 += 1
        navicella2 = game.create_sprite(0, 2)
        music.play_melody("D F G C5 E B G C5 ", 110)
        basic.pause(1000)
        navicella2.move(1)
        basic.pause(1000)
        navicella2.move(1)
        navicella2.set(LedSpriteProperty.Y, 2)
        navicella2.turn(Direction.RIGHT, 90)
        
        def on_button_pressed_a():
            global imput_navicella22
            if imput_navicella22 == 1:
                imput_navicella22 = 2
                
                def on_forever():
                    asteroide2 = game.create_sprite(4, Math.random_range(0, 4))
                    asteroide2.turn(Direction.RIGHT, 180)
                    for index in range(4):
                        basic.pause(velocita)
                        asteroide2.move(1)
                    asteroide2.delete()
                basic.forever(on_forever)
        input.on_button_pressed(Button.A, on_button_pressed_a)
                
            
    def on_forever2():
        if imput_navicella22 != 0:
            global navicella2
            navicella2.set(LedSpriteProperty.Y, 2)
            if abs(pitch) > 10:
                basic.pause(300)
                navicella2.change(LedSpriteProperty.Y, 1)
            if abs(pitch) > -10:
                basic.pause(300)
                navicella2.change(LedSpriteProperty.Y, -1)
    basic.forever(on_forever2)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_gesture_shake():
    control.reset()
input.on_gesture(Gesture.SHAKE, on_gesture_shake)

navicella2: game.LedSprite = None
imput_navicella22 = 0
velocita2 = 0
sprite = None
pitch = input.rotation(Rotation.PITCH)
velocita = 2000

but here:

def on_forever2():
        if imput_navicella22 != 0:
            global navicella2
            navicella2.set(LedSpriteProperty.Y, 2)
            if abs(pitch) > 10:
                basic.pause(300)
                navicella2.change(LedSpriteProperty.Y, 1)
            if abs(pitch) > -10:
                basic.pause(300)
                navicella2.change(LedSpriteProperty.Y, -1)
    basic.forever(on_forever2)

it says that can't find called function "navicella2.change" and unknown object type; cannot lookup attribute 'set' and 'change'

but i specified the variable "navicella2" before here:

navicella2 = game.create_sprite(0, 2)

and here:

navicella2.set(LedSpriteProperty.Y, 2)

what's wrong with my code? where do i need to change it?

biz
  • 1
  • Did you use some example code Biz? I have not programmed a micro:bit in python but why do you declare `on_button_pressed_a` as a new function inside of your original function `on_button_pressed_a`? – Scott Anderson Mar 25 '21 at 09:59

1 Answers1

0

It's weird that the error didn’t pop up, but variable names can’t have numbers in them.

It says unknown object type because it won't accept the numerical characters in the variable name. Try to write the numbers as names like twentytwo.

john-hen
  • 4,410
  • 2
  • 23
  • 40