From Zero to Playable: Building a 2D Fighting Game with Character Systems, Combat Mechanics, and Match Logic
This article breaks down the technical architecture of a 2D fighting game, covering character data design, finite-state-machine-based behavior control, frame-accurate hit detection, defense and stagger systems, aerial combos, special moves, three-tier AI opponents, and the full round-based match flow, with practical code examples for reusable implementation.
Fighting games are among the most engineering-intensive genres in action gaming. They require precise frame-level hit detection, complex state machine management, multi-player input handling, and AI behavior design. This article deconstructs the technical implementation path of a typical 2D fighting game—featuring two independent characters, a rain-soaked rooftop stage, single-player AI and local two-player versus modes, and a best-of-three match format—to give developers a reusable methodology for fighting game development.
Why Fighting Games Look Simple but Are Hard to Build
Many developers who first attempt a fighting game notice that the “homepage presentation” generated by prototyping tools looks great: polished character art and atmospheric stages. But once actual combat begins, the experience noticeably degrades. The reasons are:
- The complexity gap between presentation and logic layers is enormous. The homepage only needs static rendering or simple animations, while combat requires per-frame input sampling, collision detection, state transitions, damage resolution, and animation synchronization across multiple subsystems.
- Hit feedback is “tuned,” not implemented. Hitstop, screen shake, hit flash, and launch trajectories are all essential—missing any one of them makes combat feel floaty and unconvincing.
- Hitbox frame data requires careful configuration. Active frames for attacks, guard windows, and throw range all need to be meticulously configured on a per-frame basis.
With these challenges in mind, let’s break down the implementation module by module.
Character System: From Art to Controllable Entity
Character Data Structure Design
Each character should be abstracted as an independent “character definition” containing visual assets, attribute values, and a move list. A data-driven approach is recommended:
{"name": "Character A","maxHp": 1000,"walkSpeed": 3.2,"dashSpeed": 8.0,"jumpForce": -14.5,"moves": {"lightPunch": { "startup": 4, "active": 3, "recovery": 7, "damage": 40 },"heavyKick": { "startup": 10, "active": 4, "recovery": 16, "damage": 90 },"specialMove": { "input": ["↓", "→", "P"], "cost": 0 }}}
The startup / active / recovery three-phase frame data is the core of any fighting game: startup frames determine attack speed, active frames define the hit window, and recovery frames control end-lag. The differences between two characters (e.g., a speed type vs. a power type) come entirely from this configuration—no logic code changes are needed.
State Machine: The Backbone of Character Behavior
A character can only occupy one state at any moment. A finite state machine (FSM) is the recommended management pattern:
- Standing, walking, jumping, dashing
- Attacking (further subdivided by move)
- Guarding, hitstun, airborne, knockdown
- Throwing and being thrown
State transitions must follow a clear priority rule, for example: hitstun > throw > attack > movement. A typical implementation:
def update(self, input_frame):if self.state.can_be_interrupted(input_frame):next_state = self.resolve_priority(input_frame)self.transition_to(next_state)self.state.tick()
One notable mechanism is the “chain cancel”: it allows the player to cancel the current recovery window of an attack after the active frames have ended, using specific inputs to transition into the next move. This is the foundation of combo systems. Implementation is a matter of checking the cancel window within the attack state and permitting an early state transition.
Core Combat Mechanics
Guarding and Guard Break
Guarding is not a simple damage-reduction toggle. The system must distinguish between:
- Standing guard: Blocks high and mid attacks, but fails against low attacks.
- Crouching guard: Blocks low and mid attacks, but fails against high attacks.
- Throws: Ignore guarding entirely and counter pure-defensive play, creating a complete offense-defense rock-paper-scissors loop.
A successful block drains a small amount of “guard gauge.” When the gauge is depleted, the guard breaks and the defender enters hitstun, preventing infinite turtling.
Launchers and Combos
Launchers are the core of stylish combos. A hit defender in the air is subject to gravity and a terminal fall speed. The attacker can chain “launcher → air chase → ground reset” to build extended combos. Two constraints are essential: combo counting and damage scaling. Each consecutive hit applies an attenuation coefficient to the damage, preventing infinite combos from becoming unfair.
Special Moves and Meter Management
Special moves are triggered by directional input sequences (e.g., “down-forward + punch”). The input sampler should buffer a history of recent frames and perform pattern matching. When the meter is full, a super move triggers a cinematic camera cut: the game briefly freezes, cuts to a character close-up cinematic, and then resumes combat. This requires an independent “presentation management layer” that pauses normal logic before damage is resolved and restores it after the sequence finishes.
A Universal Hit Detection Model
Attack hitboxes and the defender’s hurtbox are represented as rectangles or capsules, tested for intersection each frame:
def check_hit(attacker, defender):for hitbox in attacker.active_hitboxes():if hitbox.intersects(defender.hurtbox()):if defender.is_guarding_correctly(hitbox):apply_chip_damage()else:apply_damage(hitbox.damage)trigger_hitstop(frames=hitbox.impact_frames)
Hitstop freezes both characters for a few frames on impact—a key source of combat feel.
AI Opponent Design: Three Difficulty Tiers
AI should not be “cheap input-reading”; it should emulate human decision-making. A layered design is recommended:
- Easy: Low attack frequency, random guarding, no special moves.
- Normal: Chooses moves based on distance (approach attacks at range, throws up close), occasionally guards.
- Hard: Reads player state—throws when the player guards too much, punishes whiffed attacks during recovery frames, and actively seeks super move opportunities when the meter is full.
A “decision cooldown plus weighted table” approach prevents robotic, machine-gun inputs:
def decide(self, dist, player_state):if self.cooling: returnweights = self.strategy_table[dist_zone(dist)][player_state]action = weighted_random(weights)self.execute(action)
Match Flow and Result System
The complete match cycle includes: character select → round start countdown → combat → KO resolution → best-of-three determination → final results screen. Points of attention:
- Pause system: Freeze all game logic on pause, but keep UI responsive.
- Round reset: Reset HP, meter, and positions at the start of each round, but preserve match point scores.
- Draw and timeout: When time runs out, the winner is determined by remaining HP. If HP is equal, the round is declared a draw and a tiebreaker round is played.
Stage and Atmosphere: The Rain-Soaked Rooftop
The stage is more than a backdrop. The rain-soaked rooftop scene gains depth through layered parallax scrolling: distant city lights, a mid-layer rain particle system, and foreground water splashes. Rain particles should be managed with an object pool to avoid garbage collection hitches. Simple mirrored floor textures overlaid with a semi-transparent layer handle the wet-surface reflection effectively. Atmosphere layered on top of combat feedback completes the “fighting game experience.”
Summary
Fighting game development is fundamentally the sum of three engineering problems: precise real-time hit detection systems (frame data, collision, state machines), reliable input and local versus handling, and the polish of hit feedback and presentation. A data-driven organization of characters and moves, FSM-based behavior management, layered AI difficulty, and presentation details like hitstop and camera cinematics yield a clean, extensible fighting game framework. For developers ready to go further, the next logical step is to abstract local multiplayer input into a unified input interface, paving the way for future networked synchronized play.
