Skip to content

Latest commit

 

History

History
890 lines (658 loc) · 22.9 KB

File metadata and controls

890 lines (658 loc) · 22.9 KB

Pyxel API sample

グラフィックス,イメージクラス,タイルマップクラス
バージョン:Pyxel 1.9版

contents

分類 項目
システム 変数 init() run() show() flip() quit()
リソース load() save()
入力 変数 btn() btnp() btnr() mouse() キー記述例
グラフィックス 変数 image() tilemap() clip() camera() pal() cls() pget() pset()
line() rect() rectb() circ() circb() elli() ellib() tri() trib()
fill() blt() bltm() text() 表示色
イメージクラス 変数 set() load() save() pget() pset()
タイルマップクラス 変数 set() pget() pset()
オーディオ sound() music() play_pos() play() playm() stop()
サウンドクラス 変数 set() set_notes() set_tones() set_volumes() set_effects()
ミュージッククラス 変数 set()
数学 ceil() floor() sgn() sqrt() sin() cos() atan2() rseed() rndi() rndf() nseed() noise()



  • コード例で使用しているリソースファイル(sample.pyxres)は公式サイトからDownloadできます。

グラフィックス


variable

変数名 説明 記述例
colors リスト パレットの表示色リスト。
表示色は 24 ビット数値(0x000000-0xffffff)で指定します。
pyxel.colors

pyxel.colors[3] = 0x19C5FA    # 表示色3を薄い青色に変更
pyxel.cls(3)

・Python リストを直接代入、取得する場合はcolors.from_listとcolors.to_listを使用してください。

# デフォルトのカラーパレットを退避
org_colors = pyxel.colors.to_list()

# デフォルトのカラーパレットで復帰
pyxel.colors.from_list(org_colors)

命令の引数など通常(pyxel.colors以外)は,色の番号で指定します。
image color_L


image()

イメージバンクimg (0-2) を操作します。(イメージクラスを参照のこと)
image(img)

引数 説明
img u32 操作対象のイメージバンク (0-2)

戻り値 説明
イメージバンク Image 指定したイメージバンクのオブジェクト

【参考】イメージバンクに描画する例

import pyxel
pyxel.init(64, 64)

pyxel.cls(1)
pyxel.image(0).text(0,0,"Image",7)  # img 0 にテキストを描画
pyxel.blt(10,10, 0, 0,0, 20,8, 0)   # img 0 の指定範囲を画面に表示

pyxel.show()

※画面にテキストを表示する場合,通常は pyxel.text() を用います。この例はイメージバンクの絵を変更する特殊な例です(キャラクタの見た目を変えてしまう場合など)。


tilemap()

タイルマップtm (0-7) を操作します。(タイルマップクラスを参照のこと)
tilemap(tm)

引数 説明
tm u32 操作対象のタイルマップ (0-7)

戻り値 説明
タイルマップ Tilemap 指定したタイルマップのオブジェクト

clip()

画面の描画領域を (x, y) から幅w、高さhに設定します。clip()で描画領域を全画面にリセットします。
clip(x, y, w, h)

引数 説明
x f64 左上座標
y f64 左上座標
w f64
h f64 高さ

import pyxel
pyxel.init(64, 64)
pyxel.mouse(True)

def update():
    return

def draw():
    pyxel.cls(1)
    pyxel.rectb(2,2,60,60,10)
    pyxel.text(8,28,"Hello world!",7)
    pyxel.clip(pyxel.mouse_x,pyxel.mouse_y, 32,32)
    return

pyxel.run(update, draw)

image clip

camera()

画面の左上隅の座標を (x, y) に変更します。camera()で左上隅の座標を (0, 0) にリセットします。
camera(x, y)

引数 説明
x f64 左上座標
y f64 左上座標

import pyxel
pyxel.init(64, 64)

def update():
    return

def draw():
    pyxel.cls(1)
    pyxel.camera(-5,-5)
    pyxel.rectb(2,2,60,60,10)
    pyxel.text(8,28,"Hello world!",7)
    return

pyxel.run(update, draw)

image pal

pal()

描画時に色col1をcol2に置き換えます。pal()で初期状態にリセットします。
pal(col1, col2)

引数 説明
col1 u8 置き換え対象の色 (0-15)
col2 u8 置き換える色 (0-15)

import pyxel
pyxel.init(64, 64)

flg = False
def update():
    global flg
    if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
        flg = not(flg)
    return

def draw():
    pyxel.cls(1)
    pyxel.rectb(2,2,60,60,10)
    pyxel.text(8,28,"Hello world!",7)
    if flg:
        pyxel.pal(1,7)
        pyxel.pal(7,1)
    else:
        pyxel.pal()
    return

pyxel.run(update, draw)

image pal


cls()

画面を色colでクリアします。
cls(col)

引数 説明
col u8 画面を塗りつぶす色 (0-15)

pyxel.cls(0)

表示色 image color_s


pget()

画面上 (x, y) のピクセルの色を取得します。
pget(x, y)

引数 説明
x f64 x座標
y f64 y座標

戻り値 説明
col u8 ピクセルの色 (0-15)

import pyxel
pyxel.init(64, 64)

col = 0
def update():
    global col
    col = pyxel.pget(pyxel.mouse_x, pyxel.mouse_y)
    return

def draw():
    pyxel.cls(1)
    for i in range(16):
        pyxel.rect(4*i,0,4,40,i)
    pyxel.text(8,48,"col="+str(col),7)
    return

pyxel.run(update, draw)

image pget

pset()

(x, y) に色colのピクセルを描画します。
pset(x, y, col)

引数 説明
x f64 x座標
y f64 y座標
col u8 描画色 (0-15)

import pyxel
pyxel.init(64, 64)

def update():
    return

def draw():
    #pyxel.cls(0)
    pyxel.pset(pyxel.mouse_x, pyxel.mouse_y,pyxel.rndi(1,15))
    return

pyxel.run(update, draw)

image pset


表示色 image color_s


line()

色colの直線を (x1, y1)-(x2, y2) に描画します。
line(x1, y1, x2, y2, col)

rect()

幅w、高さh、色colの矩形を (x, y) に描画します。※矩形(くけい)は長方形
rect(x, y, w, h, col)

rectb()

幅w、高さh、色colの矩形の輪郭線を (x, y) に描画します。
rectb(x, y, w, h, col)

circ()

半径r、色colの円を (x, y) に描画します。
circ(x, y, r, col)

circb()

半径r、色colの円の輪郭線を (x, y) に描画します。
circb(x, y, r, col)

elli()

幅w、高さh、色colの楕円を (x, y) に描画します。
elli(x, y, w, h, col)

ellib()

幅w、高さh、色colの楕円の輪郭線を (x, y) に描画します。
ellib(x, y, w, h, col)

tri()

頂点が (x1, y1)、(x2, y2)、(x3, y3)、色colの三角形を描画します。
tri(x1, y1, x2, y2, x3, y3, col)

trib()

頂点が (x1, y1)、(x2, y2)、(x3, y3)、色colの三角形の輪郭線を描画します。
trib(x1, y1, x2, y2, x3, y3, col)


表示色 image color_s


import pyxel
pyxel.init(128, 128)

def update():
    return

def draw():
    pyxel.cls(0)
    # 線
    pyxel.line(4,4, 64,4, 3)
    # 長方形
    pyxel.rect(2,8, 24,16, 4)
    pyxel.rectb(42,8, 24,16, 5)
    # 円
    pyxel.circ(12,38, 8, 6)
    pyxel.circb(50,38, 8, 7)
    # 楕円
    pyxel.elli(2,54, 24,16, 8)
    pyxel.ellib(42,54, 24,16, 9)
    # 三角形
    pyxel.tri(2,100, 22,100, 12,80, 10)
    pyxel.trib(42,100, 62,100, 52,80, 11)
    return

pyxel.run(update, draw)

fill()

(x, y) と同じ色でつながっている領域を色colで塗りつぶします。
fill(x, y, col)

引数 説明
x f64 x座標
y f64 y座標
col u8 塗りつぶし色 (0-15)

import pyxel
pyxel.init(64, 64)
pyxel.mouse(True)

def update():
    return

def draw():
    pyxel.cls(0)
    pyxel.rectb(2,2, 50,50, 7)
    pyxel.circb(40,20, 18, 7)
    pyxel.tri(10,60, 60,60, 60,40, 7)
    pyxel.fill(pyxel.mouse_x, pyxel.mouse_y, 10)
    return

pyxel.run(update, draw)

image fill


表示色 image color_s


blt()

イメージバンクimg (0-2) の (u, v) からサイズ (w, h) の領域を (x, y) にコピーします。w、hそれぞれに負の値を設定すると水平、垂直方向に反転します。colkeyに色を指定すると透明色として扱われます。
blt(x, y, img, u, v, w, h, [colkey])

引数 説明
x f64 描画先のx座標
y f64 描画先のy座標
img u32 イメージバンク (0-2)
u f64 ドット絵の座標
v f64 ドット絵の座標
w f64 ドット絵の幅(マイナスで左右反転)
h f64 ドット絵の高さ(マイナスで上下反転)
colkey u8 透明色 (0-15) (表示色

import pyxel
pyxel.init(42, 12)
pyxel.load("sample.pyxres")

def update():
    return

def draw():
    pyxel.cls(3)
    pyxel.blt(2,2, 0, 8,0, 8,8)
    pyxel.blt(12,2, 0, 8,0, 8,8, 0)
    pyxel.blt(22,2, 0, 8,0, -8,8, 0)
    pyxel.blt(32,2, 0, 8,0, 8,-8, 0)
    return

pyxel.run(update, draw)

image blt

bltm()

タイルマップtm (0-7) の (u, v) からサイズ (w, h) の領域を (x, y) にコピーします。w、hそれぞれに負の値を設定すると水平、垂直方向に反転します。colkeyに色を指定すると透明色として扱われます。1 タイルのサイズは 8x8 ピクセルで、(tile_x, tile_y)のタプルとしてタイルマップに格納されています。
bltm(x, y, tm, u, v, w, h, [colkey])

引数 説明
x f64 描画先のx座標
y f64 描画先のy座標
tm u32 タイルマップ (0-7)
u f64 タイルマップの座標(ピクセル単位)
v f64 タイルマップの座標(ピクセル単位)
w f64 タイルマップの幅(ピクセル単位 マイナスで左右反転)
h f64 タイルマップ高さ(ピクセル単位 マイナスで上下反転)
colkey u8 透明色 (0-15) (表示色

pyxel.bltm(0,0, 0, 0,0, 128,128, 0)
pyxel.camera()
pyxel.bltm(0,0, 0, self.scroll_x,self.scroll_y, pyxel.width,pyxel.height, 0)
pyxel.camera(self.scroll_x,self.scroll_y)
import pyxel
pyxel.init(42, 12)
pyxel.load("sample.pyxres")

def update():
    return

def draw():
    pyxel.cls(3)
    pyxel.bltm(2,2, 0, 8,0, 8,8)
    pyxel.bltm(12,2, 0, 8,0, 8,8, 0)
    pyxel.bltm(22,2, 0, 8,0, -8,8, 0)
    pyxel.bltm(32,2, 0, 8,0, 8,-8, 0)
    return

pyxel.run(update, draw)

text()

色colの文字列sを (x, y) に描画します。
text(x, y, s, col)

引数 説明
x f64 描画先のx座標
y f64 描画先のy座標
s str 文字列(半角英数)
col u8 文字色 (0-15)

pyxel.text(4, 4, "Hello, Pyxel!", pyxel.frame_count % 16)

表示色 image color_s


ページの先頭に戻る 

イメージクラス


variable

変数名 説明 記述例
width u32 イメージの幅 pyxel.image(0).width
height u32 イメージの高さ pyxel.image(0).height

print(pyxel.image(0).width)   # 256
print(pyxel.image(0).height)  # 256

set()

(x, y) に文字列のリストでイメージを設定します。
set(x, y, data)

引数 説明
x i32 イメージバンクの座標
y i32 イメージバンクの座標
data リスト ["1行目データ","2行目データ",・・・]
ドットの色を16進の文字で指定

image color_hex ※ 10-15 を a-f で表す


import pyxel
pyxel.init(32, 32)

data = ["bbbb",
        "b77b",
        "b77b",
        "3333"]
pyxel.image(0).set(0,0, data)

def update():
    return

def draw():
    pyxel.cls(0)
    pyxel.blt(4,8, 0, 0,0, 4,4, 0)
    return

pyxel.run(update, draw)

img set

load()

(x, y) に画像ファイル (png/gif/jpeg) を読み込みます。
load(x, y, filename)

引数 説明
x i32 イメージバンクの座標
y i32 イメージバンクの座標
filename str 画像ファイル名 (png/gif/jpeg)

pyxel.image(2).load(0, 0, "penguin.png")    # イメージバンク2に画像ファイルを読み込む例

save()

指定したイメージバンクをPNGファイルに出力する。(上級者向けAPI)
save(filename, scale)

引数 説明
filename str 出力ファイル名 (.png が付加される)
scale u32 画像サイズの倍率

pyxel.image(0).save("image_0", 1)    # イメージバンク0を等倍で出力

pget()

イメージバンクの (x, y) のピクセルの色 (0-15) を取得します。
pget(x, y)

引数 説明
x f64 イメージバンクの座標
y f64 イメージバンクの座標

戻り値 説明
col u8 ピクセルの色 (0-15)

import pyxel
pyxel.init(32, 32)
pyxel.load("sample.pyxres")

# リソースファイルのイメージバンク0の色を取得して
# 行のデータごとに16進数の文字列に変換する例
data = []
for v in range(8):
    tmpstr = ""
    for u in range(8,16):
        tmpstr += format(pyxel.image(0).pget(u, v),'x')
    data.append(tmpstr)

# 文字列データを標準出力
for row in data:
    print('"'+row+'",')

# 文字列でイメージバンク2に設定
pyxel.image(2).set(0,0,data)

def update():
    return

def draw():
    pyxel.cls(0)
    pyxel.blt(0,0, 2, 0,0, 8,8)
    return

pyxel.run(update, draw)

 print結果

"0a0ccc0a",
"0ac666ca",
"70f1f1f0",
"07ccccc0",
"00fccc50",
"000c5c0f",
"00556600",
"00000500",

pset()

イメージバンクの (x, y) に色colのピクセルを描画します。
pset(x, y, col)

引数 説明
x f64 イメージバンクの座標
y f64 イメージバンクの座標
col u8 描画色 (0-15)

import pyxel
pyxel.init(32, 32)
pyxel.load("sample.pyxres")
pyxel.image(0).pset(11,0, 10)
pyxel.image(0).pset(12,1, 10)
pyxel.image(0).pset(13,0, 10)

def update():
    return

def draw():
    pyxel.cls(0)
    pyxel.blt(12,2, 0, 8,0, 8,8, 0)
    return

pyxel.run(update, draw)

表示色 image color_s


ページの先頭に戻る 

タイルマップクラス


variable

変数名 説明 記述例
width u32 タイルマップの幅 pyxel.tilemap(0).width
height u32 タイルマップの高さ pyxel.tilemap(0).height
refimg u32 タイルマップが参照するイメージバンク (0-2) pyxel.tilemap(0).refimg

print(pyxel.tilemap(0).width)   # 256
print(pyxel.tilemap(0).height)  # 256
pyxel.tilemap(0).refimg = 1  # イメージバンク1を設定する例

03_draw_api.py内の切り替え例

print(pyxel.tilemap(0).refimg, pyxel.tilemap(0).image)
pyxel.tilemap(0).image = pyxel.image(1)
print(pyxel.tilemap(0).refimg, pyxel.tilemap(0).image)

set()

(x, y) に文字列のリストでタイルマップを設定します。
set(x, y, data)

引数 説明
x i32 タイルマップの書き込み開始位置
y i32 タイルマップの書き込み開始位置
data リスト ["1行目データ","2行目データ",・・・]
タイル座標を16進数で指定

※APIリファレンス記載の例にあるdataの値は古い仕様のものです。

03_draw_api.py 内のコードを参照してください。

import pyxel
pyxel.init(128, 128)
pyxel.load("sample.pyxres")

# タイルマップの左上4マスを下記のタイルにする例
# ( 1,0) ( 2,0) -> "0100 0200" 
# (12,0) (12,1) -> "0c00 0c01"
# 
# (tile_x,tile_y)の指定を xxyy の2オクテットで表す
# 値は16進数で範囲は0x00-0x1f
data = ["0100 0200",
        "0c00 0c01"]
pyxel.tilemap(0).set(0, 0, data)

def update():
    return

def draw():
    pyxel.cls(0)
    pyxel.bltm(0,0, 0, 0,0, 128,128)
    return

pyxel.run(update, draw)

pget()

(x, y) のタイルを取得します。タイルは(tile_x, tile_y)のタプルです。
pget(x, y)

引数 説明
x f64 タイルマップの位置
y f64 タイルマップの位置

戻り値 説明
Tile (u8,u8) タイル種類(イメージバンク上の位置)

import pyxel
pyxel.init(128, 128)
pyxel.load("sample.pyxres")
pyxel.mouse(True)

tile = (0,0)
def update():
    global tile
    xidx = pyxel.mouse_x // 8
    yidx = pyxel.mouse_y // 8
    tile = pyxel.tilemap(0).pget(xidx,yidx)
    return

def draw():
    pyxel.cls(0)
    pyxel.bltm(0,0, 0, 0,0, 128,128)
    pyxel.text(1,9, str(tile), 7)
    return

pyxel.run(update, draw)

Note
タイル種類,イメージバンクとタイルマップの関係については,タイルマップ を参照してください。


pset()

(x, y) にタイルを設定します。タイルは(tile_x, tile_y)のタプルです。
pset(x, y, tile)

引数 説明
x f64 タイルマップの位置
y f64 タイルマップの位置
Tile (u8,u8) タイル種類(イメージバンク上の位置)

xidx = x // 8
yidx = y // 8
pyxel.tilemap(0).pset(xidx,yidx, (1,0) )

color

表示色の番号です。
img color


番号 表示色 16進数 番号 表示色 16進数
0 $\color[RGB]{0,0,0} ■(0,0,0)$ 000000   8 $\color[RGB]{212,24,108} ■(212,24,108)$ d4186c
1 $\color[RGB]{43,51,95} ■(43,51,95)$ 2b335f   9 $\color[RGB]{211,132,65} ■(211,132,65)$ d38441
2 $\color[RGB]{126,32,114} ■(126,32,114)$ 7e2072   10 $\color[RGB]{233,195,91} ■(233,195,91)$ e9c35b
3 $\color[RGB]{25,149,156} ■(25,149,156)$ 19959c   11 $\color[RGB]{112,198,169} ■(112,198,169)$ 70c6a9
4 $\color[RGB]{139,72,82} ■(139,72,82)$ 8b4852   12 $\color[RGB]{118,150,222} ■(118,150,222)$ 7696de
5 $\color[RGB]{57,92,152} ■(57,92,152)$ 395c98   13 $\color[RGB]{163,163,163} ■(163,163,163)$ a3a3a3
6 $\color[RGB]{169,193,255} ■(69,193,255)$ a9c1ff   14 $\color[RGB]{255,151,152} ■(255,151,152)$ ff9798
7 $\color[RGB]{238,238,238} ■(238,238,238)$ eeeeee   15 $\color[RGB]{237,199,176} ■(237,199,176)$ edc7b0

ページの先頭に戻る  TOPに戻る