1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

迷路作ってみた #100日チャレンジ に感化されて

Last updated at Posted at 2025-04-29

今日は迷路作ってみた!

Cell_SIZEとCOLS,ROWSの数字を変えるとより複雑化するけど,難易度設定ってどうやるんだろう:rolling_eyes:
壁を先に作ってランダムに掘り進めるという逆転の発想,考えた人すごいな!

#100日チャレンジ

タイトルなし.gif

import pygame
import sys
import random

# Pygame初期化
pygame.init()

# サイズ設定
CELL_SIZE = 40
COLS, ROWS = 15, 11
WIDTH, HEIGHT = COLS * CELL_SIZE, ROWS * CELL_SIZE
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("自動生成迷路")

# 色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)

# 迷路マップ初期化(全部壁)
maze = [[1 for _ in range(COLS)] for _ in range(ROWS)]

# プレイヤー初期位置
player_x, player_y = 1, 1

# ゴール位置
goal_x, goal_y = COLS - 2, ROWS - 2

# 迷路生成関数(深さ優先探索)
def generate_maze(x, y):
    maze[y][x] = 0  # 通路にする
    directions = [(0, -2), (2, 0), (0, 2), (-2, 0)]
    random.shuffle(directions)

    for dx, dy in directions:
        nx, ny = x + dx, y + dy
        if 1 <= nx < COLS-1 and 1 <= ny < ROWS-1 and maze[ny][nx] == 1:
            maze[y + dy//2][x + dx//2] = 0  # 間の壁も壊す
            generate_maze(nx, ny)

# 迷路を生成
generate_maze(1, 1)

# メインループ
clock = pygame.time.Clock()
running = True
while running:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # キー入力
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        if player_x > 0 and maze[player_y][player_x - 1] == 0:
            player_x -= 1
    if keys[pygame.K_RIGHT]:
        if player_x < COLS-1 and maze[player_y][player_x + 1] == 0:
            player_x += 1
    if keys[pygame.K_UP]:
        if player_y > 0 and maze[player_y - 1][player_x] == 0:
            player_y -= 1
    if keys[pygame.K_DOWN]:
        if player_y < ROWS-1 and maze[player_y + 1][player_x] == 0:
            player_y += 1

    # ゴール到達判定
    if player_x == goal_x and player_y == goal_y:
    
        # ゴールメッセージを表示
        font = pygame.font.SysFont(None, 80)  # フォントサイズ80
        text = font.render("Goal", True, (255, 0, 0))  # 赤色の文字
        text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))  # 中央に配置
    
        screen.fill(WHITE)  # 一度背景を白く塗る
        screen.blit(text, text_rect)  # メッセージを画面に貼る
        pygame.display.flip()  # 画面更新
    
        pygame.time.delay(2000)  # 2秒待つ
    
        # 新しい迷路にリセット
        maze = [[1 for _ in range(COLS)] for _ in range(ROWS)]  # 全壁にリセット
        generate_maze(1, 1)  # 新しい迷路生成
        player_x, player_y = 1, 1
        goal_x, goal_y = COLS - 2, ROWS - 2
        
        continue
    
    # 描画
    screen.fill(WHITE)

    for y in range(ROWS):
        for x in range(COLS):
            if maze[y][x] == 1:
                pygame.draw.rect(screen, BLACK, (x*CELL_SIZE, y*CELL_SIZE, CELL_SIZE, CELL_SIZE))

    # プレイヤー
    pygame.draw.rect(screen, BLUE, (player_x*CELL_SIZE, player_y*CELL_SIZE, CELL_SIZE, CELL_SIZE))

    # ゴール
    pygame.draw.rect(screen, GREEN, (goal_x*CELL_SIZE, goal_y*CELL_SIZE, CELL_SIZE, CELL_SIZE))

    pygame.display.flip()
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?