M5Stackでデジモンを飼う

***注意！アグモンが動くだけです。育成は出来ません！***

![IMG_6279.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/467051/9365db35-619b-03ec-93aa-6f759ba80b34.jpeg)


#概要
ゴキゲンな蝶になってデジモンを作りました。
M5Stackをあまり使ったことがなかったので、UIFlowとLCDの使い方の勉強を兼ねて、昔ターミナルで飼おうとして途中まで作っていたプログラムをM5Stack用に書き直しました。
Stayしがちなイメージだらけの頼りない翼でも、きっと飛べるさ！

#ソースコード

```python
from m5stack import *
from m5ui import *
from uiflow import *
import random
import time

def reverse_dots(dots):
    for _dots in dots:
      _dots.reverse()

stand = [
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0],
      [0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0],
      [0,0,0,0,1,1,0,1,1,0,0,0,1,0,0,0],
      [0,0,1,1,0,0,0,0,1,1,0,0,1,0,0,0],
      [0,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0],
      [0,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0],
      [0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
      [0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0],
      [0,0,0,0,1,1,0,0,0,0,1,0,1,0,0,0],
      [0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0],
      [0,0,0,1,1,1,0,0,0,1,1,1,0,1,0,0],
      [0,0,0,0,0,1,1,0,0,0,0,1,0,0,1,0],
      [0,0,0,1,1,0,0,1,1,1,1,0,0,0,1,0],
      [0,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1],
      [0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1],
    ]

sit = [
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0],
      [0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0],
      [0,0,0,0,1,1,0,1,1,0,0,0,1,0,0,0],
      [0,0,1,1,0,0,0,0,1,1,0,0,1,0,0,0],
      [0,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0],
      [0,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0],
      [0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
      [0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0],
      [0,0,0,0,1,1,0,0,0,0,1,0,1,0,0,0],
      [0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0],
      [0,0,0,1,0,1,0,0,0,1,0,1,0,1,0,0],
      [0,0,0,1,1,0,1,0,0,0,1,0,0,0,1,0],
      [0,0,1,0,1,0,0,1,1,1,0,1,0,1,0,1],
      [0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1],
    ]

smile = [
      [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0],
      [0,0,1,1,1,1,0,0,0,0,0,1,0,0,0,0],
      [0,1,0,0,0,0,0,0,1,1,0,0,1,0,0,0],
      [0,0,1,1,1,0,0,1,0,0,1,0,1,0,0,0],
      [0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0],
      [0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0],
      [0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0],
      [0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
      [0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0],
      [0,0,0,0,1,1,0,0,0,0,1,0,1,0,0,0],
      [0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0],
      [0,0,0,1,1,1,0,0,0,1,1,1,0,1,0,0],
      [0,0,0,0,0,1,1,0,0,0,0,1,0,0,1,0],
      [0,0,0,1,1,0,0,1,1,1,1,0,0,0,1,0],
      [0,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1],
      [0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1],
    ]

angry = [
      [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0],
      [0,0,1,1,1,1,0,0,0,0,0,1,0,0,0,0],
      [0,1,0,0,0,0,0,0,1,1,0,0,1,0,0,0],
      [0,0,1,1,1,0,0,1,1,0,0,0,1,0,0,0],
      [0,0,0,0,0,1,0,1,1,0,0,0,0,1,0,0],
      [0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0],
      [0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0],
      [0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
      [0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0],
      [0,0,0,0,1,1,0,0,0,0,1,0,1,0,0,0],
      [0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0],
      [0,0,0,1,1,1,0,0,0,1,1,1,0,1,0,0],
      [0,0,0,0,0,1,1,0,0,0,0,1,0,0,1,0],
      [0,0,0,1,1,0,0,1,1,1,1,0,0,0,1,0],
      [0,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1],
      [0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1],
    ]

def display():
    start_x = 8
    move_list = [-2, -1, 1, 2]

    pose_list1 = [stand, stand, stand, stand, stand, stand, stand, stand, sit, sit]
    pose_list2 = [angry, smile]

    pos = start_x

    dot_height = 10
    dot_width = 10
    pos_x = 5
    pos_y = 5

    direction = "l"

    while True:
        setScreenColor(0x000000)
        # M5Img(0, 0, "res/XXX.jpg", True)
        dots = random.choice(pose_list1)
        move = random.choice(move_list)
        pos = pos + move

        if pos < 0:
            pos = 2
        elif pos > 8:
            pos = 6

        if pos == 0:
            dots = random.choice(pose_list2)
        elif pos == 8:
            move = 0
            dots = random.choice(pose_list2)
            direction = "r"
            reverse_dots(dots)

        if move > 0 and direction == "l":
            direction = "r"
            reverse_dots(dots)

        for i, dots_row in enumerate(dots):
            y = (pos_y*dot_height) + (i * (dot_height - 1))
            line = ([0] * pos) + dots_row + ([0] * (int(320/dot_width) - len(([0] * pos) + dots_row)))
            for j, dot in enumerate(line):
                x = (pos_x*dot_width) + (j * (dot_width - 1))
                if dot == 1:
                    M5Rect(x, y, dot_height, dot_width, 0xFFFFFF, 0xFFFFFF)
                    # M5Rect(x, y, dot_height, dot_width, 0x000000, 0x000000)
                else:
                    M5Rect(x, y, dot_height, dot_width, 0x000000, 0x000000)
                    # pass
        if direction == "r":
            direction = "l"
            reverse_dots(dots)
        time.sleep(0.5)

display()

```

#備考
動きの法則はよくわからなかったのでそれっぽく自前で実装した。
動かしてみると結構描画が遅く、画面のちらつきが気になる。
M5Stackは描画の遅さに関しては有名なようで、早くする方法も紹介されていたりするが、micropythonのライブラリなどは見つけられなかった。
他のデジモンも表示したいし育ててみたいけどデータがない。
あとドットを配列にするのメンドクサイ。
ていうか進化ぁ～するまでバッテリー持たないよね。
応用すればスプレッドシートやExcelでも飼えるかもしれない。

#おまけ
上記のソースコードを実行するとモノクロの画面になりますが、背景画像を表示するとよりそれっぽくなります。

↓LCDをフリー素材を使ってペイントで書いておきました
![over13.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/467051/cfcd8ead-bfeb-02c5-b6ee-b44dab0a24cb.jpeg)

↓フリー素材で好きな背景画像を検索して
https://www.google.com/search?q=%E3%83%95%E3%83%AA%E3%83%BC%E7%B4%A0%E6%9D%90+%E8%83%8C%E6%99%AF&tbm=isch&chips=q:%E3%83%95%E3%83%AA%E3%83%BC%E7%B4%A0%E6%9D%90+%E8%83%8C%E6%99%AF,g_1:%E8%87%AA%E7%84%B6:BQDEpf6mPbg%3D&rlz=1C1QABZ_jaJP858JP858&hl=ja&sa=X&ved=2ahUKEwilioT-9OvqAhX7y4sBHQzUD8MQ4lYoBXoECAEQHw&biw=1903&bih=920

↓ここで320x240にサイズ変更して
https://www.iloveimg.com/ja/resize-image/resize-jpg

↓ここで50％50％で合成すればいい感じになります
https://www.mojimaru.com/corabo/mask.php


![mojimaru7.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/467051/3f1e47b4-9480-9dfa-c5c5-5f47a25e39cb.jpeg)

コメントアウトしている105行目,134行目,137行目の3行を復活させて、すぐ上の104行目,133行目,136行目をコメントアウトすれば背景を適用できます。
105行目の画像ファイルのパスを適宜変更してください。


仕事机の傍らで動いていると可愛いですね。

![unnamed.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/467051/0b0dd062-4203-8ee5-017c-bd61be974841.jpeg)


<blockquote class="twitter-tweet"><p lang="ja" dir="ltr">何がウォウウォウウォウォウウォウ〜♩この空に届くのだろう〜♪<a href="https://twitter.com/hashtag/M5Stack?src=hash&amp;ref_src=twsrc%5Etfw">#M5Stack</a> <a href="https://t.co/t1dY04mutj">pic.twitter.com/t1dY04mutj</a></p>&mdash; たくみ@スカジャンのエンジニア (@hatt_takumi) <a href="https://twitter.com/hatt_takumi/status/1287047576445804544?ref_src=twsrc%5Etfw">July 25, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>