今回はpythonとpygameを用いて簡単な迷路ゲームを作ってみました!
キャラを十字キーで操作し、壁には当たり判定をつけて進めなくしました・
目的
迷路ゲームの作成
使用したもの
・python2.7
・pygame1.9.4
・ブログ「人工知能に関する断層録」
では実験していきます!
当たり判定
当たり判定は下記ソースコード内の関数、is_movableで実装しています。
処理1
十字キーが押された時に押した先の座標がマップの範囲外であれば動かない、elseであれば進む。
if x < 0 or x > COL-1 or y < 0 or y > ROW-1:
return False
処理2
十字キーが押された時に押した先の座標が壁(壁==1)であれば動かない、、else(道==0)であれば進む。
# マップチップは移動可能か?
if map[y][x] == 1:
return False
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
import sys
import os
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
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 # プレイヤーの位置(単位:マス)
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()
# プレイヤーの移動処理
if event.type == KEYDOWN and event.key == K_DOWN:
if is_movable(x, y+1):
y += 1
if event.type == KEYDOWN and event.key == K_LEFT:
if is_movable(x-1, y):
x -= 1
if event.type == KEYDOWN and event.key == K_RIGHT:
if is_movable(x+1, y):
x += 1
if event.type == KEYDOWN and event.key == K_UP:
if is_movable(x, y-1):
y -= 1
実行結果
まとめ
今回はpygameで迷路ゲームを作成しました!
簡単に楽しめるゲームができるのは作っていて楽しいです。
次は機械学習HTM(Hierarchical Temporal Memory)を用いて迷路ゲームを解かせてみたいと思います!