0

I asked a similar question about two weeks ago, and have since updated my code to no avail. It absolutely boggles my mind how convoluted it is to simply render some text in PyGame.

Here is the method for the UI I'm attempting to implement:

    def gui(self):
    
    self.hpdisplay = self.font.render('HP: 100', True, white)
    self.hpdisplay_rect = self.hpdisplay.get_rect(x=10, y=10)
    self.screen.blit(self.hpdisplay, self.hpdisplay_rect)

According to PyGame documentation, this is exactly how I'm supposed to do this, and it's even identical to some text I managed to render successfully in a different part of my code which I'll show here:

title = self.font.render('Cambria', True, white)
title_rect = title.get_rect(x=10, y=10)
self.screen.blit(title, title_rect)

The UI that isn't working is supposed to appear when I call this new() method:

def new(self):
    self.playing = True
    self.all_sprites = pygame.sprite.LayeredUpdates()
    self.tiles = pygame.sprite.LayeredUpdates()
    self.blocks = pygame.sprite.LayeredUpdates()
    self.buildings = pygame.sprite.LayeredUpdates()
    self.player = Player(self, self.startx, self.starty)
    self.enemies = pygame.sprite.LayeredUpdates()
    self.attacks = pygame.sprite.LayeredUpdates()
    self.triggers = pygame.sprite.LayeredUpdates()

    self.TileCreate()
    self.gui()

This method renders all the sprites, TileCreate arranges all the sprites in the tilemap loading system, and gui() is SUPPOSED to render some text at the top of the screen, yet no matter what I do, it simply refuses to render ANYTHING.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
kecenr
  • 21
  • 1
  • 8
  • 1
    I can't see an mistake in your code. You say yourself that *"Text not rendering despite using same code that works elsewhere"*. So the problem is not the code itself. The problem depends on the context and how you call `gui`. You have to call `self.gui()` in the application instead of in `new()`. – Rabbid76 Jul 08 '21 at 17:08
  • If the method I call gui() in is the same method that's supposed to render literally everything, wouldn't the logical conclusion be that it also renders the text? The method with text that does work is called the exact same way as gui() – kecenr Jul 08 '21 at 17:13
  • 1
    `new()` doesn't render it just generates the _Groups_. The _Sprtes_ are rendered with [`pygame.sprite.Group.draw()`](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group.draw). Note, all the objects need to be draw continuousely in the applicaition loop. – Rabbid76 Jul 08 '21 at 17:34

1 Answers1

0

Thanks to @Rabbid76 for the suggestion of calling self.gui elsewhere in the Game loop rather than in new().

If I call gui in the draw() method, which is as follows:

    def draw(self):
    self.all_sprites.draw(self.screen)
    self.clock.tick(FPS)
    self.gui()
    pygame.display.update()

I'm not exactly sure how, but the text now renders.

kecenr
  • 21
  • 1
  • 8