Last active
October 16, 2020 01:17
-
-
Save cacapon/575ffb54671245b7ccd05a413ffda26b to your computer and use it in GitHub Desktop.
シーン管理を分離しました。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyxel | |
from scene_abc import AbstractScene | |
class CastleScene(AbstractScene): | |
def __init__(self): | |
super().__init__() | |
def update(self) -> bool: | |
# TODO: 暫定でスペースで変更にしている | |
if pyxel.btnp(pyxel.KEY_SPACE): | |
return self.GAMEMODE.Dungeon | |
return self.GAMEMODE.Castle | |
def draw(self): | |
# TODO: 暫定で文字のみ | |
pyxel.text(75, 0, "Castle Scene", 11) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyxel | |
from scene_abc import AbstractScene | |
class DungeonScene(AbstractScene): | |
def __init__(self): | |
super().__init__() | |
def update(self) -> bool: | |
# TODO: 暫定でスペースで変更にしている | |
if pyxel.btnp(pyxel.KEY_SPACE): | |
return self.GAMEMODE.Title | |
return self.GAMEMODE.Dungeon | |
def draw(self): | |
# TODO: 暫定で文字のみ | |
pyxel.text(75, 0, "Dungeon Scene", 11) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum, auto | |
class GAMEMODE(Enum): | |
# 画面のシーンをEnumで定義します | |
Title = auto() | |
Castle = auto() | |
Dungeon = auto() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABCMeta, abstractmethod | |
from gamemode import GAMEMODE | |
# 大元になる抽象基底クラス | |
class AbstractScene(metaclass=ABCMeta): | |
def __init__(self): | |
self.GAMEMODE = GAMEMODE | |
@abstractmethod | |
def update(self) -> bool: | |
pass | |
@abstractmethod | |
def draw(self): | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyxel | |
from gamemode import GAMEMODE | |
from title_scene import TitleScene | |
from castle_scene import CastleScene | |
from dangeon_scene import DungeonScene | |
class App: | |
def __init__(self): | |
pyxel.init(128, 128) | |
pyxel.load("sample.pyxres") | |
self.scene = { | |
GAMEMODE.Title: TitleScene(), | |
GAMEMODE.Castle: CastleScene(), | |
GAMEMODE.Dungeon: DungeonScene(), | |
} | |
self.my_gamemode = GAMEMODE.Title # 最初に表示したいシーンを初期値に設定します。 | |
pyxel.run(self.update, self.draw) | |
def update(self): | |
# 現在選択されているシーンのupdateを読み込みます。 | |
self.my_gamemode = self.scene[self.my_gamemode].update() | |
def draw(self): | |
# こちらでは,画面の描画処理を行っています。 | |
pyxel.cls(0) # 一旦画面を真っ新にしています。前書いていた映像が残ってしまうので | |
self.scene[self.my_gamemode].draw() | |
if __name__ == "__main__": | |
App() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyxel | |
from scene_abc import AbstractScene | |
class TitleScene(AbstractScene): | |
def __init__(self): | |
super().__init__() | |
def update(self) -> bool: | |
# スペースキーを押したらスタートするようにする | |
if pyxel.btnp(pyxel.KEY_SPACE): | |
return self.GAMEMODE.Castle | |
return self.GAMEMODE.Title | |
def draw(self): | |
pyxel.bltm(x=0, y=0, tm=3, u=0, v=0, w=100, h=100) | |
pyxel.text(40, 45, "Title Scene!", 11) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment