燃烧的火焰
将学习的知识点:
图像在microbit屏幕上显示之前,是什么样的代码
乘法运算符怎么处理客串、数字、图像
怎么检测microbit 发生摇晃
怎么选择随机数
怎么让microbit上的图像移动和变淡
所需工具:
硬件:
microbit主板
数据线
软件:
Mu 或 UpyCraft-Micropython 文档:https://dfrobot.gitbooks.io/upycraft_cn/
from microbit import *
import random
# create an empty image
i = Image("00000:"*5)
# start with the fire at medium intensity
intensity = 0.5
# keep looping
while True:
# show the image and wait
display.show(i)
sleep(100)
# shake the micro:bit to stoke the fire
if accelerometer.was_gesture("shake"):
intensity = 1
# shift the image up and fade it slightly
i = i.shift_up(1) * intensity
# let the fire burn down a little (reduce the intensity)
intensity *= 0.98
# choose random brightness for bottom row of fire
for x in range(5):
i.set_pixel(x, 4, random.randint(0,9))
第1行和第2行:导入需要的包
第4行到第7行:初始化图像,全为0表示没有图像。i = Image("00000:"*5)与i =Image("00000:00000:00000:00000:00000")效果一样,全为0表示没有图像,1-9表示有图像,1的颜色最浅
第8行:设置初始化的摇晃强度
第11行:循环开始,从第12行到28行会重复运行
第13行:将图片显示出来
第14行:睡眠0.1秒
第17行和第18行:判断语句,利用microbit的加速传感器检测是否发生摇晃,如果摇晃,将震动强度置1
第21行:将图像向上平移一个单位,并且亮度变为之前的0.98倍
第27行和第28行:一个for循环,循环5次,每一次循环,第4行第x个LED的亮度会随机变化。
根据以上步骤,将代码运行起来,显示的图像就像火焰在燃烧 ^-^

浙公网安备 33010602011771号