سؤال

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 the player has not yet met the score limit of 70 to pass the level, I want 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 == random.randrange(600) and health >=25 and score < 70:
            font = pygame.font.Font("freesansbold.ttf", 30)
            label = font.render("Score target not met", 1, (0,255,0))
            labelRect = label.get_rect()
            labelRect.center = (400, 250)
            break

Here it is shown in more detail:

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 block.rect.y == random.randrange(600) and health >=25 and score < 70 and score > 0:
            font = pygame.font.Font("freesansbold.ttf", 30)
            label = font.render("Score target not met", 1, (0,255,0))
            labelRect = label.get_rect()
            labelRect.center = (400, 250)
            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)

However once I execute this code no text is actually pasted onto the window once the enemy ships have passed.

Heres my full code incase anyone knows why this is happening:

http://pastebin.com/C0V5MnSH

هل كانت مفيدة؟

المحلول

There are a few problems that I can see. First, you shouldn't check for the enemies going off of the screen inside of

for bullet in bullet_list:
    for block in block_hit_list:

you should create a new line on the main loop.

Second, every time you call

random.randrange(600)

you get a new random number inbetween 0 and 600. So the following code has a sixth of a percent chance of being ran if block is still on the screen. If block goes of the screen, it has a 0 percent chance. Instead, write

if block.rect.y >= 600:

So the whole thing should look like:

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)


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

Hope that helps!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top