8、模拟计算器(shworld)
通过按键A向左移动光标,按键B向右移动光标
金手指Y键确认选择
支持多元运算,最长显示结果限定在16位左右,防止超出屏幕宽度
如果运算错误会输出显示,比如被除数为0或者其他错误乱输入
[mw_shl_code=arduino,true]#MicroPython动手做(35)——小游戏
#模拟计算器
from mpython import *
#图片bitmap数组
bmp = bytearray([\
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0X00,
0X80,0X0C,0X00,0X60,0X03,0X00,0X18,0X00,0XC0,0X06,0X00,0X30,0X01,0X80,0X08,0X00,
0X80,0X0C,0X00,0X60,0X03,0X00,0X18,0X00,0XC0,0X06,0X3E,0X30,0X61,0X83,0X08,0X00,
0X82,0X0C,0X3C,0X61,0XE3,0X02,0X18,0X40,0XC1,0X06,0X02,0X30,0XB1,0X84,0X88,0X00,
0X86,0X0C,0X44,0X61,0X23,0X06,0X18,0X40,0XC3,0X06,0X02,0X31,0X11,0X84,0X48,0X00,
0X82,0X0C,0X04,0X60,0X23,0X06,0X18,0X70,0XC2,0X06,0X04,0X30,0XA1,0X84,0XC8,0X00,
0X82,0X0C,0X08,0X60,0XC3,0X0A,0X18,0X08,0XC7,0XC6,0X04,0X30,0XE1,0X87,0X88,0X00,
0X82,0X0C,0X08,0X60,0X23,0X12,0X18,0X08,0XC4,0X46,0X08,0X31,0X91,0X81,0X88,0X00,
0X82,0X0C,0X10,0X60,0X23,0X1F,0X18,0X88,0XC4,0X46,0X08,0X31,0X11,0X81,0X08,0X00,
0X82,0X0C,0X20,0X61,0X63,0X02,0X18,0X58,0XC6,0XC6,0X08,0X30,0XB1,0X83,0X08,0X00,
0X82,0X0C,0X3C,0X60,0X83,0X00,0X18,0X20,0XC3,0X86,0X00,0X30,0X41,0X80,0X08,0X00,
0X80,0X0C,0X00,0X60,0X03,0X00,0X18,0X00,0XC0,0X06,0X00,0X30,0X01,0X80,0X08,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XE0,0X00,0X00,0X00,0X00,
0X80,0X0C,0X00,0X60,0X03,0X00,0X18,0X00,0XC0,0X06,0X00,0X20,0X00,0X00,0X00,0X00,
0X82,0X0C,0X00,0X60,0X03,0X00,0X18,0X00,0XC0,0X06,0X1C,0X20,0X00,0X00,0X00,0X00,
0X85,0X0C,0X00,0X60,0X03,0X10,0X58,0X08,0XC0,0X06,0X34,0X20,0X00,0X00,0X00,0X00,
0X88,0X8C,0X10,0X60,0X03,0X08,0X98,0X10,0XDF,0XF6,0X22,0X20,0X00,0X00,0X00,0X00,
0X88,0X8C,0X10,0X60,0X03,0X01,0X18,0X10,0XC0,0X06,0X20,0X20,0X00,0X00,0X00,0X00,
0X88,0X8C,0X7C,0X61,0XE3,0X06,0X18,0X20,0XC0,0X06,0X20,0X20,0X00,0X00,0X00,0X00,
0X88,0X8C,0X10,0X60,0X03,0X06,0X18,0X00,0XC0,0X06,0X22,0X20,0X00,0X00,0X00,0X00,
0X8C,0X8C,0X10,0X60,0X03,0X09,0X18,0X40,0XDF,0XF6,0X26,0X20,0X00,0X00,0X00,0X00,
0X87,0X0C,0X00,0X60,0X03,0X10,0X98,0X00,0XC0,0X06,0X1C,0X20,0X00,0X00,0X00,0X00,
0X80,0X0C,0X00,0X60,0X03,0X00,0X18,0X80,0XC0,0X06,0X00,0X20,0X00,0X00,0X00,0X00,
0X80,0X0C,0X00,0X60,0X03,0X00,0X18,0X00,0XC0,0X06,0X00,0X20,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XE0,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,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,
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,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,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,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,
0X00,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X60,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,
0X00,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,
0X00,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,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,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,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,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,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,
])
#[建议]
#这一块由于数据太大写入太慢影响调试速度
#如果技术上跟的上的话,可以把背景事先截图,然后删掉代码,这样写入很快,然后盲按按钮调试
#主要逻辑是判断坐标来取值,把值拼接起来形成字符串,最后通过eval()函数运算
#实现方法很多种,自己的代码3个月后也不一定看懂,最好的教程是自己实现和学习
#by阿唯
#-------------------变量定义-------------------
#矩形光标的宽高
RECT_CURSOR=[13,12]
#第一行各键序XY坐标,Y轴固定0
#偏移量13为矩形光标的宽度
KEY_ONE=[0,0]
KEY_TWO=[13,0]
KEY_THREE=[26,0]
KEY_FOUR=[39,0]
KEY_FIVE=[52,0]
KEY_SIX=[65,0]
KEY_SEVEN=[78,0]
KEY_EIGHT=[91,0]
KEY_NINE=[104,0]
#第二行各键序XY坐标,Y轴固定13
KEY_ZERO=[0,13]
KEY_ADD=[13,13]
KEY_MINUS=[26,13]
KEY_MULTI=[39,13]
KEY_DIVIDE=[52,13]
KEY_EQUAL=[65,13]
KEY_CLEAR=[78,13]
#定义键序坐标列表,用于按钮左右移动,通过列表索引的增减来移动
KEY_LIST=[KEY_ONE,KEY_TWO,KEY_THREE,KEY_FOUR,KEY_FIVE,KEY_SIX,
KEY_SEVEN,KEY_EIGHT,KEY_NINE,KEY_ZERO,KEY_ADD,KEY_MINUS,
KEY_MULTI,KEY_DIVIDE,KEY_EQUAL,KEY_CLEAR]
#定义键序值列表,用于取值运算和显示
KEY_LIST_VAL=["1","2","3","4","5","6","7","8","9","0","+","-","*","/","=","c"]
#当前键序0,即引用KEY_LIST[0],也就是默认在1的位置
KEY_INDEX=0
#数值内容,通过eval()对字符串表达式直接运算
RESULT_CALC=""
#-------------------函数方法-------------------
#移动光标,例moveCursor("left")
def moveCursor(direction=None):
oled.fill(0)
oled.bitmap(0, 0, bmp, 128, 64, 1)
oled.DispChar("输出:"+RESULT_CALC,10,37)
global KEY_INDEX #如果要对全局变量修改的话需要引用
if(direction=="left"): #如果是向左移动,则KEY_LIST索引-1
if KEY_INDEX>0: #只有索引>0才可以进行递减,防止越界
KEY_INDEX=KEY_INDEX-1
if(direction=="right"): #同理
if KEY_INDEX<15:
KEY_INDEX=KEY_INDEX+1
_x=KEY_LIST[KEY_INDEX][0] #通过KEY_LIST的索引来获取矩形光标的填充位置
_y=KEY_LIST[KEY_INDEX][1]
RECT_CURSOR_WIDTH=RECT_CURSOR[0] #获取矩形光标的宽高
RECT_CURSOR_HEIGHT=RECT_CURSOR[1]
oled.fill_rect(_x, _y, RECT_CURSOR_WIDTH, RECT_CURSOR_HEIGHT, 1) #填充画面
oled.show()
#-------------------程序入口-------------------
#第一次方向参数direction不传,用于渲染一次主画面
moveCursor()
#-------------------按钮监听-------------------
#按钮a触发事件
def on_button_a_down(_):
if button_a.value() == 1: return
moveCursor("left")
#按钮b触发事件
def on_button_b_down(_):
if button_b.value() == 1: return
moveCursor("right")
#按钮监听回调函数
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
#金手指触摸监听
while True:
time.sleep_ms(50) #延迟调高,按钮迟钝,反之延迟越低越容易误触
if(touchPad_Y.read() < 100):
keyVal=KEY_LIST_VAL[KEY_INDEX] #通过全局索引获取键值列表的值
if(KEY_INDEX<=9): #如果索引<=9,说明按的是前面10个数字,直接累加就行
RESULT_CALC=RESULT_CALC+keyVal
if(keyVal=="+"): #如果按的是+好,累加就可以,下面同理
RESULT_CALC=RESULT_CALC+"+"
if(keyVal=="-"):
RESULT_CALC=RESULT_CALC+"-"
if(keyVal=="*"):
RESULT_CALC=RESULT_CALC+"*"
if(keyVal=="/"):
RESULT_CALC=RESULT_CALC+"/"
if(keyVal=="="): #当按了等号后,会获得一个累加的字符串变量RESULT_CALC
try: #RESULT_CALC比如为"1+2*3",这里加try屏蔽运算错误
RESULT_CALC=str(eval(RESULT_CALC)) #核心函数eval()进行字符串运算,运算不成立会报错
RESULT_CALC=RESULT_CALC[:16] #取结果集左边16位字符串,防止超出画面显示
moveCursor() #最后提前调用一次移动函数显示画面
RESULT_CALC="" #置结果集为空,不会生效,下一次移动才会生效
continue #跳出本次循环,不然最后moveCursor()又要重画一次
except: #异常处理
RESULT_CALC="输入错误"
moveCursor()
RESULT_CALC=""
continue
if(keyVal=="c"):
RESULT_CALC=""
moveCursor() #不传参数direction=None(不移动光标),直接渲染主画面[/mw_shl_code]
|