为什么 pygame 没有降低这个实体的健康?

问题描述

我正在开发 Space Invaders(我自己的版本),当飞船射击敌人时,它不会损失生命值。这是应该降低敌人生命值的代码

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)
        elif event.type == pygame.KEYUP:
            spaceship_x_change = 0
            red = (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))

另外,作为旁注,这只是我代码的一小部分。

解决方法

正如@Rabbid76 指出的那样,您确保 spaceship_x 始终在边界 1 和 displaywidth - 140 之间。然后,您有等式 spaceship_x + 69 < 65,这意味着 {{1} }.这不会发生在您的代码中

,

spaceship_x69 的总和将始终大于 65。因此永远不会满足条件 if spaceship_x+69 < 65。因此 blue_enemy_health 永远不会增加。