注意!アグモンが動くだけです。育成は出来ません!
概要
ゴキゲンな蝶になってデジモンを作りました。
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,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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でも飼えるかもしれない。
おまけ
上記のソースコードを実行するとモノクロの画面になりますが、背景画像を表示するとよりそれっぽくなります。
↓ここで320x240にサイズ変更して
https://www.iloveimg.com/ja/resize-image/resize-jpg
↓ここで50%50%で合成すればいい感じになります
https://www.mojimaru.com/corabo/mask.php
コメントアウトしている105行目,134行目,137行目の3行を復活させて、すぐ上の104行目,133行目,136行目をコメントアウトすれば背景を適用できます。
105行目の画像ファイルのパスを適宜変更してください。
仕事机の傍らで動いていると可愛いですね。
何がウォウウォウウォウォウウォウ〜♩この空に届くのだろう〜♪#M5Stack pic.twitter.com/t1dY04mutj
— たくみ@スカジャンのエンジニア (@hatt_takumi) July 25, 2020