0

Problem

So, i'm trying to render text that holds the player's score, but when I writ the code, it doesn't work.

Python Code

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()

What I want to happen

I want text to display on the screen at the x and y coordinates that I put in my code.

What I Am Getting

Currently, the text is not being displayed, also there are no errors.

What I have tried

  1. I have tried to put in the absolute path to the font, ex: C:/name/file/font.TTF
2
  • Why is your Game a pygame sprite? :P Commented May 26, 2017 at 13:53
  • Please make sure that we can just copy and execute your code before you post it here. stackoverflow.com/help/mcve Commented May 26, 2017 at 14:05

1 Answer 1

1

In your current code, this line is the problem: text.blit(text, (self.x, self.y)). This code is drawing some text onto itself. You want to draw the text onto the screen. Replace that line with this line: screen.blit(text, (self.x, self.y)). This means that you need to have a screen argument for your Text's draw function. So def draw(self): should be def draw(self,screen):. Finally, you must pass the self.screen argument, so self.p1Score.draw() should be self.p1Score.draw(self.screen). The final code, with all changes made should be:

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.