作为学习、练习与尝试,这里创建一个斐波那契图块和螺旋的小游戏。
打开网页版:https://arcade.makecode.com/,设置项目名称:斐波那契图块和螺旋
MicroPython实验代码
- def on_up_pressed():
- global cy
- cy += -15
- scene.center_camera_at(cx, cy)
- controller.up.on_event(ControllerButtonEvent.PRESSED, on_up_pressed)
-
- def on_down_pressed():
- global cy
- cy += 15
- scene.center_camera_at(cx, cy)
- controller.down.on_event(ControllerButtonEvent.PRESSED, on_down_pressed)
-
- def on_right_pressed():
- global cx
- cx += 20
- scene.center_camera_at(cx, cy)
- controller.right.on_event(ControllerButtonEvent.PRESSED, on_right_pressed)
-
- def drawArc():
- global x, y
- # draw a circle arc using random dots!
- for index in range(100000):
- x = randint(0, f)
- y = randint(0, f)
- # test if the point will draw the circle
- if x * x + y * y >= (f - 1) ** 2 and x * x + y * y < (f + 1) ** 2:
- if rotate == 0:
- x = f - x
- y = f - y
- elif rotate == 1:
- x = f - x
- elif rotate == 3:
- y = f - y
- fibSquare.set_pixel(x, y, 1)
-
- def on_left_pressed():
- global cx
- cx += -20
- scene.center_camera_at(cx, cy)
- controller.left.on_event(ControllerButtonEvent.PRESSED, on_left_pressed)
-
- fibSprite: Sprite = None
- f0 = 0
- fibSquare: Image = None
- rotate = 0
- y = 0
- f = 0
- x = 0
- cy = 0
- cx = 0
- clr = 1
- repeat = 14
- f1 = 1
- fibSprite0 = sprites.create(img("""
- .
- """), 0)
- cx = scene.screen_width() / 2
- cy = scene.screen_height() / 2
- fibSprite0.top += 20
-
- def on_update_interval():
- global f0, f1, f, repeat, fibSquare, clr, fibSprite, rotate, fibSprite0
- if repeat > 0:
- f0 = f1
- f1 = f
- f = f1 + f0
- info.set_score(f)
- repeat += -1
- fibSquare = image.create(f, f)
- fibSquare.fill_rect(0, 0, f, f, clr)
- clr += 1
- fibSprite = sprites.create(fibSquare, 0)
- if rotate == 0:
- fibSprite.top = fibSprite0.bottom
- fibSprite.left = fibSprite0.left
- rotate += 1
- elif rotate == 1:
- fibSprite.bottom = fibSprite0.bottom
- fibSprite.left = fibSprite0.right
- rotate += 1
- elif rotate == 2:
- fibSprite.bottom = fibSprite0.top
- fibSprite.right = fibSprite0.right
- rotate += 1
- elif rotate == 3:
- fibSprite.top = fibSprite0.top
- fibSprite.right = fibSprite0.left
- rotate = 0
- drawArc()
- fibSprite0 = fibSprite
- game.on_update_interval(1000, on_update_interval)
复制代码
|