pyxelゲームで使用する16色を任意の色にできる。
成果物
pyxelゲームで使用する16色を任意の色にできる。外部ファイルpalette0.txtに定義した値から読み取るように実装した。
コード
#!/usr/bin/env python3 # coding: utf8 import os, pyxel class App: def __init__(self): self.palette = Palette() self.window = Window() pyxel.run(self.update, self.draw) def update(self): self.window.update() def draw(self): pyxel.cls(0) self.palette.draw() class Window: def __init__(self): self.__palette = Palette() self.__init() @property def Width(self): return 128 @property def Height(self): return 32 @property def Caption(self): return "init() pallet" @property def BorderWidth(self): return 0 def update(self): pass def draw(self): self.Palette.draw() @property def Palette(self): return self.__palette def __init(self): pyxel.init(self.Width, self.Height, border_width=self.BorderWidth, caption=self.Caption, palette=self.Palette.Colors) class Palette: def __init__(self): self.__fid = 0 self.load_palette() def draw(self): for c in range(len(self.__colors)): pyxel.rect(0 + ((c % 8) * 16), 0 + (int(c / 8) * 16), 16, 16, c) def load_palette(self): with open(self.__palette_file_path(self.__fid), 'r') as f: self.__colors = list(map(lambda l: int(l, 16), f)) print(self.__colors) def __palette_file_path(self, pid=0): return os.path.join(self.ResDir, self.__palette_file_name(pid)) def __palette_file_name(self, pid=0): return 'palette' + str(pid) + '.txt' @property def FileId(self): return self.__fid @property def Colors(self): return self.__colors @property def ResDir(self): this = os.path.abspath(__file__) here = os.path.dirname(this) parent = os.path.dirname(here) return os.path.join(parent, 'res') App()
要点
pyxel.init(palette=self.Palette.Colors)
システムAPIによると、palette
引数には整数型配列16個を渡す。
palette=[0x000000, 0x1D2B53, 0x7E2553, 0x008751, 0xAB5236, 0x5F574F, 0xC2C3C7, 0xFFF1E8, 0xFF004D, 0xFFA300, 0xFFEC27, 0x00E436, 0x29ADFF, 0x83769C, 0xFF77A8, 0xFFCCAA]
この整数値は色であるRGB。
起動中にパレット色を変更できない
パレットの指定は初回のinit()
で1回のみ可。2回init()
を実行するとクラッシュする。
スペースキーで別パレットデータを読込、再びinit()
する。以下のようなエラーでクラッシュする。
pyxel error: failed to initialize SDL Audio in 'Audio'
どうあがいても1ゲームでは最初に指定した16色しか使えない。
前回まで
- ラズパイ4Bにpyenvをインストールする(python 3.8.2)
- pyxelをインストールする(pyenv python3.8.2)
- pyxeleditorにおけるImageエディタの使い方
- pyxeleditorにおけるTileMapエディタの使い方
- pyxeleditorにおけるSoundエディタの使い方
- pyxeleditorにおけるMusicエディタの使い方
- pyxelのリソースにおける概念・制限まとめ
- pyxeleditorにおけるpyxresファイル形式まとめ
- pyxelのAPI調査
- pyxelで最小コード 窓を出す
- Pyxelで窓の境界線を消す
- 携帯ゲーム機の解像度
- pyxelで矩形を描画する
- pyxelで荒ぶる矩形
- pyxelでキーに応じて動く矩形
- pyxelで画像を描画する
- pyxelで画像をアニメーションする(スプライト。パラパラ漫画)
- pyxelで左右反転した画像を使ってアニメーションする
- pyxelでテキストを表示する
- pyxelで日本語を表示する1(ビットマップ・フォント)
- ビットマップフォント作成(TTF→PNG)
- pyxelでPNG画像をビットマップフォントとして読込・表示する(日本語表示)
- pyxelのAPIで音を鳴らす
- 画像、音声、動画をHTMLで再生する
- pyxelのAPIでSoundをつなげて鳴らす
- pyxelのAPIでMusicに組み立てて鳴らす
- pyxelのAPIでImageをつくる
- pyxelのAPIでTileMapをつくる
- pyxelのAPIでTileMapをランダム作成する
対象環境
- Raspbierry pi 4 Model B
- Raspbian buster 10.0 2019-09-26 ※
- bash 5.0.3(1)-release
- pyxel 1.3.1
$ uname -a Linux raspberrypi 4.19.97-v7l+ #1294 SMP Thu Jan 30 13:21:14 GMT 2020 armv7l GNU/Linux