问题描述
我正在尝试使用 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
之类的名称。