Python pygame writing text in sprite [closed]

2020-08-09 04:37发布

I am making a game where you shall shoot down diffrent boxes with diffrent nummber and text on and was wondring if you can write text as a sprite

1条回答
Lonely孤独者°
2楼-- · 2020-08-09 05:07

First off I feel like you haven't done your research, but are just song for free code. However I will answer your question, don't think that you will ever get this again.

class Text(pygame.sprite.Sprite):
    def __init__(self, text, size, color, width, height):
        # Call the parent class (Sprite) constructor  
        pygame.sprite.Sprite.__init__(self)

        self.font = pygame.font.SysFont("Arial", size)
        self.textSurf = self.font.render(text, 1, color)
        self.image = pygame.Surface((width, height))
        W = self.textSurf.get_width()
        H = self.textSurf.get_height()
        self.image.blit(self.textSurf, [width/2 - W/2, height/2 - H/2])

I hope that helps, this will draw text centered on surface in a sprite

查看更多
登录 后发表回答