2018-11-4 11:55:53 [显示全部楼层]
3469浏览
查看: 3469|回复: 0

[入门教程] 【掌控】mpython-21…这是一个有”水平“的掌控

[复制链接]
一张图片,记录了地质年代的水平面。
【掌控】mpython-21…这是一个有”水平“的掌控图2

画了圈圈之后,大圣老师提了一个问题,这么有水平的掌控,能不能做个水平仪出来。这是个没有水平的问题。
深入一想,前面我从玫瑰曲线探索来的画圆方法不是实时画圆,不能用啊。
可是画方我会啊。
先用方形做个水平仪吧。

【小目标】
水平仪
学习加速度传感器使用及相关算法。
练习自定义函数等知识。

【画个实心方形】
实心方形,中心点在64,32。
因为方形的起点是在左上角点,所以,用方形中心点要进行些坐标换算。

【掌控】mpython-21…这是一个有”水平“的掌控图1
[mw_shl_code=python,true]from mpython import *

display.fill(0)

display.fill_rect(50, 20, 24, 24, 1)
display.show()[/mw_shl_code]


【水平仪:仔细看,有一个小点点】

[mw_shl_code=python,true]from mpython import *

rectx = 0
recty = 0
while True:
    display.fill(0)
    display.rect(56,24,17,16,1)
    display.rect(61,28,7,7,1)
    x = accelerometer.get_y()
    y = accelerometer.get_x()
    rectx = 128-int(x*64+64)
    recty = int(y*32+32)
    display.pixel(rectx, recty, 1)
    display.show()[/mw_shl_code]
【小点点太太太小,换个小方形】


这个代码是在上面基础上改了一下下。
变小点为方形,并转换了坐标位置。

[mw_shl_code=python,true]from mpython import *

rectx = 0
recty = 0
while True:
    display.fill(0)
    display.rect(56,24,17,16,1)
    display.rect(61,28,7,7,1)
    x = accelerometer.get_y()
    y = accelerometer.get_x()
    rectx = 128-int(x*64+64)
    recty = int(y*32+32)
    display.fill_rect(rectx-3, recty-3, 5, 5, 1)
    display.show()[/mw_shl_code]
【换成圆圈圈】


谢谢李时念老师的画圆圈代码。

[mw_shl_code=python,true]#  水平仪 画圆代码作者李时念老师
from mpython import *
import math

def drawcircle(x,y,r,color,full = 0) :
    if(full == 0) :
        for i in range(x-r,x+r+1):
            display.pixel(i,int(y-math.sqrt(r*r-(x-i)*(x-i))),color)
            display.pixel(i,int(y+math.sqrt(r*r-(x-i)*(x-i))),color)
        for i in range(y-r,y+r+1):
            display.pixel(int(x-math.sqrt(r*r-(y-i)*(y-i))),i,color)
            display.pixel(int(x+math.sqrt(r*r-(y-i)*(y-i))),i,color)
    else :
        for i in range(x-r,x+r+1):
            a = int(math.sqrt(r*r-(x-i)*(x-i)))
            display.vline(i, y - a, a * 2,color)
        for i in range(y-r,y+r+1):
            a = int(math.sqrt(r*r-(y-i)*(y-i)))
            display.hline(x - a, i, a * 2, color)


while True:
    display.fill(0)
    drawcircle(64, 32, 16, 1, 0)
    drawcircle(64, 32, 10, 1, 0)
    x = accelerometer.get_y()
    y = accelerometer.get_x()
    rectx = 128-int(x*64+64)
    recty = int(y*32+32)   
    # drawcircle(64, 32, 32, 1, 0)
    # drawcircle(64, 32, 24, 1, 1)
    # drawcircle(64, 32, 16, 0, 0)
    drawcircle(rectx, recty, 8, 1, 1)
    display.show()[/mw_shl_code]

【拓展一下】
1、当达到水平时,亮绿灯。
2、自己来个更好的创意。

[mw_shl_code=python,true]#  水平仪 画圆代码作者李时念老师
from mpython import *
import math

def drawcircle(x, y, r, color, full = 0) :
    if(full == 0):
        for i in range(x - r, x + r + 1):
            display.pixel(i, int(y-math.sqrt(r*r-(x-i)*(x-i))),color)
            display.pixel(i, int(y+math.sqrt(r*r-(x-i)*(x-i))),color)
        for i in range(y-r,y+r+1):
            display.pixel(int(x-math.sqrt(r*r-(y-i)*(y-i))),i,color)
            display.pixel(int(x+math.sqrt(r*r-(y-i)*(y-i))),i,color)
    else :
        for i in range(x-r,x+r+1):
            a = int(math.sqrt(r*r-(x-i)*(x-i)))
            display.vline(i, y - a, a * 2,color)
        for i in range(y-r,y+r+1):
            a = int(math.sqrt(r*r-(y-i)*(y-i)))
            display.hline(x - a, i, a * 2, color)


while True:
    display.fill(0)
    drawcircle(64, 32, 16, 1, 0)
    drawcircle(64, 32, 10, 1, 0)
    x = accelerometer.get_y()
    y = accelerometer.get_x()
    rectx = 128-int(x*64+64)
    recty = int(y*32+32)   
    # drawcircle(64, 32, 32, 1, 0)
    # drawcircle(64, 32, 24, 1, 1)
    # drawcircle(64, 32, 16, 0, 0)
    drawcircle(rectx, recty, 8, 1, 1)
    display.show()
    if rectx == 64 and recty == 32:
        rgb.fill((0, 60, 0))
        rgb.write()
    else:
        rgb.fill((0, 0, 0))
        rgb.write()[/mw_shl_code]
我家小朋友说,气泡是向上走的,所以代码改下。

[mw_shl_code=python,true]#  水平仪 画圆代码作者李时念老师
from mpython import *
import math

def drawcircle(x, y, r, color, full = 0) :
    if(full == 0):
        for i in range(x - r, x + r + 1):
            display.pixel(i, int(y-math.sqrt(r*r-(x-i)*(x-i))),color)
            display.pixel(i, int(y+math.sqrt(r*r-(x-i)*(x-i))),color)
        for i in range(y-r,y+r+1):
            display.pixel(int(x-math.sqrt(r*r-(y-i)*(y-i))),i,color)
            display.pixel(int(x+math.sqrt(r*r-(y-i)*(y-i))),i,color)
    else :
        for i in range(x-r,x+r+1):
            a = int(math.sqrt(r*r-(x-i)*(x-i)))
            display.vline(i, y - a, a * 2,color)
        for i in range(y-r,y+r+1):
            a = int(math.sqrt(r*r-(y-i)*(y-i)))
            display.hline(x - a, i, a * 2, color)


while True:
    display.fill(0)
    drawcircle(64, 32, 16, 1, 0)
    drawcircle(64, 32, 10, 1, 0)
    x = accelerometer.get_y()
    y = accelerometer.get_x()
    rectx = 128-int(x*64+64)
    recty = int(y*32+32)   
    # drawcircle(64, 32, 32, 1, 0)
    # drawcircle(64, 32, 24, 1, 1)
    # drawcircle(64, 32, 16, 0, 0)
    drawcircle(128-rectx, 64-recty, 8, 1, 1)
    display.show()
    if rectx == 64 and recty == 32:
        rgb.fill((0, 60, 0))
        rgb.write()
    else:
        rgb.fill((0, 0, 0))
        rgb.write()[/mw_shl_code]

改成中心圆点不动,圆环动。


哈哈,一起创造吧。
大圣老师,你这是写帖子吗,记录了一步步的思考过程罢了。

李时念老师说让我开阔思路,大家继续一起帮我想想呗。

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail