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を作る(20. 戦闘画面:文字Fontと戦闘メッセージの背景調整とHPバー表示)

Posted at

概要

・文字Fontを調整
・戦闘メッセージのアニメーション処理については、ChatGPTを参考に難しい実装をしていましたが、あるサイトには、もう少し簡単の実装でできることが判明したので、不要なアニメーション処理のフラグをやめました。ただし、メッセージの表示の調整には、もう少し細かい課題があります。
・HPバー表示を追加(これを追加するとだんだんRPGっぽい感じになる)

image.png
黒基調にしておくと文字が見やすいことが分かったが、日本語の漢字で画数が多いとやはり見ずらい面がある

改良後のTextAnimationクラス

class TextAnimation(pg.sprite.Sprite):
    def __init__(self, font, fore_color, bg_color, x, y, speed, screen):
        super().__init__()
        self.font = font
        self.color = fore_color
        self.bg_color = bg_color
        self.x = x
        self.y = y
        self.speed = speed
        self.screen = screen
        
    def draw(self, fixed_texts, counter):
        """現在の状態を描画"""
        self.fixed_texts = fixed_texts
        last_line = len(self.fixed_texts)
        # すでに表示された固定テキストを描画
        for i, text in enumerate(self.fixed_texts):
            if i == last_line -1:
                text_surface = self.font.render(text[0:counter//self.speed], True, self.color)
                self.screen.blit(text_surface, (self.x, self.y + i * 32))
            else:
                text_surface = self.font.render(text, True, self.color)
                self.screen.blit(text_surface, (self.x, self.y + i * 32))

PlayerStatusクラス

import pygame as pg
from utils import TextSprite 
from settings import *
from player_data import *

class PlayerStatus(pg.sprite.Sprite):
    def __init__(self, bg_size, battle_sprites, *groups):
        super().__init__(*groups)

        self.offset = pg.Vector2()
        self.offset.x = 231
        self.offset.y = 0
        
        self.bg_size = bg_size
        self.view_status = {
            "name": NAME,
            "job": JOB,
            "HP": HP,
            "MP": MP,
            "LV": LV
        }
        self.infact_status = {
            'ATK': 30,
            'DEF': 25
        }

        self.font = pg.font.Font(FONT, 20)
        self.battle_sprites = battle_sprites

    def draw_bar(self):
        self.bar_rect = pg.FRect(self.offset.x + self.bg_size[0] + 110,
                                 self.offset.y + 95, 100,15)
        pg.draw.rect(self.screen, (RED), self.bar_rect)
        ratio = self.bar_rect.width / HP
        val = self.view_status['HP']
        progress_rect = pg.FRect(self.bar_rect.topleft, (val * ratio, self.bar_rect.height))
        pg.draw.rect(self.screen, (GREEN), progress_rect)

        pg.draw.rect(self.screen, (WHITE), self.bar_rect, 2, border_radius=2)

    def draw_status(self, screen):
        # 枠
        self.p_area = pg.Rect(
            0,
            0, 
            self.offset.x -2,
            HEIGHT -2)
        self.surface = pg.Surface(self.p_area.size, pg.SRCALPHA)
        self.surface.fill((52, 50, 30, 190))
        self.screen = screen
        self.screen.blit(self.surface,
                        [
                            self.p_area.x + 2 + self.offset.x + self.bg_size[0],
                            self.p_area.y + 2 + self.offset.y,
                            self.p_area.width -2,
                            self.p_area.height -2
                        ])
        y_pos = []
        span = 30
        for i in range(1,6):
            y_pos.append(self.offset.y + span * i)

        for (k,v), y in zip(self.view_status.items(), y_pos):
            text_sprite = TextSprite(
                k + ' : ' + str(v), 
                self.font, 
                (255,255,255),(0,0,0), 
                self.offset.x + self.bg_size[0] + span - 20,
                y, self.battle_sprites)
            text_sprite.draw(screen)

        pg.draw.rect(self.screen,
                    '#D3DED0',
                    [
                        self.p_area.x + self.offset.x + self.bg_size[0],
                        self.offset.y,
                        self.offset.x,
                        int(HEIGHT )
                    ], 
                    3, 
                    border_radius=5)
        
        self.draw_bar()

反省点

ChatGPTはそれなりに優れてはいるが、やはりうまく使いこなせるには、経験値が必要で、間違っている指示だといつまでもその場グルグル回って、逆に無駄な実装が多い

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?