MicroPython动手做(35)——体验小游戏-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: eagler8

MicroPython动手做(35)——体验小游戏

[复制链接]
 楼主| 发表于 2020-6-26 14:49 | 显示全部楼层
mPython X 实验图形编程2

38.jpg
 楼主| 发表于 2020-6-26 15:01 | 显示全部楼层
吃鸡游戏

43.gif
 楼主| 发表于 2020-6-26 15:11 | 显示全部楼层

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]
 楼主| 发表于 2020-6-26 18:43 | 显示全部楼层
9、是男人就下一百层(luyi)

设置Y/O触摸键控制人物左右移动

[mw_shl_code=arduino,true]#MicroPython动手做(35)——小游戏
#是男人就下一百层

from mpython import *

import time

def man(x, y):
    global x1, y1, xman, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8, x9, y9, x10, y10, yman, down
    oled.fill_circle(x, (y - 25), 3, 1)
    oled.fill_rect((x - 5), (y - 21), 10, 10, 1)
    oled.fill_rect((x - 4), (y - 11), 3, 11, 1)
    oled.fill_rect((x + 1), (y - 11), 3, 11, 1)

def dangban():
    global x, y, x1, y1, xman, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8, x9, y9, x10, y10, yman, down
    oled.fill_rect(0, 0, 5, 8, 1)
    oled.fill_rect(0, 8, 5, 8, 1)
    oled.fill_rect(0, 17, 5, 8, 1)
    oled.fill_rect(0, 26, 5, 8, 1)
    oled.fill_rect(0, 35, 5, 8, 1)
    oled.fill_rect(0, 44, 5, 8, 1)
    oled.fill_rect(0, 53, 5, 8, 1)
    oled.fill_rect(0, 62, 5, 8, 1)

from machine import Timer

_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass

tim12 = Timer(12)

def timer12_tick(_):
    global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
    try:
        touchPad_P.read();pass
    except:
        return
    if touchPad_P.read() < 400:
        if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
    elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
    if touchPad_Y.read() < 400:
        if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
    elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
    if touchPad_T.read() < 400:
        if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
    elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
    if touchPad_H.read() < 400:
        if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
    elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
    if touchPad_O.read() < 400:
        if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
    elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
    if touchPad_N.read() < 400:
        if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
    elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)

def on_touchpad_Y_pressed():
    global x, y, x1, y1, xman, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8, x9, y9, x10, y10, yman, down
    xman = xman + -5

def on_touchpad_O_pressed():
    global x, y, x1, y1, xman, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8, x9, y9, x10, y10, yman, down
    xman = xman + 5

def dangban2():
    global x, y, x1, y1, xman, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8, x9, y9, x10, y10, yman, down
    oled.fill_rect(122, 0, 5, 8, 1)
    oled.fill_rect(122, 8, 5, 8, 1)
    oled.fill_rect(122, 17, 5, 8, 1)
    oled.fill_rect(122, 26, 5, 8, 1)
    oled.fill_rect(122, 35, 5, 8, 1)
    oled.fill_rect(122, 44, 5, 8, 1)
    oled.fill_rect(122, 53, 5, 8, 1)
    oled.fill_rect(122, 62, 5, 8, 1)
x1 = 10
y1 = 59
x2 = 70
y2 = 95
x3 = 30
y3 = 129
x4 = 60
y4 = 165
x5 = 90
y5 = 199
x6 = 70
y6 = 235
x7 = 55
y7 = 269
x8 = 40
y8 = 305
x9 = 20
y9 = 339
x10 = 50
y10 = 375
xman = 20
yman = 33
while True:
    down = True
    oled.fill(0)
    dangban()
    dangban2()
    oled.fill_rect(x1, y1, 30, 5, 1)
    oled.fill_rect(x2, y2, 25, 5, 1)
    oled.fill_rect(x3, y3, 20, 5, 1)
    oled.fill_rect(x4, y4, 30, 5, 1)
    oled.fill_rect(x5, y5, 20, 5, 1)
    oled.fill_rect(x6, y6, 25, 5, 1)
    oled.fill_rect(x7, y7, 25, 5, 1)
    oled.fill_rect(x8, y8, 30, 5, 1)
    oled.fill_rect(x9, y9, 20, 5, 1)
    oled.fill_rect(x10, y10, 30, 5, 1)
    man(xman, yman)
    oled.show()
    if xman >= x1 and xman <= x1 + 30 and yman == y1:
        down = False
    if xman >= x2 and xman <= x2 + 25 and yman == y2:
        down = False
    if xman >= x3 and xman <= x3 + 20 and yman == y3:
        down = False
    if xman >= x4 and xman <= x4 + 30 and yman == y4:
        down = False
    if xman >= x5 and xman <= x5 + 20 and yman == y5:
        down = False
    if xman >= x6 and xman <= x6 + 25 and yman == y6:
        down = False
    if xman >= x7 and xman <= x7 + 25 and yman == y7:
        down = False
    if xman >= x8 and xman <= x8 + 30 and yman == y8:
        down = False
    if xman >= x9 and xman <= x9 + 20 and yman == y9:
        down = False
    if xman >= x10 and xman <= x10 + 30 and yman == y10:
        down = False
    if down:
        yman = yman + 1
    else:
        yman = yman + -1
    y1 = y1 + -1
    y2 = y2 + -1
    y3 = y3 + -1
    y4 = y4 + -1
    y5 = y5 + -1
    y6 = y6 + -1
    y7 = y7 + -1
    y8 = y8 + -1
    y9 = y9 + -1
    y10 = y10 + -1
    time.sleep_ms(50)[/mw_shl_code]

 楼主| 发表于 2020-6-26 18:50 | 显示全部楼层
mPython  实验图形编程


01.jpg
 楼主| 发表于 2020-6-26 18:52 | 显示全部楼层
mPython  实验图形编程2


02.jpg
 楼主| 发表于 2020-6-26 18:54 | 显示全部楼层
mPython  实验图形编程3


03.jpg
 楼主| 发表于 2020-6-26 19:04 | 显示全部楼层
是男人就下一百层

04.jpg
 楼主| 发表于 2020-6-26 19:12 | 显示全部楼层
10、秒表计时器
秒表主要是用来统计秒数的。它有一般有两个按键。A键和B键。
第一次按下A键时,开始计时。
第二次按下A键时,暂停计时。
再按下A键时,继续上一步暂停的计时。
按下B键,归零。

[mw_shl_code=arduino,true]#MicroPython动手做(35)——小游戏
#秒表计时器

from mpython import *
import time
import framebuf
import font.digiface_44
from machine import Timer

def on_button_b_down(_):
    global AKey, timeCount
    time.sleep_ms(10)
    if button_b.value() == 1: return
    timeCount = 0
    oled.fill(0)
    display_font(font.digiface_44, "00:00", 2, 10, False, 2)
    oled.show()
    tim1.deinit()
    AKey = 1

def timer1_tick(_):
    global AKey, timeCount
    # 大于等于60,表示已经计时1小时,超出设计范围,退出计时,并恢复初始
    if timeCount // 60 >= 60:
        oled.fill(0)
        display_font(font.digiface_44, "00:00", 0, 0, False, 2)
        oled.show()
        tim1.deinit()
    else:
        timeCount = timeCount + 1
        # 分的部分,如果是一位数(0-9),则在前面补0
        if timeCount // 60 < 10:
            # 秒的部分,如果是一位数(0-9),则在前面补0
            if timeCount % 60 < 10:
                oled.fill(0)
                display_font(font.digiface_44, (''.join([str(x) for x in ["0", timeCount // 60, ":", "0", timeCount % 60]])), 2, 10, False, 2)
                oled.show()
            else:
                oled.fill(0)
                display_font(font.digiface_44, (''.join([str(x) for x in ["0", timeCount // 60, ":", timeCount % 60]])), 2, 10, False, 2)
                oled.show()
        else:
            if timeCount % 60 < 10:
                oled.fill(0)
                display_font(font.digiface_44, (''.join([str(x) for x in [timeCount // 60, ":", "0", timeCount % 60]])), 2, 10, False, 2)
                oled.show()
            else:
                oled.fill(0)
                display_font(font.digiface_44, (''.join([str(x) for x in [timeCount // 60, ":", timeCount % 60]])), 2, 10, False, 2)
                oled.show()

tim1 = Timer(1)

def on_button_a_down(_):
    global AKey, timeCount
    time.sleep_ms(10)
    if button_a.value() == 1: return
    if AKey == 1:
        AKey = 0
        tim1.init(period=1000, mode=Timer.PERIODIC, callback=timer1_tick)
    else:
        AKey = 1
        tim1.deinit()

def display_font(_font, _str, _x, _y, _wrap, _z=0):
    _start = _x
    for _c in _str:
        _d = _font.get_ch(_c)
        if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
        if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
        oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
        framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
        _x += _d[2]

button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)


oled.fill(0)
oled.DispChar("秒表计时器", 34, 26, 1)
oled.show()
time.sleep(3)
oled.fill(0)
display_font(font.digiface_44, "00:00", 2, 10, False, 2)
oled.show()
timeCount = 0
# A中,1表示初始状态,接下来要计数,当它变成0时,表示接下来需要暂停
AKey = 1[/mw_shl_code]
 楼主| 发表于 2020-6-26 19:15 | 显示全部楼层
mPython X 实验图形编程

01.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-28 01:32 , Processed in 0.081749 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表