I am making an attempt to make a recreation the place the participant has to dodge asteroids coming, and I might wish to show the lives. I regarded up some tutorials and determined to decide on pygame.freetype. Once I run the code although, the textual content doesn’t present up prefer it did after I ran the instance code.
Right 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 Participant(pygame.sprite.Sprite):
def __init__(self):
tremendous(Participant, self).__init__()
self.surf = pygame.picture.load("ship.png").convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.surf.get_rect()
self.lives = 3
def replace(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.proper > SCREEN_WIDTH:
self.rect.proper = SCREEN_WIDTH
pygame.mixer.music.cease()
move_sound.cease()
self.kill()
win_sound.play()
time.sleep(3)
increase SystemExit(0)
if self.rect.prime <= 0:
self.rect.prime = 0
if self.rect.backside >= SCREEN_HEIGHT:
self.rect.backside = SCREEN_HEIGHT
class Asteroid(pygame.sprite.Sprite):
def __init__(self):
tremendous(Asteroid, self).__init__()
self.surf = pygame.picture.load("asteroid.png").convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.surf.get_rect(
heart=(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.velocity = random.randint(5,20)
self.collided_with_player = False
def replace(self):
self.rect.move_ip(-self.velocity, 0)
if self.rect.proper < 0:
self.kill()
def take away(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()
display = pygame.show.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 250)
participant = Participant()
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(participant)
score_font = pygame.freetype.Font("myfont.ttf", 24)
clock = pygame.time.Clock()
operating = True
whereas operating:
score_font.render_to(display, (40, 350), "Hi there World!", (255, 255, 255))
for occasion in pygame.occasion.get():
if occasion.sort == KEYDOWN:
if occasion.key == K_ESCAPE:
operating = False
elif occasion.sort == QUIT:
operating = False
elif occasion.sort == ADDENEMY:
new_Asteroid = Asteroid()
enemies.add(new_Asteroid)
all_sprites.add(new_Asteroid)
pressed_keys = pygame.key.get_pressed()
participant.replace(pressed_keys)
enemies.replace()
display.fill((0,0,0))
for entity in all_sprites:
display.blit(entity.surf, entity.rect)
if pygame.sprite.spritecollideany(participant, enemies):
if not new_Asteroid.collided_with_player:
participant.lives -= 1
print(participant.lives)
new_Asteroid.collided_with_player = True
move_sound.cease()
hit_sound.play()
if participant.lives == 0:
participant.kill()
operating = False
pygame.show.flip()
clock.tick(30)
```