((((;゚Д゚))))ガクガクブルブル
成果物
コード
import pyxel, random class App: def __init__(self): self.window = Window() self.rect = RageRect() pyxel.run(self.update, self.draw) def update(self): self.rect.update() def draw(self): self.window.draw() self.rect.draw() class Window: def __init__(self, width=128, height=96, border_width=0): pyxel.init(width, height, border_width=border_width) def draw(self): pyxel.cls(0) class RageRect: def __init__(self, x=0, y=0, width=64, height=64, color=8): self.x = x self.y = y self.w = width self.h = height self.c = color def update(self): self.mx= random.randint(-4, 4) self.my= random.randint(-4, 4) def draw(self): pyxel.rect((pyxel.width / 2) - (self.w / 2) + self.mx, (pyxel.height/ 2) - (self.h / 2) + self.my, self.w, self.h, self.c) App()
クラス分けした。これができると気持ちいい。
要点
アニメーション
class RageRect: def update(self): self.mx= random.randint(-4, 4) self.my= random.randint(-4, 4) def draw(self): pyxel.rect((pyxel.width / 2) - (self.w / 2) + self.mx, (pyxel.height/ 2) - (self.h / 2) + self.my, self.w, self.h, self.c)
update()
でランダムに荒ぶる座標量を算出する:mx
,my
draw()
で矩形を描画する
手順1の座標を反映させることで、ランダムに座標が変わり、荒ぶってくれる。
ランダム
-4
から4
までの範囲にある整数値を返す。
random.randint(-4, 4)
中央
矩形を画面の中央に配置する。
(pyxel.width / 2) - (self.w / 2) (pyxel.height/ 2) - (self.h / 2)
所感
ついに動いた。でも勝手に動くだけじゃなぁ。ゲームといえば操作でしょ。
次回、キー入力に応答して移動させてみる。
前回まで
- ラズパイ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で矩形を描画する
対象環境
- 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