hmood
hmood

Reputation: 603

Why is pygame not reducing the health of this entity?

I am working on Space Invaders (my own version), and when the spaceship shoots an enemy, it doesn't lose health. Here is the code that should decrease an enemy's health:

while not done:

    # --- Event Processing and controls
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                spaceship_x_change = 10
            elif event.key == pygame.K_LEFT:
                spaceship_x_change = -10
            elif event.key == pygame.K_r:
                red = (255, 0, 0)
        elif event.type == pygame.KEYUP:
            spaceship_x_change = 0
            red = (0, 0, 0)

    spaceship_x += spaceship_x_change

    # Preventing the ship from going off the screen
    if spaceship_x > display_width - 140:
        spaceship_x -= 10
    if spaceship_x < 1:
        spaceship_x += 10
    if spaceship_x+69 < 65 and spaceship_x > 75:
        blue_enemy_health = blue_enemy_health - 1

    message(str(blue_enemy_health), white, 65, 10, font, game_display)
    game_display.blit(blue_enemy, (20, 25))

Also, as a side note, this is only a small portion of my code.

Upvotes: 0

Views: 47

Answers (2)

Rabbid76
Rabbid76

Reputation: 210978

The sum of spaceship_x and 69 will always be greater than 65. Hence the condition if spaceship_x+69 < 65 is never met. It follows that blue_enemy_health is never incremented.

Upvotes: 0

laurensvm
laurensvm

Reputation: 193

As @Rabbid76 points out, you ensure that spaceship_x is always between the bounds 1 and displaywidth - 140. Then, you have the equation spaceship_x + 69 < 65, which means spaceship_x < -4. This cannot happen in your code

Upvotes: 1

Related Questions