rzyzzxw 发表于 2018-10-3 10:37:02

【掌控】mpython-7…猜拳猜拳,石头…剪刀…布

本帖最后由 rzyzzxw 于 2018-10-4 18:13 编辑

掌控发布会有一个小视频,掌控晃一晃,变图案。
http://v.youku.com/v_show/id_XMzg0NzU4MTUzMg==.html?spm=a2h3j.8428770.3416059.1

哈哈,这个用随机数和加速度传感器的小项目,可以改成好多好玩的项目,比如今天大圣老师要做的石头…剪刀…布。
大圣老师已经做过多个版本的石头…剪刀…布,掌控也要做一个。
做过之后,发现掌控的运行速度真是快,加速度传感器真灵敏。

视频镇楼:
http://v.youku.com/v_show/id_XMzg0NzU5MTQ4OA==.html?spm=a2h3j.8428770.3416059.1

哈哈,不是石头…剪刀…布,上当了,没有,有了这个字体小小的数码骰子,再加上图片取模工具,一起玩起来。

首先感谢陈工哈,昨天晚上给的代码提示:
#abs() 函数返回数字的绝对值。
今天一测试,果然好用。

【1】数码骰子来一波

代码中有注释,不啰嗦,直接上代码:

from mpython import * #此库为掌控板内置传感器驱动
import random#随机数产生库
import time #导入时间库

while True:
    x1 = accelerometer.get_x()
    y1 = accelerometer.get_y()
    z1 = accelerometer.get_z()
    time.sleep_ms(100)
    x2 = accelerometer.get_x()
    y2 = accelerometer.get_y()
    z2 = accelerometer.get_z()
    if abs(x1 - x2) > 0.5 or abs(y1 - y2) > 0.5 or abs(z1 - z2) > 0.5:
      #任意一轴的分时差值超过0.5,则认为检测到摇动
      display.fill(0) #清屏,以清掉以前显示数据
      display.show()#刷新
      num = random.randint(1,6)#随机产生1-6的数
      display.DispChar(str(num),20,25,1) #产生的随机数送显示缓存
      display.show() #数据送ODED显示
    time.sleep_ms(100)

【2】猜拳猜拳,石头…剪刀…布
1、准备三张小图片(百度一下),处理好(前面有教程的)。

2、用用取模软件Img2Lcd工具取得有用的代码。


同一方法得到三个图片的数据。

3、写程序了。

有了前面几个程序的基础,做起来轻车熟路。

完整代码:from mpython import * #此库为掌控板内置传感器驱动
import random#随机数产生库
import time #导入时间库
import framebuf

#图片bitmap数组
bmp1 = bytearray([\
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0XF8,0X00,0X00,0X00,
0X00,0X00,0X00,0X03,0XFE,0X00,0X00,0X00,0X00,0X00,0X00,0X07,0X9F,0X00,0X00,0X00,
0X00,0X00,0X00,0X07,0X07,0X80,0X00,0X00,0X00,0X00,0X00,0X0E,0X03,0X80,0X00,0X00,
0X00,0X00,0X00,0X0E,0X03,0XC0,0X00,0X00,0X00,0X00,0X00,0X0C,0X01,0XE0,0X00,0X00,
0X00,0X00,0X00,0X1C,0X00,0XE0,0X00,0X00,0X00,0X00,0X00,0X1C,0X00,0XF0,0X00,0X00,
0X00,0X00,0X00,0X1C,0X00,0X70,0X00,0X00,0X00,0X00,0X00,0X1C,0X00,0X70,0X00,0X00,
0X00,0X00,0X00,0X1C,0X00,0X38,0X00,0X00,0X00,0X00,0X00,0X1C,0X00,0X38,0X00,0X00,
0X00,0X00,0X00,0X1C,0X00,0X38,0X00,0X00,0X00,0X3F,0XC0,0X1C,0X00,0X18,0X00,0X00,
0X00,0X7F,0XF0,0X0C,0X00,0X1C,0X00,0X00,0X00,0XF0,0XFC,0X0E,0X00,0X1C,0X00,0X00,
0X00,0XE0,0X3F,0X0E,0X00,0X1C,0X00,0X00,0X01,0XC0,0X0F,0X86,0X00,0X0E,0X00,0X00,
0X01,0XC0,0X03,0XC7,0X00,0X0E,0X00,0X00,0X01,0XC0,0X01,0XF7,0X00,0X0E,0X00,0X00,
0X00,0XC0,0X00,0XFF,0X00,0X0E,0X00,0X00,0X00,0XE0,0X00,0X3F,0X80,0X06,0X00,0X00,
0X00,0XE0,0X00,0X1F,0X80,0X07,0X00,0X00,0X00,0X70,0X00,0X0F,0X80,0X07,0X00,0X00,
0X00,0X30,0X00,0X03,0XC0,0X07,0X00,0X00,0X00,0X38,0X00,0X03,0XE0,0X07,0X00,0X00,
0X00,0X1C,0X00,0X01,0XE0,0X03,0X80,0X00,0X00,0X1E,0X00,0X00,0X70,0X03,0X80,0X00,
0X00,0X0F,0X00,0X00,0X30,0X07,0XF0,0X00,0X00,0X07,0X80,0X00,0X18,0X3F,0XFC,0X00,
0X00,0X03,0XE0,0X00,0X0C,0XE0,0X3E,0X00,0X00,0X01,0XF0,0X00,0X0F,0X80,0X0E,0X00,
0X00,0X00,0X7C,0X00,0X06,0X00,0X07,0X00,0X00,0X00,0X3F,0X00,0X06,0X00,0X03,0X80,
0X00,0X00,0X0F,0XC0,0X0C,0X00,0X01,0XC0,0X00,0X00,0X03,0XF0,0X18,0X00,0X01,0XC0,
0X00,0X00,0X01,0XFC,0X10,0X00,0X00,0XC0,0X00,0X00,0X00,0X3F,0XF0,0X00,0X00,0XE0,
0X00,0X00,0X00,0X1F,0XF0,0X00,0X80,0X60,0X00,0X00,0X00,0X3C,0X20,0X00,0XC0,0X70,
0X00,0X00,0X00,0X78,0X20,0X00,0XC0,0X30,0X00,0X00,0X00,0XF0,0X20,0X00,0X80,0X30,
0X00,0X00,0X00,0XE0,0X20,0X00,0X80,0X30,0X00,0X00,0X01,0XC7,0XE0,0X01,0X00,0X70,
0X00,0X00,0X01,0XC4,0X20,0X01,0X00,0X30,0X00,0X00,0X01,0X80,0X30,0X03,0X00,0X30,
0X00,0X00,0X01,0X80,0X10,0X06,0X00,0X70,0X00,0X00,0X01,0X80,0X18,0X0C,0X00,0X60,
0X00,0X00,0X01,0X80,0X18,0X18,0X00,0X60,0X00,0X00,0X01,0XC0,0X0E,0X70,0X00,0X60,
0X00,0X00,0X01,0XC0,0X07,0XE0,0X00,0XC0,0X00,0X00,0X00,0XE0,0X06,0X60,0X01,0XC0,
0X00,0X00,0X00,0XE0,0X0C,0X00,0X01,0X80,0X00,0X00,0X00,0X70,0X18,0X00,0X03,0X80,
0X00,0X00,0X00,0X3F,0X70,0X00,0X07,0X00,0X00,0X00,0X00,0X0F,0XE0,0X00,0X1E,0X00,
0X00,0X00,0X00,0X03,0XF0,0X03,0XFC,0X00,0X00,0X00,0X00,0X00,0X78,0X03,0XF0,0X00,
0X00,0X00,0X00,0X00,0X3C,0X0F,0X00,0X00,0X00,0X00,0X00,0X00,0X1F,0XFE,0X00,0X00,
0X00,0X00,0X00,0X00,0X0F,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0XC0,0X00,0X00,
])
# 剪刀 该部分代码来自取模工具

fb1 = framebuf.FrameBuffer(bmp1,64,64, framebuf.MONO_HLSB)   #创建64x64大小帧缓存区储存图片帧,水平扫描

bmp2 = 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,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X03,0XF8,0X78,0X00,0X00,0X00,0X00,0X00,0X0F,0X80,0X58,0X00,0X00,0X00,0X00,0X00,
0X01,0X81,0X18,0X00,0X00,0X00,0X00,0X00,0X03,0X70,0XFF,0X00,0X00,0X00,0X00,0X00,
0X07,0XF1,0XFF,0X00,0X00,0X00,0X00,0X00,0X0F,0X11,0X70,0X00,0X00,0X00,0X00,0X00,
0X09,0X70,0XE4,0X00,0X00,0X00,0X00,0X00,0X01,0XE1,0XC6,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X03,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X7F,0XFE,0X00,0X00,
0X00,0X00,0X00,0X7F,0XFF,0XFF,0X80,0X00,0X00,0X00,0X01,0XFF,0XE0,0X07,0XC0,0X00,
0X00,0X00,0X0F,0XE0,0X30,0X00,0XE0,0X00,0X00,0X00,0X1F,0X00,0X0C,0X00,0X70,0X00,
0X00,0X00,0X7C,0X00,0X02,0X00,0X38,0X00,0X00,0X00,0XF0,0X00,0X01,0X80,0X38,0X00,
0X00,0X01,0XE0,0X00,0X00,0XE0,0X1C,0X00,0X00,0X03,0XC0,0X00,0X00,0X60,0X1C,0X00,
0X00,0X03,0X80,0X00,0X00,0X20,0X0C,0X00,0X00,0X07,0X00,0X00,0X00,0X10,0X1C,0X00,
0X00,0X0F,0X00,0X00,0X00,0X10,0X18,0X00,0X00,0X0E,0X00,0X00,0X00,0X08,0X1C,0X00,
0X00,0X0E,0X00,0X00,0X00,0X08,0X3C,0X00,0X00,0X1C,0X00,0X10,0X00,0X08,0X7F,0X00,
0X00,0X1C,0X00,0X10,0X00,0X04,0XC7,0X80,0X00,0X1C,0X00,0X00,0X00,0X05,0X03,0X80,
0X00,0X1C,0X00,0X08,0X00,0X06,0X01,0XC0,0X00,0X18,0X00,0X04,0X00,0X08,0X01,0XC0,
0X00,0X18,0X00,0X06,0X00,0X08,0X00,0XE0,0X00,0X1C,0X00,0X03,0X00,0X00,0X00,0XE0,
0X00,0X1C,0X00,0X02,0X80,0X10,0X00,0XE0,0X00,0X1C,0X00,0X00,0X60,0X60,0X00,0XE0,
0X00,0X0C,0X00,0X00,0X3D,0XC0,0X01,0XC0,0X00,0X0E,0X00,0X00,0X07,0X00,0X01,0XC0,
0X00,0X0E,0X00,0X00,0X10,0X00,0X03,0X80,0X00,0X07,0X00,0X00,0X08,0X00,0X07,0X80,
0X00,0X03,0X80,0X00,0X18,0X00,0X0F,0X00,0X00,0X01,0XE0,0X00,0X26,0X00,0X3E,0X00,
0X00,0X00,0XFC,0X00,0X41,0X11,0XB8,0X00,0X00,0X00,0X78,0X00,0X00,0X1C,0X38,0X00,
0X00,0X00,0X1C,0X00,0X00,0X00,0X3C,0X00,0X00,0X00,0X1C,0X00,0X40,0X00,0X1C,0X00,
0X00,0X00,0X0E,0X00,0X40,0X00,0X38,0X00,0X00,0X00,0X07,0X00,0X20,0X00,0X38,0X00,
0X00,0X00,0X03,0XE0,0XF0,0X00,0X70,0X00,0X00,0X00,0X01,0XFF,0XFC,0X00,0XF0,0X00,
0X00,0X00,0X00,0XFF,0XFF,0X03,0XE0,0X00,0X00,0X00,0X00,0X00,0X0F,0XFF,0X80,0X00,
0X00,0X00,0X00,0X00,0X03,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFC,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
])
# 石头 该部分代码来自取模工具

fb2 = framebuf.FrameBuffer(bmp2,64,64, framebuf.MONO_HLSB)   #创建64x64大小帧缓存区储存图片帧,水平扫描

bmp3 = 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,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X01,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X07,0XF0,0X00,0X38,0X00,0X00,
0X00,0X00,0X0F,0XF8,0X01,0XFE,0X00,0X00,0X00,0X00,0X1F,0XFC,0X07,0XFF,0X00,0X00,
0X00,0X00,0X1E,0X3C,0X0F,0XFF,0X80,0X00,0X00,0X00,0X78,0X1C,0X1F,0X03,0XC0,0X00,
0X00,0X00,0X70,0X1C,0X3E,0X01,0XC0,0X00,0X00,0X00,0XF0,0X1C,0X7C,0X00,0XE0,0X00,
0X00,0X00,0XE0,0X1C,0XF0,0X00,0XE0,0X00,0X00,0X00,0XC0,0X1C,0XF0,0X00,0XE0,0X00,
0X00,0X00,0XC0,0X1F,0XE0,0X00,0XE0,0X00,0X00,0X01,0XC0,0X1F,0XC0,0X01,0XC0,0X00,
0X00,0X01,0XC0,0X1F,0X80,0X01,0XC0,0X00,0X00,0X01,0XC0,0X1F,0X00,0X01,0XC0,0X00,
0X00,0X01,0XC0,0X1F,0X00,0X03,0XC0,0X00,0X00,0X01,0XC0,0X1E,0X00,0X03,0X80,0X00,
0X00,0X01,0XC0,0X3C,0X00,0X07,0X06,0X00,0X00,0X01,0XC0,0X3C,0X00,0X0E,0X7F,0XC0,
0X00,0X01,0XC0,0X3C,0X00,0X0F,0XFF,0XE0,0X00,0X01,0XC0,0X38,0X00,0X1F,0XFD,0XE0,
0X00,0X01,0XC0,0X38,0X00,0X1F,0XC0,0XF0,0X00,0X01,0XC0,0X70,0X00,0X7F,0X00,0X70,
0X00,0X01,0XC0,0X70,0X00,0X7C,0X00,0X70,0X00,0X01,0XC3,0XF0,0X00,0XF8,0X00,0X70,
0X00,0X03,0XC0,0X70,0X01,0XE0,0X00,0X70,0X00,0X03,0X80,0X1C,0X03,0XC0,0X00,0XE0,
0X00,0X07,0X00,0X0F,0X07,0X80,0X01,0XE0,0X00,0X0F,0X00,0X03,0X87,0X00,0X01,0XC0,
0X00,0X0E,0X00,0X00,0XFE,0X00,0X03,0X80,0X00,0X1C,0X00,0X00,0X7C,0X00,0X07,0X80,
0X00,0X1C,0X00,0X00,0X18,0X00,0X0F,0X00,0X00,0X18,0X00,0X00,0X08,0X00,0X1E,0X00,
0X00,0X18,0X00,0X00,0X06,0X00,0X3C,0X00,0X00,0X10,0X00,0X00,0XC3,0X00,0XF8,0X00,
0X00,0X10,0X00,0X07,0XF8,0X00,0XF0,0X00,0X00,0X10,0X00,0X1E,0X1E,0X03,0XC0,0X00,
0X00,0X18,0X00,0X30,0X07,0X0F,0XC0,0X00,0X00,0X18,0X00,0X00,0X01,0X9F,0X80,0X00,
0X00,0X18,0X00,0X00,0X00,0XFF,0XC0,0X00,0X00,0X18,0X00,0X00,0X00,0X7F,0XF0,0X00,
0X00,0X18,0X00,0X00,0X00,0X70,0XF8,0X00,0X00,0X1C,0X00,0X00,0X00,0X00,0X3C,0X00,
0X00,0X1C,0X00,0X00,0X00,0X00,0X1C,0X00,0X00,0X0E,0X00,0X00,0X00,0X00,0X0E,0X00,
0X00,0X0F,0X00,0X00,0X00,0X00,0X0E,0X00,0X00,0X07,0X00,0X00,0X00,0X00,0X0E,0X00,
0X00,0X03,0XC0,0X00,0X00,0X00,0X07,0X00,0X00,0X01,0XC0,0X00,0X00,0X00,0X07,0X00,
0X00,0X00,0XF0,0X00,0X00,0X00,0X06,0X00,0X00,0X00,0X38,0X00,0X07,0XF8,0X06,0X00,
0X00,0X00,0X38,0X00,0X0F,0XFC,0X0E,0X00,0X00,0X00,0X1E,0X00,0X7F,0X3F,0X0E,0X00,
0X00,0X00,0X0F,0X01,0XFC,0X0F,0X9C,0X00,0X00,0X00,0X07,0XFF,0XF0,0X03,0XF8,0X00,
0X00,0X00,0X03,0XFF,0XC0,0X03,0XF8,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
])
# 布 该部分代码来自取模工具

fb3 = framebuf.FrameBuffer(bmp3,64,64, framebuf.MONO_HLSB)   #创建64x64大小帧缓存区储存图片帧,水平扫描

while True:
    x1 = accelerometer.get_x()
    y1 = accelerometer.get_y()
    z1 = accelerometer.get_z()
    time.sleep_ms(100)
    x2 = accelerometer.get_x()
    y2 = accelerometer.get_y()
    z2 = accelerometer.get_z()
    if abs(x1 - x2) > 0.5 or abs(y1 - y2) > 0.5 or abs(z1 - z2) > 0.5:
      #任意一轴的分时差值超过0.5,则认为检测到摇动
      display.fill(0) #清屏,以清掉以前显示数据
      display.show()#刷新
      num = random.randint(1,3)#随机产生1-3的数
      #display.DispChar(str(num),20,25,1) #产生的随机数送显示缓存
      #display.show() #数据送ODED显示
      if num == 1:
            display.blit(fb1,32,0)#将fb1帧缓存送至oled显示,起始位(32,0)
            display.show()         #刷新显示屏
      if num == 2:
            display.blit(fb2,32,0)#将fb2帧缓存送至oled显示,起始位(32,0)
            display.show()         #刷新显示屏
      if num == 3:
            display.blit(fb3,32,0)#将fb3帧缓存送至oled显示,起始位(32,0)
            display.show()         #刷新显示屏      
    time.sleep_ms(100)

终于可以上石头剪刀布的视频了。http://v.youku.com/v_show/id_XMzg0NzY1MjEzMg==.html?spm=a2h3j.8428770.3416059.1



rzegkly 发表于 2018-10-3 14:18:14

掌控板的编写流程有了一个新的认识

hnyzcj 发表于 2018-10-3 15:45:28

朱老师你太快了

俗人 发表于 2018-10-4 15:19:31

厉害厉害666

rzyzzxw 发表于 2018-12-28 21:37:28

# 数码骰子
from mpython import *
import random# 随机数产生库
import time # 导入时间库

while True:
    x1 = accelerometer.get_x()
    y1 = accelerometer.get_y()
    z1 = accelerometer.get_z()
    time.sleep_ms(100)
    x2 = accelerometer.get_x()
    y2 = accelerometer.get_y()
    z2 = accelerometer.get_z()
    if abs(x1 - x2) > 0.5 or abs(y1 - y2) > 0.5 or abs(z1 - z2) > 0.5:
      #任意一轴的分时差值超过0.5,则认为检测到摇动
      oled.fill(0) # 清屏,以清掉以前显示数据
      num = random.randint(1,6)# 随机产生1-6的数
      oled.DispChar(str(num),20,25,1) # 产生的随机数送显示缓存
      oled.show() # 数据送ODED显示
    time.sleep_ms(100)
页: [1]
查看完整版本: 【掌控】mpython-7…猜拳猜拳,石头…剪刀…布