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/9a76abe519033bb3df86e85673d2ec50 to your computer and use it in GitHub Desktop.

Select an option

Save kujirahand/9a76abe519033bb3df86e85673d2ec50 to your computer and use it in GitHub Desktop.
Maze for Thumb
import thumby
import random
import time
# 描画フォントの設定
FW, FH = 5, 7
thumby.display.setFont("/lib/font5x7.bin", FW, FH, 1)
# FPSを指定
thumby.display.setFPS(8)
# 定数の指定
cur = {}
flag_title = True
maze = []
game = {"x": 1, "y": 1}
MAZE_SIZE = 51
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"] += FH
def init_game():
global maze
maze = make_maze()
game["x"] = 1
game["y"] = 1
# メインループ
def main_loop():
global flag_title
while True:
clear_screen()
if flag_title:
echo("--------")
echo("MAZE")
echo("--------")
echo(f"A:Quit")
echo(f"B:Start")
if (thumby.buttonB.justPressed()):
flag_title = False
clear_screen()
init_game()
if(thumby.buttonA.justPressed()):
thumby.reset() # ゲームを終了する
thumby.display.update()
continue
draw_screen()
move_player()
if(thumby.buttonA.justPressed()):
thumby.reset() # ゲームを終了する
# 画面を更新(重要)
thumby.display.update()
def move_player():
nx = game["x"]
ny = game["y"]
if(thumby.buttonU.pressed()):
ny -= 1
if(thumby.buttonD.pressed()):
ny += 1
if(thumby.buttonL.pressed()):
nx -= 1
if(thumby.buttonR.pressed()):
nx += 1
if 0 <= nx < MAZE_SIZE and 0 <= ny < MAZE_SIZE:
v = maze[ny][nx]
if v == 0:
game["x"] = nx
game["y"] = ny
def draw_screen():
for y in range(5):
for x in range(7):
cur["x"] = x * FW
cur["y"] = y * FH
if x == 3 and y == 2:
echo("o") # player
continue
dx = game["x"] + x - 3
dy = game["y"] + y - 2
if dx < 0 or dy < 0 or dx >= MAZE_SIZE or dy >= MAZE_SIZE:
echo("*")
continue
v = maze[dy][dx]
if v:
echo("*")
thumby.display.drawRectangle(0, 0, FW*7, FH*5, 1)
cur["x"] = FW * 8
cur["y"] = 0
echo("MAZE")
echo(f"x:{game["x"]}")
echo(f"y:{game["y"]}")
if (game["x"] == MAZE_SIZE - 2) and (game["y"] == MAZE_SIZE - 2):
echo("GOAL!")
def shuffle(a):
"""シャッフル"""
for i in range(len(a) - 1, 0, -1):
j = random.randrange(i + 1)
a[i], a[j] = a[j], a[i]
def make_maze(width=MAZE_SIZE, height=MAZE_SIZE):
"""迷路をランダムに作成"""
# 穴掘り法は奇数サイズが扱いやすい
if width % 2 == 0:
width += 1
if height % 2 == 0:
height += 1
# 1=壁, 0=道
maze = [[1 for _ in range(width)] for _ in range(height)]
dirs = [
(0, -2),
(0, 2),
(-2, 0),
(2, 0),
]
# 開始位置
sx, sy = 1, 1
maze[sy][sx] = 0
stack = [(sx, sy)]
while stack:
x, y = stack[-1]
# 掘れる方向を集める
candidates = []
for dx, dy in dirs:
nx = x + dx
ny = y + dy
if 1 <= nx < width - 1 and 1 <= ny < height - 1:
if maze[ny][nx] == 1:
candidates.append((dx, dy))
if candidates:
shuffle(candidates)
dx, dy = candidates[0]
nx = x + dx
ny = y + dy
# 間の壁を掘る
maze[y + dy // 2][x + dx // 2] = 0
maze[ny][nx] = 0
stack.append((nx, ny))
else:
# もう掘れないので戻る
stack.pop()
return maze
# メイン
main_loop()
@kujirahand

Copy link
Copy Markdown
Author

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