我找不到我的微位代码有什么问题,即使我指定了变量也找不到

问题描述

我正在尝试使用 python 在 micro:bit 中编写一个简单的游戏, 这是代码

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

在这里

def on_forever2():
        if imput_navicella22 != 0:
            global navicella2
            navicella2.set(LedSpriteProperty.Y,-1)
    basic.forever(on_forever2)

它说找不到被调用函数“navicella2.change”未知的对象类型;无法查找属性 'set''change'

但我在这里之前指定了变量 "navicella2":

navicella2 = game.create_sprite(0,2)

这里:

navicella2.set(LedSpriteProperty.Y,2)

我的代码有什么问题? 我需要在哪里更改它?

解决方法

奇怪的是没有弹出错误,但是变量名中不能有数字。

它说 unknown object type 因为它不接受变量名中的数字字符。尝试将数字写成 twentytwo 之类的名称。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...