2017-12-17 15 views
1

私はPygameのスプライトで1行のテキストをレンダリングすることができますが、複数行のテキストをレンダリングすることもできますが、スプライトは使用できません。 Spriteを使って複数行のテキストをレンダリングする方法。Pygame - スプライトを使った複数行テキストのレンダリング

私は(私はpygameのコード実行するための基本的なをスキップするつもりです - 背景に、initなど)スプライトを使用して1行のテキストを描画するためにこれを持っている:

class Score(pygame.sprite.Sprite): 
    . 
    . 
    def update(self, lives, score): 
     . 
     . 
     self.text = "LIVES: %d SCORE: %d" % (lives, score) 
     self.image = self.font.render(self.text, True, self.color) 
     self.rect = self.image.get_rect() 
     . 
     . 

. 
. 
def main(): 
    . 
    . 
    score = pygame.sprite.RenderUpdates() 
    score.add(Score()) 
    . 
    . 
    while 1: 
     . 
     . 
     score.clear(screen, background) 
     score_list = score.draw(screen) 
     pygame.display.update(score_list) 

     score.update(lives, score) 
    . 
    . 

を、私はちょうどそれはだかどうかを知りたいです私のレンダリングメソッドを使用してそれを行うことさえできますか、それとも何か別の方法で行うことに集中すべきかどうか?

おそらく1つの関連する質問です。この方法はPygameでオブジェクト(画像)をレンダリングする正しい方法ですか?

ありがとうございました。

+0

人生とスコアだけをレンダリングしたい場合は、2つのテキストサーフェスを別々に作成してblitするのが最も簡単な解決策だと思います。 – skrx

+0

それは本当ですが、それは私がスプライトなしで言及したように私がそれを行う方法です、そして、私はそうしたくありません。プログラム/ゲームは実際かなり複雑で、私は他のオブジェクトをこのようにレンダリングしています。それはおそらくそれを遅くするでしょう。 – mikro45

答えて

0

文字列を最初にFONT.renderでレンダリングしてから、透明なサーフェスを作成し、別々のテキストサーフェスをblitすることができます。透明ベースイメージの幅を確認するには、別のテキストサーフェスの.get_width()メソッドを使用し、高さはFONT.get_height()です。

import pygame as pg 


pg.init() 
screen = pg.display.set_mode((640, 480)) 
clock = pg.time.Clock() 
FONT = pg.font.Font(None, 40) 
BLUE = pg.Color('dodgerblue1') 


class Score(pg.sprite.Sprite): 

    def __init__(self, pos): 
     super(Score, self).__init__() 
     self.lives = 3 
     self.score = 0 
     self.rect = pg.Rect(pos, (1, 1)) 
     self.update_image() 

    def update_image(self): 
     height = FONT.get_height() 
     # Put the rendered text surfaces into this list. 
     text_surfaces = [] 
     for txt in ('lives {}'.format(self.lives), 
        'score {}'.format(self.score)): 
      text_surfaces.append(FONT.render(txt, True, BLUE)) 
     # The width of the widest surface. 
     width = max(txt_surf.get_width() for txt_surf in text_surfaces) 

     # A transparent surface onto which we blit the text surfaces. 
     self.image = pg.Surface((width, height*2), pg.SRCALPHA) 
     for y, txt_surf in enumerate(text_surfaces): 
      self.image.blit(txt_surf, (0, y*height)) 
     # Get a new rect (if you maybe want to make the text clickable). 
     self.rect = self.image.get_rect(topleft=self.rect.topleft) 


def main(): 
    score = Score((100, 100)) 
    all_sprites = pg.sprite.Group(score) 

    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 
      elif event.type == pg.KEYDOWN: 
       if event.key == pg.K_s: 
        # Increment the score and update the image. 
        # It would be nicer to turn the score into a 
        # property and update the image automatically. 
        score.score += 1 
        score.update_image() 

     all_sprites.update() 
     screen.fill((30, 30, 30)) 
     all_sprites.draw(screen) 

     pg.display.flip() 
     clock.tick(30) 


if __name__ == '__main__': 
    main() 
    pg.quit() 
+1

ご協力いただきありがとうございます。出来た。 "Surface.blit(サーフェス、矩形)"を使うことができないことは分かりませんでした。 – mikro45

関連する問題