这是与射击游戏有关的项目的一部分。变量fail_times
并未按预期增加。我应该如何处理这个问题?
def check_fail(bullets,stats,screen,fail_times):
for bullet in bullets:
if bullet.rect.right>=screen.get_rect().right:
bullets.remove(bullet)
fail_times+=1
print(fail_times)
elif fail_times>3:
stats.game_active=False
pygame.mouse.set_visible(True)
答案 0 :(得分:2)
如果使用类变量创建一个类,它将具有您要查找的范围:
class game:
def __init__(self, fail_times=0):
self.fail_times = fail_times
def check_fail(self, bullets, stats, screen):
for bullet in bullets:
if bullet.rect.right >= screen.get_rect().right:
bullets.remove(bullet)
self.fail_times += 1
print(fail_times)
elif fail_times > 3:
stats.game_active = False
pygame.mouse.set_visible(True)
然后使用它必须实例化该类:
my_game = game()
my_game.check_fail(bullets, stats, screen)