📝 Worksheet← Lesson← Course
BitWithBite
Pygame Mastery · Quick Reference

Enemy AI Cheat Sheet

Pygame Mastery
In one line: Patrols, line-of-sight chasing, and a finite state machine that ties them together into readable enemy behavior.

Key Ideas

1Patrol. Walk between two points, reversing direction on arrival — the simplest "alive" enemy behavior.
2Chasing. Move toward player.pos each frame when distance < detection radius, using a normalized direction vector.
3Finite State Machine. The enemy is always in exactly one state (patrol/chase/attack); transitions follow clear distance/health rules.
4Pathfinding Concepts. Full A* searches a grid for shortest routes; simpler waypoint graphs solve most 2D needs cheaply.
5Boss Phases. Health thresholds (e.g. below 50%) trigger a more aggressive phase with faster attack timers.

FSM Transition Snippet

if state == "patrol" and dist < 220:
  state = "chase"
elif state == "chase":
  if dist < 40: state = "attack"
  elif dist > 300: state = "patrol"
detection radiusEnemyPlayer (chasing)Player (out of range)
When distance < detection radius the enemy switches state to chase and moves toward player.pos every frame.