0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pygameを使って RPGを作る(23.NPC)

Posted at

NPCの作成

NPCを作成し、衝突した場合、会話するメッセージを表示するという簡単なもの

NPCクラス

from settings import * 
import pygame as pg
from utils import TextAnimation

class Communication:
    def __init__(self, parent):
        self.parent = parent

        self.x, self.y = self.get_draw_pos()
        self.font = pg.font.Font(FONT, MESSAGE_FONT_SIZE)
        self.speed = 200
        self.display_surface = pg.display.get_surface()
        self.text = TextAnimation(
            self.font, 
            (255,255,255),
            (0,0,255), 
            self.x - 90, 
            self.y + 50, 
            self.speed, 
            self.display_surface)
        
        self.description = 'ここは通れません。'
        self.counter = 0
        self.draw_flag = False

    def get_draw_pos(self):
        titlex = int(self.parent.player.rect.centerx / TILE_SIZE) * TILE_SIZE
        titley = int(self.parent.player.rect.centery / TILE_SIZE) * TILE_SIZE

        return titlex, titley

    def draw(self):
        pg.draw.rect(self.display_surface, '#8E7698', [self.x - 100, 
                                                       self.y + 40, 200-2, 50-4])
        pg.draw.rect(self.display_surface,'#D3DED0', [self.x - 100, 
                                                      self.y + 40, 200, 50],5,border_radius=5)

        flag, self.counter = self.text.draw_anime(self.description, self.counter)

        if flag and self.counter <= len(self.description) :
            self.counter += 1

        if self.counter > len(self.description): 
            self.draw_flag = True

MapにはNという文字でNPCを配置する

    "map_01": [
        'BBBBBBBBBBBBBBBBBBB',
        'B.................B',
        'B...............P.N',
        'B.................B',
        'B.................B',
        'B.....BBBB........B',
        'B.................B',
        'B.................B',
        'BBBBBBBBBBBBBBBBBBB',
    ],

Mainクラスはこんな感じで追加

def community(self, dt):
    if self.com_npc == None:
        self.com_npc = Communication(self)

    self.com_npc.draw()

残課題

複数のNPCと会話内容、イベントNPCとか動くNPCとかはとりあえず後回し。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?