Dojo Defender Sessie 7: Cheatsheet

Power-up variabelen

powerups = []  # lijst van power-up GameObjects
active_powerup = None  # 'spread', 'shield', 'speed' of None
powerup_timer = 0  # frames remaining

Power-up spawnen (bij enemy death)

def spawn_powerup(x, y):
    if random.random() < 0.2:  # 20% kans
        ptype = random.choice(['spread', 'shield', 'speed'])
        pu = GameObject(f'powerup_{ptype}.png', x, y)
        pu.pu_type = ptype
        pu.speed = 2
        powerups.append(pu)

Power-up verzamelen

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 seconden
        powerups.remove(pu)

Timer voor actieve power-up

if active_powerup:
    powerup_timer -= 1
    if powerup_timer <= 0:
        active_powerup = None

Spread shot (3 kogels)

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 = 10

Bij gewoon schieten: bullet.dx = 0 en bullet.dy = -8.

Shield — hit absorberen

if active_powerup == 'shield':
    active_powerup = None  # schild is op
    invincible = True
    invincible_timer = 300  # 5 seconden onzichtbaarheid

Speed boost — 2x sneller

ship_speed = 5
if active_powerup == 'speed':
    ship_speed = 10

High score opslaan

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))

Reset toevoegen in reset_game()

Vergeet niet om in reset_game() de power-up variabelen te resetten:

powerups.clear()
active_powerup = None
powerup_timer = 0