0
\$\begingroup\$

I'm trying to make a game where the player has to dodge asteroids coming, and I'd like to display the lives. I looked up some tutorials and decided to settle on pygame.freetype. When I run the code though, the text does not show up like it did when I ran the example code.

Here is the code:

import pygame, random, time, pygame.freetype

from pygame.locals import (
    RLEACCEL,
    K_UP,
    K_DOWN,
    K_RIGHT,
    K_LEFT,
    K_SPACE,
    K_ESCAPE,
    KEYDOWN,
    QUIT,
)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()
        self.surf = pygame.image.load("ship.png").convert()
        self.surf.set_colorkey((255, 255, 255), RLEACCEL)
        self.rect = self.surf.get_rect()
        self.lives = 3

    def update(self, pressed_keys):
        if pressed_keys[K_UP]:
            self.rect.move_ip(0, -5)
            move_sound.play()
        if pressed_keys[K_DOWN]:
            self.rect.move_ip(0, 5)
            move_sound.play()
        if pressed_keys[K_RIGHT]:
            self.rect.move_ip(5, 0)
            move_sound.play()
        if pressed_keys[K_LEFT]:
            self.rect.move_ip(-5, 0)
            move_sound.play()

        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > SCREEN_WIDTH:
            self.rect.right = SCREEN_WIDTH
            pygame.mixer.music.stop()
            move_sound.stop()
            self.kill()
            win_sound.play()
            time.sleep(3)
            raise SystemExit(0)

        if self.rect.top <= 0:
            self.rect.top = 0
        if self.rect.bottom >= SCREEN_HEIGHT:
            self.rect.bottom = SCREEN_HEIGHT

class Asteroid(pygame.sprite.Sprite):
    def __init__(self):
        super(Asteroid, self).__init__()

        self.surf = pygame.image.load("asteroid.png").convert()
        self.surf.set_colorkey((255, 255, 255), RLEACCEL)
        self.rect = self.surf.get_rect(
            center=(
            random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
            random.randint(0, SCREEN_HEIGHT),
            )
        )
        
        self.speed = random.randint(5,20)
        self.collided_with_player = False

    def update(self):
        self.rect.move_ip(-self.speed, 0)
        if self.rect.right < 0:
            self.kill()


    def remove(self):
        self.kill()


SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

pygame.mixer.init()

pygame.mixer.music.load("music.wav")
pygame.mixer.music.play(loops=-1)

win_sound = pygame.mixer.Sound("game_win.wav")
hit_sound = pygame.mixer.Sound("hit_sound.wav")
move_sound = pygame.mixer.Sound("move_sound.wav")
move_sound.set_volume(.03)

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 250)

player = Player()

enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
score_font = pygame.freetype.Font("myfont.ttf", 24)


clock = pygame.time.Clock()
running = True

while running:
    score_font.render_to(screen, (40, 350), "Hello World!", (255, 255, 255))

    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False

        elif event.type == QUIT:
            running = False

        elif event.type == ADDENEMY:
            new_Asteroid = Asteroid()
            enemies.add(new_Asteroid)
            all_sprites.add(new_Asteroid)

    pressed_keys = pygame.key.get_pressed()
    player.update(pressed_keys)
    enemies.update()

    screen.fill((0,0,0))

    for entity in all_sprites:
        screen.blit(entity.surf, entity.rect)

        if pygame.sprite.spritecollideany(player, enemies):
            if not new_Asteroid.collided_with_player:
                player.lives -= 1
                print(player.lives)
                new_Asteroid.collided_with_player = True
                move_sound.stop()
                hit_sound.play()

                if player.lives == 0:
                    player.kill()
                    running = False

    pygame.display.flip()
    clock.tick(30)
```
\$\endgroup\$
5
  • 1
    \$\begingroup\$ You clear the screen before you have the chance to see the text; move score_font.render_to(screen, (40, 350), "Hello World!", (255, 255, 255)) just before pygame.display.flip(). \$\endgroup\$ Commented May 29, 2022 at 0:01
  • \$\begingroup\$ Like I commented with the other post, you should edit the post with the updated code/situation so that we see clearly the whole picture. \$\endgroup\$ Commented May 30, 2022 at 0:08
  • \$\begingroup\$ screen.fill((0,0,0)) means filling the entire screen with black. So make sure it is called before all draws in every render, otherwise previous draws will be overwritten. BTW I recommend submitting a minimal runnable example rather than the entire game program, as this will make the question more focused. \$\endgroup\$ Commented May 30, 2022 at 7:11
  • \$\begingroup\$ @Mangata thanks this worked! If you put this in answer form I would totally accept it. Also, I can't believe I hadn't noticed this earlier! \$\endgroup\$ Commented Jun 1, 2022 at 21:38
  • \$\begingroup\$ Also @Vaillancourt \$\endgroup\$ Commented Jun 1, 2022 at 21:38

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.