# MIT License # # Copyright (c) 2026 Tidepost contributors # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # # Exception: assets/glyphs.json contains rasterized glyph data derived from # Noto Sans CJK JP (SIL Open Font License 1.1). See assets/NOTO-LICENSE.txt. # Pyxel is an external dependency under its own MIT license. """入力・更新・描画の共通入門 3/3。独立した練習画面。""" from dataclasses import dataclass, replace WIDTH, HEIGHT = 320, 180 FPS = 30 LIMIT = 90 @dataclass(frozen=True) class State: x: int = 20 tick: int = 0 phase: str = "play" def update(state, pressed, held): if "R" in pressed: return State() if "P" in pressed and state.phase in ("play", "pause"): return replace(state,phase="pause" if state.phase=="play" else "play") if state.phase != "play": return state x = max(0,min(WIDTH-8,state.x+2*(("RIGHT" in held)-("LEFT" in held)))) tick = state.tick+1 return State(x,tick,"done" if tick>=LIMIT else "play") class App: def __init__(self): self.state = State() def control(self, pressed, held): self.state = update(self.state,pressed,held) def draw(self, p): p.cls(1) p.text(8, 12, "GAME CLOCK / "+self.state.phase.upper(), 7) p.text(8, 35, "ACTIVE UPDATES: "+str(self.state.tick)+"/"+str(LIMIT), 10) p.text(8, 50, "SIMULATED SECONDS: "+format(self.state.tick/FPS,".1f"), 6) p.rect(self.state.x, 83, 8, 8, 11) p.text(8, 110, "FRAME COUNT IS NOT THE GAME CLOCK", 7) p.text(8, 138, "LEFT/RIGHT MOVE P PAUSE/RESUME", 6) p.text(8, 154, "R RESET (FIRST) ESC QUIT", 6) KEYS = ("LEFT", "RIGHT", "R", "P") def main(headless=False, screenshot=None, demo=False): from pathlib import Path import pyxel as p screenshot = str(Path(screenshot).resolve()) if screenshot else None if screenshot: Path(screenshot).parent.mkdir(parents=True,exist_ok=True) app = App() p.init(WIDTH,HEIGHT,title="Input / Update / Draw",fps=FPS,display_scale=3,headless=headless) keys = {key:getattr(p,"KEY_"+key) for key in KEYS} limit = 48 if demo else 12 def frame_update(): if headless and p.frame_count>=limit: if screenshot: p.screenshot(screenshot,scale=3) p.quit() return if headless and demo: f = p.frame_count pressed = {"P"} if f in (10,30) else {"RIGHT"} if f==0 else set() app.control(pressed,{"RIGHT"}|pressed) elif not headless: app.control({k for k,v in keys.items() if p.btnp(v)}, {k for k,v in keys.items() if p.btn(v)}) p.run(frame_update,lambda:app.draw(p)) return app if __name__=="__main__": import argparse parser=argparse.ArgumentParser() parser.add_argument("--headless-smoke",action="store_true") parser.add_argument("--demo-checkpoint",action="store_true") parser.add_argument("--screenshot") args=parser.parse_args() main(args.headless_smoke,args.screenshot,args.demo_checkpoint)