Files
mygame/CLAUDE.md
2026-05-20 20:26:25 +08:00

2.5 KiB

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

Running the Game

Use python env: C:\Users\Think1Admin\miniconda3\envs\play\python.exe

# Install dependencies
pip install -r requirements.txt

# Run (must use module form from project root)
python -m game.main

Requires Python 3.10+ with pygame >= 2.5. Developed with Python 3.12.

Architecture

Classic Pygame main loop at 60 FPS in game/main.py (Game class). Update and draw pipelines are linear and sequential — no event bus or observer patterns.

All game state lives on the Game instance as shared mutable lists: enemies, towers, projectiles, plus scalar state (gold, lives). Systems communicate by reading/writing these lists directly.

Update Order

WaveManager.update → spawn enemies
Enemy.update → move along path, detect end-of-path
Tower.update → find targets, create Projectiles
Projectile.update → home in, deal damage (splash/slow on hit)
Reward collection + cleanup of dead entities
Win/loss check

Draw Order

Background → Map → Towers → Enemies → Projectiles → Placement preview → UI → Game-over overlay

Key Design Patterns

Centralized config — All constants, colors, tower/enemy stat tables, and path waypoints are in game/config.py. Balance tuning only requires editing this file and WAVE_DATA in wave.py.

Data-driven types — Tower and enemy types are dictionaries (TOWER_DATA, ENEMY_DATA). Adding a new type requires: add dict entry in config.py, add draw code in the respective class, update wave data if it's an enemy, add button in UI._init_tower_buttons() if it's a tower.

Targeting — Towers target by path progress (furthest-along enemy in range), not nearest distance.

Coordinate systems — Pixel coords for rendering/movement; grid (col, row) for tower placement. GameMap handles conversion with UI_TOP_HEIGHT (40px) offset for the top status bar.

Procedural graphics — All visuals drawn with pygame.draw calls. No sprite/image assets.

Extending the Game

  • New tower: Add to TOWER_DATA in config, draw shape in Tower.draw(), projectile visual in Projectile.draw(), button in UI._init_tower_buttons().
  • New enemy: Add to ENEMY_DATA in config, draw in Enemy.draw(), add to WAVE_DATA compositions.
  • New map: Edit PATH_WAYPOINTS in config. Path cells recalculate automatically.
  • Balance: All numeric tuning is in config.py constants.