文本无法显示 - Python(PyGame)

时间:2017-05-26 01:01:34

标签: python pygame

问题

所以,我正在尝试渲染保存玩家得分的文本,但是当我编写代码时,它不起作用。

Python代码

class Player(pygame.sprite.Sprite):
    def __init__(self, x, color):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface((10, 100))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.y = HEIGHT / 2 - self.rect.height / 2
        self.rect.centerx = x
        self.displacement = 8
        self.score = 0

    def update(self):
        # update function

        self.constrain(0, HEIGHT - self.rect.height)

    def constrain(self, min, max):
        # constrian player to walls

        if self.rect.y > max:
            self.rect.y = max
        if self.rect.y < min:
            self.rect.y = min


class Text():
    def __init__(self, x, y, color, text, font):
        self.x = x
        self.y = y
        self.color = color
        self.text = text
        self.font = font

    def draw(self):
        text = self.font.render(self.text, False, self.color)
        text.blit(text, (self.x, self.y))

class Game(pygame.sprite.Sprite):
    def __init__(self):
        # get sprite object
        pygame.sprite.Sprite.__init__(self)

        # init pygame
        pygame.init()

        # init pygame modules
        pygame.mixer.init()
        pygame.font.init()

        # set window
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        # set title
        pygame.display.set_caption(TITLE)
        # holds whether the game is running for not
        self.running = True
        # get timer function
        self.clock = pygame.time.Clock()

        # set fonts
        self.scoreText = pygame.font.Font("assets/fonts/square.TTF", 25)
        self.titleFontLight = pygame.font.Font("assets/fonts/square.TTF", 50)

        # group for sprites
        self.allSprites = pygame.sprite.Group()

    def new(self):
        # creates a new game

        # make players
        self.player1 = Player(20, RED)
        self.player2 = Player(WIDTH - 30, BLUE)
        self.ball = Ball()
        self.p1Score = Text(100, 100, (RED), str(self.player1.score), self.scoreText)
        self.p2Score = Text(WIDTH - 10, 10, (WHITE), str(self.player2.score), self.scoreText)

        # put players inside groups
        self.allSprites.add(self.player1)
        self.allSprites.add(self.player2)

        # run game loop
        self.gameLoop()

    def gameLoop(self):
        # main game loop

        while self.running:
            self.allSprites.update()
            self.update()
            self.clock.tick(FPS)

    def update(self):
        # updates game

        self.draw()

        pygame.display.update()

    def draw(self):
        # draws to screen

        # set background color
        self.screen.fill(BLACK)

        # draw to screen
        self.ball.draw(self.screen)
        self.p1Score.draw()
        self.allSprites.draw(self.screen)

if __name__ == '__main__':
    Game().new()

我想要发生什么

我想让文字显示在我放入代码的x和y坐标的屏幕上。

我的目标

目前,文本未显示,也没有错误。

我尝试了什么

  1. 我试图输入字体的绝对路径,例如:C:/name/file/font.TTF

1 个答案:

答案 0 :(得分:1)

在您当前的代码中,这一行是问题所在: text.blit(text, (self.x, self.y))。此代码将一些文本绘制到自身上。您想要将文本绘制到屏幕上。用这一行替换该行: screen.blit(text, (self.x, self.y))。 这意味着您需要为screen的绘图功能设置Text参数。因此def draw(self):应为def draw(self,screen):。最后,您必须传递self.screen参数,因此self.p1Score.draw()应为self.p1Score.draw(self.screen)。所有更改的最终代码应为:

class Player(pygame.sprite.Sprite):
    def __init__(self, x, color):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface((10, 100))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.y = HEIGHT / 2 - self.rect.height / 2
        self.rect.centerx = x
        self.displacement = 8
        self.score = 0

    def update(self):
        # update function

        self.constrain(0, HEIGHT - self.rect.height)

    def constrain(self, min, max):
        # constrian player to walls

        if self.rect.y > max:
            self.rect.y = max
        if self.rect.y < min:
            self.rect.y = min


class Text():
    def __init__(self, x, y, color, text, font):
        self.x = x
        self.y = y
        self.color = color
        self.text = text
        self.font = font

    def draw(self,screen):
        text = self.font.render(self.text, False, self.color)
        screen.blit(text, (self.x, self.y))

class Game(pygame.sprite.Sprite):
    def __init__(self):
        # get sprite object
        pygame.sprite.Sprite.__init__(self)

        # init pygame
        pygame.init()

        # init pygame modules
        pygame.mixer.init()
        pygame.font.init()

        # set window
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        # set title
        pygame.display.set_caption(TITLE)
        # holds whether the game is running for not
        self.running = True
        # get timer function
        self.clock = pygame.time.Clock()

        # set fonts
        self.scoreText = pygame.font.Font("assets/fonts/square.TTF", 25)
        self.titleFontLight = pygame.font.Font("assets/fonts/square.TTF", 50)

        # group for sprites
        self.allSprites = pygame.sprite.Group()

    def new(self):
        # creates a new game

        # make players
        self.player1 = Player(20, RED)
        self.player2 = Player(WIDTH - 30, BLUE)
        self.ball = Ball()
        self.p1Score = Text(100, 100, (RED), str(self.player1.score), self.scoreText)
        self.p2Score = Text(WIDTH - 10, 10, (WHITE), str(self.player2.score), self.scoreText)

        # put players inside groups
        self.allSprites.add(self.player1)
        self.allSprites.add(self.player2)

        # run game loop
        self.gameLoop()

    def gameLoop(self):
        # main game loop

        while self.running:
            self.allSprites.update()
            self.update()
            self.clock.tick(FPS)

    def update(self):
        # updates game

        self.draw()

        pygame.display.update()

    def draw(self):
        # draws to screen

        # set background color
        self.screen.fill(BLACK)

        # draw to screen
        self.ball.draw(self.screen)
        self.p1Score.draw(self.screen)
        self.allSprites.draw(self.screen)

if __name__ == '__main__':
    Game().new()