Skip to content

Instantly share code, notes, and snippets.

@kujirahand
Last active June 4, 2026 01:16
Show Gist options
  • Select an option

  • Save kujirahand/a0777c525bf2df55e89b22da02eb5c08 to your computer and use it in GitHub Desktop.

Select an option

Save kujirahand/a0777c525bf2df55e89b22da02eb5c08 to your computer and use it in GitHub Desktop.
LifeGame for Thumby
import time
import thumby
import random
# 定数の定義
DISP_W = thumby.display.width # 72
DISP_H = thumby.display.height # 40
# セルのサイズと個数を定義
CW, CH = 3, 3
COLS,ROWS = DISP_W // CW, DISP_H // CH
# 相対座標の定義
REL_XY = [
(-1, -1), ( 0, -1), ( 1, -1),
(-1, 0), ( 1, 0),
(-1, 1), ( 0, 1), ( 1, 1),
]
# 描画フォントの設定
thumby.display.setFont("/lib/font5x7.bin", 5, 7, 1)
# FPSを指定
thumby.display.setFPS(8)
# ゲーム定数の指定
flag_title = True
cells = []
cur = {}
def clear_screen():
"""画面をクリア"""
thumby.display.fill(0)
cur["x"] = 0
cur["y"] = 0
def echo(text):
"""画面出力"""
thumby.display.drawText(text, cur["x"], cur["y"], 1)
cur["y"] += 7
def init_game():
"""ゲームを初期化"""
global cells
cells = [[random.randint(0, 1) for _ in range(COLS)] for _ in range(ROWS)]
def draw_cells():
"""セルを描画"""
clear_screen()
for y, row in enumerate(cells):
yy = y * CH
for x, v in enumerate(row):
xx = x * CW
if v:
thumby.display.drawFilledRectangle(xx, yy, CW-1, CH-1, 1)
def count_life(x, y):
"""周囲八方向の生物の数を数える"""
cnt = 0
for p in REL_XY:
xx, yy = x + p[0], y + p[1]
if xx < 0 or yy < 0 or xx >= COLS or yy >= ROWS:
continue
if cells[yy][xx]:
cnt += 1
return cnt
def next_generation():
"""次の世代へ"""
global cells
cells2 = [[0 for _ in range(COLS)] for _ in range(ROWS)]
for y in range(ROWS):
for x in range(COLS):
n = count_life(x, y)
v = cells[y][x]
if v == 0:
if n == 3:
cells2[y][x] = 1
elif 2 <= n <= 3:
cells2[y][x] = 1
cells = cells2
# メインループ
while True:
clear_screen()
if flag_title: # タイトルを表示
echo("------------")
echo("* LIFEGAME *")
echo("------------")
echo(f"A:Quit B:GO")
if(thumby.buttonB.justPressed()):
flag_title = False
init_game()
thumby.display.update()
continue
draw_cells()
next_generation()
# ボタンの状態を確認
if (thumby.buttonB.justPressed()):
init_game() # セルを初期化
if(thumby.buttonA.justPressed()):
thumby.reset() # ゲームを終了する
# 画面を更新(重要)
thumby.display.update()
@kujirahand

Copy link
Copy Markdown
Author

DICE

import time
import thumby
import random

# 定数の定義
DISP_W = thumby.display.width  # 72
DISP_H = thumby.display.height # 40
# 描画フォントの設定
thumby.display.setFont("/lib/font8x8.bin", 8, 8, 1)
# FPSを指定
thumby.display.setFPS(8)
# ゲーム定数の指定
cur = {}

def clear_screen():
    """画面をクリア"""
    thumby.display.fill(0)
    cur["x"] = 0
    cur["y"] = 0

def echo(text):
    """画面出力"""
    thumby.display.drawText(text, cur["x"], cur["y"], 1)
    cur["y"] += 8

# メインループ
r = random.randint(1, 6)
while True:
    clear_screen()
    echo("--------")
    echo(f"DICE:[{r}]")
    echo("--------")
    echo(f"A:Quit")
    echo(f"B:Next")
    # ボタンの状態を確認
    if thumby.buttonB.justPressed() or thumby.buttonU.justPressed() or thumby.buttonD.justPressed():
        thumby.display.fill(1)
        thumby.display.update()
        time.sleep_ms(80)
        thumby.display.fill(0)
        thumby.display.update()
        r = random.randint(1, 6)
    if(thumby.buttonA.justPressed()):
        thumby.reset() # ゲームを終了する
    # 画面を更新(重要)
    thumby.display.update()

@kujirahand

Copy link
Copy Markdown
Author

Hello, World!

import thumby

# 描画フォントの設定
thumby.display.setFont("/lib/font8x8.bin", 8, 8, 1)

# FPSを指定
thumby.display.setFPS(8)
# 定数の指定
cur = {}

def clear_screen():
    """画面をクリア"""
    thumby.display.fill(0)
    cur["x"] = 0
    cur["y"] = 0

def echo(text):
    """画面出力"""
    thumby.display.drawText(text, cur["x"], cur["y"], 1)
    cur["y"] += 8

# メインループ
while True:
    clear_screen()
    echo("--------")
    echo("Hello,")
    echo("  World!")
    echo("--------")
    echo(f"A:Quit")
    if(thumby.buttonA.justPressed()):
        thumby.reset() # ゲームを終了する
    # 画面を更新(重要)
    thumby.display.update()

@kujirahand

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment