6
6

More than 3 years have passed since last update.

M5Stackでデジモンを飼う

Last updated at Posted at 2020-07-27

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

IMG_6279.jpg

概要

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

ソースコード

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://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

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

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

unnamed.jpg

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6