0

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
hmood
  • 603
  • 7
  • 25
  • 2
    I don't think `spaceship_x + 69` is ever less than `65`. Did you accidentally mix up `<` and `>`? – Rabbid76 Dec 19 '20 at 20:03
  • @Rabbid76 the spaceship moves and so does `space_x+69 ` moves. – hmood Dec 19 '20 at 20:05
  • 1
    ... I mean the sum of `spaceship_x` and `69` will always be greater than `65`. Hence the condition `if spaceship_x+69 < 65` is never met. – Rabbid76 Dec 19 '20 at 20:07
  • 1
    @Rabbid76 ahhhhhhh i see. thanks :). I was wondering what was wrong. – hmood Dec 19 '20 at 20:08
  • You may find help in the answers to [How do I detect collision in pygame?](https://stackoverflow.com/questions/29640685/how-do-i-detect-collision-in-pygame/65064907#65064907) – Rabbid76 Dec 19 '20 at 20:52

2 Answers2

1

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

laurensvm
  • 143
  • 8
0

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174