今回は前回の「キャラを迷路の中でランダムに動かすのをじっと見てみた!笑 by python and pygame」の改良版として、一度通ったところは通らないようにしてみました。つまり、壁にぶつかるまで進み続けるプログラムになりました!
目的
キャラが自動で壁まで歩き続ける
使用したもの
・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)]
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()
# y += 1
# pygame.time.wait(1000)
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
print x, y
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
if len(preposi_Index) == 1:
print "------------------"
前回はmap上の0の位置だけ移動できるように
1. 現在地から前後左右の4つのマップ情報を取得する(関数:presentposion)
2. 4つの情報から、移動できる場所(0)の場所を取得し、複数の移動できる場所の中からランダムに1つ選びキャラを移動させた。
加えて、一度通った所を通らないようにすることで前回より進むようになった!
実験結果(動画)
キャラを迷路の中でランダムに動かすのをじっと見てみた!笑 by python and pygame Part2
感想
前回よりは進んだ!
ただこれ以上進まないので、一度通ったところでもある程度は戻れるようにしないと・・・
ということで、これからも少しずつキャラを賢くしていきたいと思います。
興味があれば、また次の記事をお待ちください!