python 迷路の作り方 -tkinter-

<H1>配列の1が壁、配列の0が床</H1>

```python
import tkinter
root = tkinter.Tk()
root.title("迷路")
canvas = tkinter.Canvas(width=800, height=560, bg="white")
canvas.pack()
maze = [
    [1,1,1,1,1,1,1,1,1,1],
    [1,0,0,0,0,0,1,0,0,1],
    [1,0,1,1,0,0,1,0,0,1],
    [1,0,0,1,0,0,0,0,0,1],
    [1,0,0,1,1,1,1,1,0,1],
    [1,0,0,0,0,0,0,0,0,1],
    [1,1,1,1,1,1,1,1,1,1]
]

for y in range(7): #縦に7回
    for x in range(10): #横に10回
        if maze[y][x] == 1: #次の配列に入るとxの値はリセットされる、yは80づつ足されてく
            canvas.create_rectangle(x*80,y*80,x*80+80,y*80+80, fill="gray")
root.mainloop()

```
![スクリーンショット 2022-07-18 16.14.01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1614609/a4bea102-67a7-86c6-27b8-551f0e52c4c7.png)

<H1>床をキャラクターが動く</H1>

```python
import tkinter

key = ""
def key_down(e):
    global key
    key = e.keysym

def key_up(e):
    global key
    key = ""

mx = 1
my = 1

def main_proc():
    global mx,my
    if key == "Up" and maze[my-1][mx] == 0:
        my = my - 1
    if key == "Down" and maze[my+1][mx] == 0:
        my = my + 1
    if key == "Left" and maze[my][mx-1] == 0:
        mx = mx - 1
    if key == "Right" and maze[my][mx+1] == 0:
        mx = mx + 1
    canvas.coords("MYCHR", mx*80+40, my*80+40)
    root.after(300,main_proc)

root = tkinter.Tk()
root.title("迷路移動する")
root.bind("<KeyPress>", key_down)
root.bind("<KeyRelease>", key_up)
canvas = tkinter.Canvas(width=800, height=560, bg="white")
canvas.pack()
maze = [
    [1,1,1,1,1,1,1,1,1,1],
    [1,0,0,0,0,0,1,0,0,1],
    [1,0,1,1,0,0,1,0,0,1],
    [1,0,0,1,0,0,0,0,0,1],
    [1,0,0,1,1,1,1,1,0,1],
    [1,0,0,0,0,0,0,0,0,1],
    [1,1,1,1,1,1,1,1,1,1]
]

for y in range(7):
    for x in range(10):
        if maze[y][x] == 1:
            canvas.create_rectangle(x*80,y*80,x*80+79,y*80+79, fill="gray", width=0)
img = tkinter.PhotoImage(file="yusha.png")
canvas.create_image(mx*80+40, my*80+40, image=img, tag="MYCHR")
main_proc()
root.mainloop()
```
![スクリーンショット 2022-07-18 18.12.38.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1614609/a2d51165-f805-02aa-cb65-7c33e9f51f4f.png)

<H1>床を塗る、クリアしたらメッセージを表示、やり直しができる</H1>

```python
import tkinter
import tkinter.messagebox

key = ""
def key_down(e):
    global key
    key = e.keysym

def key_up(e):
    global key
    key = ""

mx = 1
my = 1
yuka = 0
def main_proc():
    global mx,my,yuka
    if key == "Shift_L" and yuka > 1: #左のシフトを押すとやり直し
        canvas.delete("PAINT") #ピンクの画像を全て削除
        mx = 1 #キャラクターを初期位置に移動
        my = 1 #キャラクターを初期位置に移動
        yuka = 0 #床を塗った数を初期化
        for y in range(7): #床を塗った数を初期化
            for x in range(10):
                if maze[y][x] == 2:
                    maze[y][x] = 0
    if key == "Up" and maze[my-1][mx] == 0:
        my = my - 1
    if key == "Down" and maze[my+1][mx] == 0:
        my = my + 1
    if key == "Left" and maze[my][mx-1] == 0:
        mx = mx - 1
    if key == "Right" and maze[my][mx+1] == 0:
        mx = mx + 1
    if maze[my][mx] == 0:
        maze[my][mx] = 2
        yuka = yuka + 1
        canvas.create_rectangle(mx*80,my*80,mx*80+79,my*80+79,fill="pink",width=0, tag="PAINT") #タグづけして一度に全て削除
    canvas.delete("MYCHR") #キャラを消す
    canvas.create_image(mx*80+40, my*80+40, image=img, tag="MYCHR") #キャラを再描写
    if yuka == 30: #床を全部塗ったらダイアログを出す
        canvas.update()
        tkinter.messagebox.showinfo("クリア","全部塗り終わりました")
    else:
        root.after(300,main_proc)

root = tkinter.Tk()
root.title("迷路移動して塗る")
root.bind("<KeyPress>", key_down)
root.bind("<KeyRelease>", key_up)
canvas = tkinter.Canvas(width=880, height=560, bg="white")
canvas.pack()
maze = [
    [1,1,1,1,1,1,1,1,1,1,1],
    [1,0,0,0,0,0,1,0,0,1,1],
    [1,0,1,1,0,0,1,0,0,1,1],
    [1,0,0,1,0,0,0,0,0,1,1],
    [1,0,0,1,1,1,1,1,0,1,1],
    [1,0,0,0,0,0,0,0,0,1,1],
    [1,1,1,1,1,1,1,1,1,1,1]
]

for y, row in enumerate(maze):
    for x, cell in enumerate(row):
        if cell == 1:
            canvas.create_rectangle(x*80,y*80,x*80+79,y*80+79, fill="gray", width=0)
img = tkinter.PhotoImage(file="yusha.png")
canvas.create_image(mx*80+40, my*80+40, image=img, tag="MYCHR")
main_proc()
root.mainloop()
```
![スクリーンショット 2022-07-19 9.47.39.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1614609/766289ed-075d-1076-1b09-a758d3b155a6.png)