I am making a Pygame game and I have a label that every time you lose it should make the screen black and say GAME OVER. This is my code and the Label won't show up even though I copied it from my other label code and just changed it to label2. What is wrong?
if self.rect.colliderect(obstacle2.rect) or self.rect.colliderect(obstacle1.rect):
obstacle2.kill()
obstacle1.kill()
player.kill()
screenColor = BLACK
default_font2 = pygame.font.get_default_font()
font_renderer2 = pygame.font.Font(default_font2, 100)
label2 = font_renderer2.render('GAME OVER', True, WHITE)
label2_rect = label2.get_rect()
label2_rect.center = screen_rect.center
screen.blit(label2, label2_rect)
You need to draw the label in the application loop. The condition self.rect.colliderect(obstacle2.rect) or self.rect.colliderect(obstacle1.rect)
is just fulfilled in a single frame. Hence the label is just visible for a single frame and not noticeable.
Before the application loop, add a game_over
variable:
game_over = False
Set the variable when the collision is detected:
if self.rect.colliderect(obstacle2.rect) or self.rect.colliderect(obstacle1.rect):
obstacle2.kill()
obstacle1.kill()
player.kill()
screenColor = BLACK
# [...]
game_over = True
Draw the text in the application loop depending on game_over
:
# application loop
while True:
# [...]
if game_over
# [...]
screen.blit(label2, label2_rect)
pygame.display.update()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.