MicroPythonで使うWio Terminal - machine.Sprite

MicroPythonで使うWio Terminal - 目次

machine.Spriteの使用例

RAM上にスクリーンバッファ(仮想画面)を確保して描画処理を行った後、一気にLCDへ転送します。

# Wio Terminal machine.Spriteのサンプル
# This code is provided under a CC0 Public Domain License.
# http://creativecommons.org/publicdomain/zero/1.0/
# 2022年6月18日 佐藤恭一 https://kyoutan.jpn.org
#
# 透明色の指定はあり。拡大・回転する関数は無いようだった。
# 文字や図形の描画関数は machine.LCDと同じ https://kyouichisato.blogspot.com/2022/05/micropythonwio-terminal.html

from machine import LCD
from machine import Sprite

lcd = LCD()
lcd.fillScreen(lcd.color.BLUE)

sprite = Sprite(lcd)                   # Spriteの描画先を指定する(引数間違っているかもしれない)
width, height = 32, 32
sprite.createSprite(width, height)     # サイズを指定してSpriteを作成
sprite.fillScreen(sprite.color.GREEN)  # Spriteを塗りつぶし
sprite.fillSprite(sprite.color.BLACK)  # fillScreenも fillSpriteも同じ動作のようだった
x = sprite.width // 2
y = sprite.height // 2
r = sprite.width // 3
color = sprite.color.RED
sprite.fillCircle(x, y, r, color)     # 円を描く

x, y = 0, 0
sprite.pushSprite(x, y)               # そのままLCDに転送

x = sprite.width
transparent = sprite.color.BLACK
sprite.pushSprite(x, y, transparent)  # 透明色を指定して転送

sprite.deleteSprite()           # Spriteを削除(メモリ解放)
x = lcd.width // 2
y = lcd.height // 2
sprite.createSprite(x, y)       # 再度Spriteを作成して使用できる
sprite.fillSprite(sprite.color.BLACK)
x, y = 0, 0
font = 2
sprite.setTextColor(Sprite.color.WHITE)
sprite.drawString("ABCDE01234", x, y, font)  # machine.LCDと同じく文字や図形の描画ができる
y += 16
sprite.drawString("FGHIJ56789", x, y, font)
y += 16
sprite.drawString("KLMNO01234", x, y, font)
y += 16
sprite.drawString("PQRST56789", x, y, font)
y = 0
sprite.pushSprite(x, y, transparent)
x += 32-8
y += 32-8
sprite.pushSprite(x, y, transparent)
x += 64
y += 64
sprite.pushSprite(x, y, transparent)

sprite.deleteSprite()
sprite.createSprite(lcd.width, lcd.height)
sprite.fillSprite(sprite.color.BLUE)
sprite2 = Sprite(sprite)               # 描画先を別のspriteに指定しようとしてもLCDに描画されてしまう
sprite2.createSprite(32, 32)
sprite2.fillSprite(sprite.color.BLACK)
sprite2.fillCircle(16, 16, 10, sprite.color.RED)
x, y = 0, 0
sprite2.pushSprite(x, y, transparent)            # spriteに描画してほしいけどLCDに描画されてしまう
#sprite.pushSprite(0, 0)


透明色を指定して描画することができる。黒字に赤の丸の画像で、黒を透明色として描画すると、赤丸だけを表示することができる。


表示の重ね合わせができる。書き換え処理をSpriteに対して行ってから、一気にLCDへ転送することで、書き換え時のチラツキをなくす事もできる。

LCDの描画関数をそのままSpriteでも使うことができる。


Spriteの描画関数一覧

>>> help(machine.Sprite)
object <class 'eSprite'> is of type type
  deinit -- <function>
  __enter__ -- <function>
  __exit__ -- <function>
  fillScreen -- <function>
  fillSprite -- <function>
  createSprite -- <function>
  pushSprite -- <function>
  deleteSprite -- <function>
  setRotation -- <function>
  getRotation -- <function>
  invertDisplay -- <function>
  drawRightString -- <function>
  drawChar -- <function>
  drawString -- <function>
  drawCentreString -- <function>
  setTextFont -- <function>
  drawPixel -- <function>
  fontHeight -- <function>
  textWidth -- <function>
  drawNumber -- <function>
  drawFloat -- <function>
  setTextColor -- <function>
  setTextSize -- <function>
  setTextWrap -- <function>
  setTextDatum -- <function>
  getTextDatum -- <function>
  setTextPadding -- <function>
  drawLine -- <function>
  drawFastVLine -- <function>
  drawFastHLine -- <function>
  drawRect -- <function>
  fillRect -- <function>
  drawRoundRect -- <function>
  fillRoundRect -- <function>
  drawCircle -- <function>
  drawCircleHelper -- <function>
  fillCircle -- <function>
  fillCircleHelper -- <function>
  drawEllipse -- <function>
  fillEllipse -- <function>
  drawTriangle -- <function>
  fillTriangle -- <function>
  getCursorX -- <function>
  getCursorY -- <function>
  getPivotX -- <function>
  getPivotY -- <function>
  color16to8 -- <function>
  color565 -- <function>
  getPivotX -- <function>
  setCursor -- <function>
  setPivot -- <function>
  pushImage -- <function>
  color -- <class 'color'>
  datum -- <class 'datum'>

>>> dir(sprite)
['__enter__', '__exit__', 'color', 'color16to8', 'color565', 'createSprite' , 'datum', 'deinit', 'deleteSprite', 'drawCentreString', 'drawChar', 'drawCircle' , 'drawCircleHelper', 'drawEllipse', 'drawFastHLine', 'drawFastVLine', 'drawFloat' , 'drawLine', 'drawNumber', 'drawPixel', 'drawRect', 'drawRightString', 'drawRoundRect' , 'drawString', 'drawTriangle', 'fillCircle', 'fillCircleHelper', 'fillEllipse' , 'fillRect', 'fillRoundRect', 'fillScreen', 'fillSprite', 'fillTriangle', 'fontHeight' , 'getCursorX', 'getCursorY', 'getPivotX', 'getPivotY', 'getRotation', 'getTextDatum' , 'height', 'invertDisplay', 'pushImage', 'pushSprite', 'setCursor', 'setPivot' , 'setRotation', 'setTextColor', 'setTextDatum', 'setTextFont', 'setTextPadding' , 'setTextSize', 'setTextWrap', 'textWidth', 'width']

このブログの人気の投稿

windowsで「インターネット接続の共有」の設定

月刊 I/O 記事リスト 1976~1989

X68000実機のROMを保存