人工知性を作りたい

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

キャラを迷路の中でランダムに動かすのをじっと見てみた!笑 by python and pygame

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

今回は迷路ゲームを機械学習で解いて見た!の前段階として、キャラをランダムに動かしてみるとどうなるのかをじっと観察して見ました。

 

目的

キャラが迷路をランダムに動いているのを観察する

 

 

使用したもの

・python2.7

・pygame1.9.4

 

では、実験!

 

ソースコード 

# -*- 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, 0, 1, 1, 1 ,0 ,1 ,0 ,0 ,0 ,0 ,1 ,1 ,0 ,1 ,1 ,0],
	[0, 1, 1, 1, 0, 0, 0, 0 ,0 ,1 ,0 ,1 ,1 ,1 ,0 ,1 ,0 ,0 ,0 ,0],
	[0, 1, 1, 1, 0, 1, 1, 1 ,1 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,0],
	[0, 0, 0, 1, 0, 0, 0, 0 ,1 ,0 ,0 ,1 ,0 ,1 ,1 ,0 ,0 ,0 ,1 ,0],
	[1, 1, 0, 1, 1, 1, 1, 1 ,1 ,0 ,1 ,1 ,0 ,0 ,1 ,1 ,1 ,0 ,1 ,1],
	[1, 0, 0, 0, 0, 0, 1, 1 ,0 ,0 ,0 ,0 ,1 ,0 ,1 ,1 ,0 ,0 ,1 ,0],
	[1, 0, 1, 1, 1, 0, 0, 0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,0 ,0],
	[1, 0, 1, 0, 1, 1, 1, 0 ,1 ,0 ,1 ,1 ,0 ,1 ,1 ,0 ,0 ,0 ,0 ,1],
	[0, 0, 1, 0, 0, 1, 0, 0 ,1 ,0 ,0 ,1 ,0 ,1 ,0 ,1 ,1 ,1 ,0 ,0],
	[0, 1, 1, 1, 0, 1, 0, 1 ,0 ,0 ,1 ,1 ,0 ,1 ,0 ,1 ,1 ,0 ,1 ,0],
	[0, 0, 0, 1, 0, 1, 0, 0 ,1 ,0 ,1 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0],
	[1, 1, 0, 1, 0, 1, 0, 1 ,1 ,0 ,0 ,1 ,0 ,1 ,1 ,0 ,1 ,1 ,1 ,0],
	[0, 0, 0, 1, 0, 1, 1, 1 ,1 ,1 ,0 ,1 ,0 ,1 ,1 ,0 ,0 ,0 ,1 ,0],
	[0, 1, 1, 1, 0, 1, 0, 0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,1 ,1 ,0 ,1 ,1],
	[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):
	preposi = [0 for i in range(4)]

	if x == COL:
		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:
		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)]

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():
        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)
	if nextposi == 0:
		x += 1
		pygame.time.wait(100)
	elif nextposi == 1:
		x -= 1
		pygame.time.wait(100)
	elif nextposi == 2:
		y += 1
		pygame.time.wait(100)
	elif nextposi == 3:
		y -= 1
		pygame.time.wait(100)
    print x,y

 

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

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

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

 

実験結果(動画)

www.youtube.com

 

 

感想

全然進まねぇ〜

同じところをグルグルしてる〜

 

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

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