人工知性を作りたい

私が日々、挑戦したことや学んだことなどを紹介していく雑記ブログです。 (新しいAI技術HTM, 専門の音声信号処理, 趣味のアニメ等も書いてます。)

pythonによる迷路の自動探索 手当たり次第編!

f:id:hiro-htm877:20190406210329p:plain



今回は前回の「キャラを迷路の中でランダムに動かすのをじっと見てみた!笑 by python and pygame Part2」の改良版として、壁にぶつかったらぶつかった位置を壁と設定して、またランダムで移動させるシステムを作成した! 

目的

キャラが自動で迷路をクリアする

 

 

使用したもの

・python2.7

・pygame1.9.4

 

では、実験!

 

 

実験結果(動画)


pythonによる迷路の自動探索 手当たり次第編!

 

前回はmap上の0の位置だけ移動できるように

1. 現在地から前後左右の4つのマップ情報を取得する(関数:presentposion)

2. 4つの情報から、移動できる場所(0)の場所を取得し、複数の移動できる場所の中からランダムに1つ選びキャラを移動させた。

3. 一度通った所を通らないようにした  

感想

今回はゴールを設定してなかったですがゴールを設定して、一時間ぐらい走らせといたらゴールするかも・・・

とりあえずゴール出来そうにはなってきました笑!

ということで、これからも少しずつキャラを賢くしていきたいと思います。

興味があれば、また次の記事をお待ちください!

 

 

ps. 少しバグみたいなのもありますがそれも修正して行きたいと思います。

(通れないところに隙間ができている)

 

 

 

ソースコード 

# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
import sys
import os
import random

SCR_RECT = Rect(0, 0, 640, 640)
ROW,COL = 20,20
GS = 32

map = [
	[0, 0, 1, 0, 1, 0, 0, 0 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,0],
	[0, 1, 0, 0, 0, 1, 1, 1 ,0 ,1 ,0 ,1 ,1 ,0 ,1 ,1 ,1 ,0 ,1 ,0],
	[0, 0, 1, 1, 0, 0, 0, 1 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,0],
	[1, 0, 1, 0, 1, 1, 0, 0 ,0 ,1 ,1 ,1 ,1 ,1 ,0 ,0 ,1 ,0 ,1 ,0],
	[0, 0, 0, 0, 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0, 1, 0, 0, 0, 1, 0, 1 ,1 ,1 ,0 ,0 ,1 ,1 ,0 ,1 ,0 ,0 ,0 ,0],
	[0, 0, 0, 1, 0, 0, 0, 1 ,1 ,1 ,1 ,0 ,0 ,0 ,1 ,1 ,1 ,1 ,1 ,0]
]

def load_image(filename, colorkey=None):
    filename = os.path.join("data", filename)
    try:
        image = pygame.image.load(filename)
    except pygame.error, message:
        print "Cannot load image:", filename
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image

def draw_map(screen):
    """マップを描画する"""
    for r in range(ROW):
        for c in range(COL):
            if map[r][c] == 0:
                screen.blit(grassImg, (c*GS,r*GS), (0,128,GS,GS))
            elif map[r][c] == 1:
                screen.blit(waterImg, (c*GS,r*GS), (0,128,GS,GS))

def is_movable(x, y):
    """(x,y)は移動可能か?"""
    # マップ範囲内か?
    if x < 0 or x > COL-1 or y < 0 or y > ROW-1:
        return False
    # マップチップは移動可能か?
    if map[y][x] == 1:  # 水は移動できない
        return False
    return True
def presentposition(x, y): ### mapの端にぶつかった時の処理 preposi = [0 for i in range(4)] if x == COL-1: preposi[0] = -1 else: preposi[0] = map[y][x+1] if x == 0: preposi[1] = -1 else: preposi[1] = map[y][x-1] if y == ROW-1: preposi[2] = -1 else: preposi[2] = map[y+1][x] if y == 0: preposi[3] = -1 else: preposi[3] = map[y-1][x] return preposi pygame.init() screen = pygame.display.set_mode(SCR_RECT.size) pygame.display.set_caption("PyRPG プレイヤーの移動") # イメージロード playerImg = load_image("chara_tip/pipo-charachip005b.png", -1) # プレイヤー grassImg = load_image("pipo-map001/640x480/pipo-map001_at-tuti.png") waterImg = load_image("pipo-map001/640x480/pipo-map001_at-sabaku.png") x,y = 0,0 # プレイヤーの位置(単位:マス) ylen = len(map) xlen = len(map[0]) createmap = [[0 for j in range(xlen)] for i in range(ylen)] Px = 0 Py = 0 while True: draw_map(screen) # マップ描画 screen.blit(playerImg, (x*GS,y*GS), (0,0,GS,GS)) # プレイヤー描画 pygame.display.update() for event in pygame.event.get(): print "\n" if event.type == QUIT: sys.exit() if event.type == KEYDOWN and event.key == K_ESCAPE: sys.exit() preposi = presentposition(x, y) print preposi preposi_Index = [i for i, j in enumerate(preposi) if j == 0] print preposi_Index nextposi = random.choice(preposi_Index) print "Px, Py", Px, Py print "x, y", x, y if len(preposi_Index) == 1: print "------------------" print "STOP!!!" map[y][x] = -1 x, y = Px, Py pygame.time.wait(100) ###追加中 preposi = presentposition(x, y) print preposi preposi_Index = [i for i, j in enumerate(preposi) if j == 0] print preposi_Index nextposi = random.choice(preposi_Index) pygame.time.wait(100) print "nextposi", nextposi if nextposi == 0: if x+1 != Px: Px = x Py = y x += 1 pygame.time.wait(100) elif nextposi == 1: if x-1 != Px: Px = x Py = y x -= 1 pygame.time.wait(100) elif nextposi == 2: if y+1 != Py: Px = x Py = y y += 1 pygame.time.wait(100) elif nextposi == 3: if y-1 != Py: Px = x Py = y y -= 1 pygame.time.wait(100) print "preposi_Index", preposi_Index