Dojo Defender Session 7 : Cheatsheet
Variables de power-up
powerups = [] # liste de GameObjects de power-up
active_powerup = None # 'spread', 'shield', 'speed' ou None
powerup_timer = 0 # frames restantesFaire apparaître un power-up (à la mort d’un ennemi)
def spawn_powerup(x, y):
if random.random() < 0.2: # 20 % de chance
ptype = random.choice(['spread', 'shield', 'speed'])
pu = GameObject(f'powerup_{ptype}.png', x, y)
pu.pu_type = ptype
pu.speed = 2
powerups.append(pu)Ramasser un power-up
for pu in powerups[:]:
pu.rect.y += pu.speed
if pu.rect.top > HEIGHT + 30:
powerups.remove(pu)
elif pu.rect.colliderect(ship.rect) and ship.visible:
active_powerup = pu.pu_type
powerup_timer = 300 # 5 secondes
powerups.remove(pu)Timer du power-up actif
if active_powerup:
powerup_timer -= 1
if powerup_timer <= 0:
active_powerup = NoneSpread shot (3 balles)
if active_powerup == 'spread' and shoot_cooldown == 0:
angles = [-15, 0, 15]
for a in angles:
rad = math.radians(a)
bullet = GameObject('bullet.png', x, y)
bullet.dx = math.sin(rad) * 3
bullet.dy = -8
bullets.append(bullet)
shoot_cooldown = 10Pour un tir normal : bullet.dx = 0 et bullet.dy = -8.
Bouclier — absorber un hit
if active_powerup == 'shield':
active_powerup = None # le bouclier est épuisé
invincible = True
invincible_timer = 300 # 5 secondes d'invincibilitéSpeed boost — 2x plus vite
ship_speed = 5
if active_powerup == 'speed':
ship_speed = 10Sauvegarder le high score
def load_highscore():
path = 'highscore.txt'
if os.path.exists(path):
with open(path) as f:
return int(f.read().strip())
return 0
def save_highscore(val):
if val > load_highscore():
with open('highscore.txt', 'w') as f:
f.write(str(val))Ajouter le reset dans reset_game()
N’oublie pas de remettre les variables de power-up à zéro dans reset_game() :
powerups.clear()
active_powerup = None
powerup_timer = 0