Question

Currently I am working on a small 'space invaders' style game using pygame and I wanted to create various outcomes depending on the score / health level. I wish to make it so if the enemy ships have passed the player and go off the screen and the score target has not yet met then I want it to display a message stating the target score has not been met.

I have this code so far which I think should allow this to happen:

if block.rect.y >= 600 and health >=25 and score < 70:
    font = pygame.font.Font("freesansbold.ttf", 30)
    label2 = font.render("Score target not met", 1, (0,255,0))
    label2Rect = label.get_rect()
    labelRect.center = (400, 250)

Here it is implemented in more depth:

while True:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
    if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
        pygame.quit()
        sys.exit()

    elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
        laser.play()
        k_space = True

if k_space:
    bullet = Bullet()
    bullet.rect.x = player.rect.x
    bullet.rect.y = player.rect.y
    all_sprites_list.add(bullet)
    bullet_list.add(bullet)

k_space = False






for player in player_list:

    block_hit_list = pygame.sprite.spritecollide(player, block_list, True)

    for block in block_hit_list:
        collision.play()
        block_list.remove(block)
        all_sprites_list.remove(block)
        health -= 25

        if health == 0:
            font = pygame.font.Font("freesansbold.ttf", 30)
            label = font.render("GAME OVER", 1, (255,0,0))
            labelRect = label.get_rect()
            labelRect.center = (400, 250)
            gameover.play()
            collision.stop()
            player_list.remove(player)
            all_sprites_list.remove(player)
            break












for bullet in bullet_list:

    block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)

    for block in block_hit_list:
        explosion.play()
        bullet_list.remove(bullet)
        all_sprites_list.remove(bullet)
        score += 10

        if health >= 25 and score == 70:
            win.play()
            font = pygame.font.Font("freesansbold.ttf", 30)
            label = font.render("LEVEL COMPLETE!", 1, (0,255,0))
            labelRect = label.get_rect()
            labelRect.center = (400, 250)
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            break

    if score >= 70:
        collision.stop()
        laser.stop()
        explosion.stop()
        bullet_list.remove(bullet)
        all_sprites_list.remove(bullet)
        block_list.remove(block)
        all_sprites_list.remove(block)
        player_list.remove(player)
        all_sprites_list.remove(player)

    if health == 0:
        laser.stop()
        bullet_list.remove(bullet)
        all_sprites_list.remove(bullet)

    if bullet.rect.y < -10:
        bullet_list.remove(bullet)
        all_sprites_list.remove(bullet)

    #NOT FUNCTIONING

if block.rect.y >= 600 and health >=25 and score < 70:
    font = pygame.font.Font("freesansbold.ttf", 30)
    label2 = font.render("Score target not met", 1, (0,255,0))
    label2Rect = label.get_rect()
    labelRect.center = (400, 250)

    #NOT FUNCTIONING
font = pygame.font.Font(None, 30)
text = font.render("Score: " + str(score), True, WHITE)
textpos = text.get_rect(centerx=screen.get_width()/5)

font = pygame.font.Font(None, 30)
text2 = font.render("Health: " + str(health), True, GREEN)
textpos2 = text2.get_rect(centerx=screen.get_height())

all_sprites_list.update()

screen.blit(background, (0,0))

if label != None: 
        screen.blit(label, labelRect)

if health == 75:
    text2 = font.render("Health: " + str(health), True, YELLOW)
elif health == 50:
    text2 = font.render("Health: " + str(health), True, ORANGE)
elif health == 25:
    text2 = font.render("Health: " + str(health), True, RED)
elif health == 0:
    text = font.render(" ", True, BLACK)
    text2 = font.render(" ", True, BLACK)

if score >= 70:
    text = font.render(" ", True, BLACK)
    text2 = font.render(" ", True, BLACK)

screen.blit(text, textpos)

screen.blit(text2, textpos2)

screen.blit(label, labelRect)

all_sprites_list.draw(screen)

pygame.display.update()

clock.tick(190)

pygame.quit()

If anyone has any idea as to why the text isnt printing once the if statement is met id really appreciate the help. Thank you.


UPDATE

I have just realized that the font does in fact get printed to the screen however this only happens if I manage to avoid collisions with all enemy ships and if I dont fire a bullet. Does anyone have any idea as to why this might be happening? Thanks

Was it helpful?

Solution

Hmmm... It looks to me like you have done everything right. Here is some of my code for blitting text to a screen that I know works:

font = pygame.font.Font("Fonts/Typewriter.ttf", 20)
deathCount = font.render("Deaths: "+str(deaths), 1,(255,255,255))
winTally = font.render("Wins: "+str(winCount), 1,(255, 255, 255))
screen.blit(deathCount, (5, 5))

Besides that, unless your if statement is not working I'm not sure sorry :/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top