鍵と扉
CoddyのPygameジャーニー「プロジェクト」セクションの一部。レッスン 23/31。
右側の壁にあるドアは鍵がかかっています。騎士はその上を歩いて鍵を拾い、鍵を持っていると、ドアに触れたときに開きます。
鍵のかかったドアも、騎士の進路を塞ぐ障害物のひとつです。Roomのメソッドは、騎士を止めるRectのリストを返します。
def blocks(self):
if self.door_open:
return self.walls
return self.walls + [self.door]ドアに触れるときには注意点があります。moveは毎フレーム騎士をドアの外へ押し出すため、騎士のRectがドアと重なることはありません。knight.rect.inflate(8, 8)は騎士のRectを各辺4ピクセルずつ大きくしたコピーで、騎士が押しつけられているドアとこのコピーは重なります。
if room.has_key and knight.rect.inflate(8, 8).colliderect(room.door):
room.door_open = Trueドアの先はウィンドウの端なので、clamp_ipを使って騎士が画面内にとどまるようにします。後のレッスンでは、ドアを通って次の部屋へ進みます。鍵を持っている間は、画面の上部に鍵を表示しましょう。
チャレンジ
中級Roomで、self.has_key = Falseとself.door_open = Falseを設定します。Kがある場合はself.key = cell_rect(col, row)を設定し、Dがある場合は、そのタイルの48×48のRectをself.doorに設定します。- 上記の
blocksメソッドを追加し、knight.move(dx, dy, room.blocks())で騎士を移動させます。 - 移動後に
knight.rect.clamp_ip(screen.get_rect())を実行します。騎士が鍵を持っておらず、room.keyに触れたら、room.has_key = Trueを設定します。その後、上記のとおりドアを開きます。 - ドアが閉じている間は
screen.blit(door_img, room.door)でドアを描画し、鍵を拾うまではdraw(key_img, room.key)で鍵を描画します。また、room.has_keyがtrueの間は、(136, 6)にkey_imgを描画します。
自分で試してみよう
import pygame
TILE = 48
MAP = [
"##############",
"#............#",
"#.P......K...#",
"#............#",
"#....##......D",
"#....##......#",
"##.#######.###",
"#............#",
"#...S....H...#",
"##############",
]
pygame.init()
screen = pygame.display.set_mode((672, 480))
pygame.display.set_caption("Rune Keep")
clock = pygame.time.Clock()
font = pygame.font.Font("assets/fonts/pixel.ttf", 32)
def load(name):
return pygame.image.load("assets/images/" + name).convert_alpha()
floor_img = load("floor.png")
wall_img = load("wall.png")
knight_img = load("knight.png")
door_img = load("dungeon_door.png")
key_img = load("key.png")
slime_img = load("slime.png")
spider_img = load("spider.png")
heart_img = load("heart.png")
empty_img = load("heart_empty.png")
sword_img = load("sword.png")
potion_img = load("potion_red.png")
def draw(image, rect):
screen.blit(image, image.get_rect(center=rect.center))
def cell_rect(col, row):
rect = pygame.Rect(0, 0, 32, 32)
rect.center = (col * TILE + 24, row * TILE + 24)
return rect
class Room:
def __init__(self, lines):
self.walls = []
self.start = (72, 72)
for row, line in enumerate(lines):
for col, char in enumerate(line):
if char == "#":
self.walls.append(pygame.Rect(col * TILE, row * TILE, TILE, TILE))
elif char == "P":
self.start = cell_rect(col, row).center
class Knight(pygame.sprite.Sprite):
def __init__(self, center):
super().__init__()
self.image = knight_img
self.rect = pygame.Rect(0, 0, 32, 32)
self.rect.center = center
def move(self, dx, dy, blocks):
self.rect.x += dx
for block in blocks:
if self.rect.colliderect(block):
if dx > 0:
self.rect.right = block.left
elif dx < 0:
self.rect.left = block.right
self.rect.y += dy
for block in blocks:
if self.rect.colliderect(block):
if dy > 0:
self.rect.bottom = block.top
elif dy < 0:
self.rect.top = block.bottom
room = Room(MAP)
knight = Knight(room.start)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
dx = 0
dy = 0
if keys[pygame.K_LEFT]:
dx = -3
if keys[pygame.K_RIGHT]:
dx = 3
if keys[pygame.K_UP]:
dy = -3
if keys[pygame.K_DOWN]:
dy = 3
knight.move(dx, dy, room.walls)
for row in range(10):
for col in range(14):
screen.blit(floor_img, (col * TILE, row * TILE))
for wall in room.walls:
screen.blit(wall_img, wall)
draw(knight.image, knight.rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()
プロジェクトのすべてのレッスン
自分で練習してみよう: Pygameプレイグラウンド