pyxelはpython3で簡単にレトロゲームが作成できるゲームエンジンです。
readmeのサンプルを修正する形でクリックゲームを作りました。
install
linux版は開発中とのことだったので、Macにインストールしました
brew install python3 glfw
pip3 install pyxel
サンプルのインストールをして試すこともできます
cd ~/src
install_pyxel_examples
cd pyxel_examples
python3 01_hello_pyxel.py
base
最初は正方形1つが横に移動していくだけのものです。
- init 初期化とアプリの実行
- update ステータスの更新(座標xの更新)
- draw 正方形の描画
import pyxel
class App:
def __init__(self):
pyxel.init(160, 120)
self.x = 0
pyxel.run(self.update, self.draw)
def update(self):
self.x = (self.x + 1) % pyxel.width
def draw(self):
pyxel.cls(0)
pyxel.rect(self.x, 0, self.x + 7, 7, 9)
App()
できたもの
移動する色、サイズ違いの正方形をクリックして消していくゲームです
QuickTimePlayer + Giftedでgifを作ったのですが、縦横比がおかしくなってしまいましたorz..
code
import pyxel
import random
SQUARE_INITIAL_COUNT = 10
SCREEN_WIDTH = 120
SCREEN_HEIGHT = 160
class Square:
def __init__(self):
self.size = random.randint(8, 16)
self.x = random.randint(0, SCREEN_WIDTH)
self.y = random.randint(0, SCREEN_HEIGHT)
self.x2 = self.x + self.size
self.y2 = self.y + self.size
self.color = random.randint(1,15)
self.is_alive = True
def update(self):
self.x = (self.x + 0.5) % SCREEN_WIDTH
self.y = (self.y + 0.5) % SCREEN_HEIGHT
if self.y2 >= SCREEN_HEIGHT -10:
self.y = 0
self.x2 = self.x + self.size
self.y2 = self.y + self.size
if self.clicked() == True:
self.remove()
def clicked(self):
if pyxel.mouse_x < self.x or pyxel.mouse_x > self.x2:
return False
if pyxel.mouse_y < self.y or pyxel.mouse_y > self.y2:
return False
return True
def remove(self):
if pyxel.btnp(pyxel.MOUSE_LEFT_BUTTON):
self.is_alive = False
def colorChange(self):
if pyxel.btnp(pyxel.MOUSE_LEFT_BUTTON):
self.color = random.randint(1,15)
class App:
def __init__(self):
pyxel.init(SCREEN_WIDTH, SCREEN_HEIGHT)
pyxel.mouse(True)
self.squares = [Square() for _ in range(SQUARE_INITIAL_COUNT)]
pyxel.run(self.update, self.draw)
def update(self):
if pyxel.btnp(pyxel.KEY_Q):
pyxel.quit()
for index, square in enumerate(self.squares):
square.update()
if not square.is_alive:
del self.squares[index]
def draw(self):
pyxel.cls(0)
for square in self.squares:
pyxel.rectb(square.x, square.y, square.x2, square.y2, square.color)
# text表示 todo: 関数にしたい
color = 3
if len(self.squares) == 0:
text = "Clear!"
color = pyxel.frame_count % 15 + 1
else:
text = "counter: " + str(len(self.squares))
return pyxel.text(0, SCREEN_HEIGHT -7, text, color)
App()