4、平衡自行车(shworld)
[mw_shl_code=arduino,true]#MicroPython动手做(35)——小游戏
#平衡自行车
from mpython import *
#自行车BMP图像转字节数据参考
#https://mpython.readthedocs.io/z ... asics/oled.html#id4
bmp = bytearray([\
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X7F,0X00,0X0F,0XF8,0X00,0X00,
0X00,0X00,0X18,0X00,0X0C,0X0C,0X00,0X00,0X00,0X00,0X08,0X00,0X04,0X04,0X00,0X00,
0X00,0X00,0X04,0X00,0X02,0X38,0X00,0X00,0X00,0X00,0X07,0XFF,0XFF,0X00,0X00,0X00,
0X00,0X00,0X07,0X00,0X07,0X00,0X00,0X00,0X00,0X00,0X09,0X00,0X04,0X80,0X00,0X00,
0X00,0X00,0X11,0X80,0X0C,0XC0,0X00,0X00,0X00,0XFF,0X30,0X80,0X18,0X7F,0XE0,0X00,
0X03,0X80,0XE0,0XC0,0X30,0X70,0X38,0X00,0X06,0X00,0XF0,0X60,0X21,0XB0,0X06,0X00,
0X08,0X00,0X88,0X20,0X43,0X10,0X03,0X00,0X10,0X01,0X04,0X30,0X86,0X08,0X01,0X00,
0X30,0X03,0X0E,0XFF,0X84,0X0C,0X00,0X80,0X20,0X1F,0XFF,0X31,0X8C,0X04,0X00,0XC0,
0X60,0X32,0X02,0X20,0X8C,0X00,0X00,0XC0,0X60,0X22,0X02,0X24,0XC8,0X00,0X00,0XC0,
0X60,0X1F,0X02,0X3C,0X8C,0X00,0X00,0XC0,0X30,0X0F,0XFF,0XFF,0X04,0X00,0X00,0X80,
0X10,0X00,0X04,0X18,0X04,0X00,0X01,0X80,0X18,0X00,0X0C,0X08,0X02,0X00,0X03,0X00,
0X0C,0X00,0X18,0X00,0X01,0X00,0X06,0X00,0X07,0X00,0X70,0X00,0X00,0XC0,0X0C,0X00,
0X01,0XFF,0XC0,0X00,0X00,0X3F,0XF0,0X00,0X00,0X3E,0X00,0X00,0X00,0X0F,0XC0,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
])
#无限循环
while True:
bikeWidth=60 #自行车图片宽度
bikeHeight=30 #自行车图片高度
bikeInitX=34 #初始化自行车的X中央点,屏幕宽度128/2-补偿自行车宽度30=34
bikeInitY=35 #初始化自行车的Y点,手动测量调为35即可
y=accelerometer.get_y() #如官方说明图所示,y轴和x轴是相反的,所以这里获取的是y轴加速度
if y<=1 and y>=-1: #定界,其实不定也行。用于下面设定变量转换
offsetX=int(numberMap(y,1,-1,-60,128)) #-60=最小宽度0-自行车宽度,128为屏幕最大宽度
#offsetX主要把y轴的加速度转换成屏幕的宽度分辨率
#numberMap变量转换说明
#numberMap(输入值,需映射的最小值,需映射的最大值,映射出的最小值,映射出的最大值)
#比如numberMap(i,1,2,10000,20000)
#即i是变量,范围在1~2之间,通过numberMap函数你可以得到转换后的范围,在10000-2000之间
#如i是1.5则返回15000,i是1.8返回18000,以此类推
move_x=offsetX #移动的坐标
#清除
oled.fill(0)
#填充图形
oled.Bitmap(move_x,bikeInitY, bmp, bikeWidth , bikeHeight, 1)
#填充左侧钉子,手绘坐标
oled.line(0,64,2,58,1)
oled.line(2,58,4,64,1)
#填充右侧钉子,手绘坐标
oled.line(127,64,125,58,1)
oled.line(125,58,123,64,1)
#判断是否碰到左右钉子边界,并填充文字提示
if move_x+bikeWidth >=125:
oled.DispChar('老铁,扎前胎了!', 60-14, 0)
if move_x+bikeWidth<=58:
oled.DispChar('老铁,扎后胎了!',0,0)
#显示图形文字
oled.show()[/mw_shl_code]
|