我正在 pygame 中开发一个小游戏,并且我设置了一个健康变量,所以每次我的玩家被敌舰击中时,我的健康都会减少 25,但是有时(如果我的玩家快速穿过屏幕)我能够将此值设为 0 以下,它会继续减少 25 为负数。
有什么办法可以设置它,一旦达到 0,分数就不能低于 0?
到目前为止,这是我的代码:
for player in player_list:
block_hit_list = pygame.sprite.spritecollide(player, block_list, True)
for block in block_hit_list:
health -= 25
collision.play()
if health < 0:
health = max(value, 0)
if health == 0:
gameover.play()
collision.stop()
player_list.remove(player)
all_sprites_list.remove(player)
block_list.remove(block)
all_sprites_list.remove(block)
更新
我已经更新了代码,现在它似乎运行正常,不确定我是否使用了有意义的东西(如果是,欢迎任何改进):(我对 python 很陌生。