Skip to content

Realistic physics, procedural terrain, and input fix - #7

Open
incredibleamir-dot wants to merge 10 commits into
ternus:masterfrom
incredibleamir-dot:master
Open

Realistic physics, procedural terrain, and input fix#7
incredibleamir-dot wants to merge 10 commits into
ternus:masterfrom
incredibleamir-dot:master

Conversation

@incredibleamir-dot

Copy link
Copy Markdown

Summary

Reworks the lunar lander into a playable, realistic simulation and fixes a
critical input bug that made every key press dead.

What changed

Realistic rocket physics (SI units)

  • Moon surface gravity 1.62 m/s², world scaled at 8 px/m
  • Lander mass = 1500 kg dry + 450 kg fuel; mass falls as fuel burns
  • Engine thrust = burn rate × exhaust velocity = 10 kN, so
    thrust-to-weight ratio rises ~3.2 → 4.1 as the tank empties (like a real rocket)
  • Landing succeeds below 3 m/s while upright on the pad (realistic LM
    touchdown speed)
  • Sub-pixel motion preserved via float _px/_py positions (was lost by
    Rect.move() truncation, leaving the lander frozen)

Procedural terrain

  • Starfield sky, gradient planet and horizon glow
  • Cratered lunar surface with a marked landing pad ("Perfect landing!")
  • Irregular, shaded boulders that never spawn over the pad

State machine

  • READY → PLAYING → RESULT; SPACE launches, no more mid-fall surprise
  • Continuous thrust (hold SPACE/UP), ←/→ rotation at 40°/s
  • HUD: fuel (kg), altitude (m), speed (m/s), mass

Input fix (critical)

pygame.key.get_pressed() is scancode-indexed, so keys built from
enumerate(...) never matched the K_* constants and no input registered.
Added POLLED_KEYS/pressed_keys() to translate scancodes to key
constants, with a regression test.

Testing

  • 50 tests passing (pytest -q)
  • Verified with a simulated controller: perfect pad landing in ~20 s
  • Verified headless that the game loop starts and exits cleanly

Controls

Key Action
SPACE Launch / thrust
← / → Rotate left / right
R Restart

Rework the game around real SI rocket physics: moon gravity (1.62 m/s^2),
mass loss as fuel burns, and engine thrust computed from burn rate x exhaust
velocity so thrust-to-weight rises over time. Landing succeeds below 3 m/s
while upright on the pad.

Fix input: pygame.key.get_pressed() is scancode-indexed, so the old
enumerate() keys never matched the K_* constants and no key did anything.
Add POLLED_KEYS / pressed_keys() to translate scancodes, with a regression
test.

Other changes:
- Track lander position in float _px/_py so sub-pixel motion is not lost
  (Rect.move() truncates), fixing a frozen-lander bug
- READY -> PLAYING -> RESULT state machine; SPACE launches
- Procedural terrain: starfield sky, cratered moon, marked landing pad,
  irregular shaded boulders that avoid the pad
- HUD shows fuel (kg), altitude (m), speed (m/s), mass
- Tests updated for SI values; 50 passing
…errain

Full rework of the PyGame Lunar Lander into a playable, realistic
simulation, now packaged as an installable module.

Physics (real SI rocket mechanics):
- Moon gravity 1.62 m/s^2, mass loss as fuel burns, 10 kN thrust from
  burn rate x exhaust velocity, rising thrust-to-weight ratio
- Float-position tracking so sub-pixel motion is never lost
- Mid-frame fuel exhaustion still delivers partial thrust

Gameplay and visuals:
- READY -> PLAYING -> RESULT state machine; SPACE launches
- Procedural terrain: starfield, cratered moon, marked landing pad,
  irregular shaded boulders
- Fuel (kg) / altitude (m) / speed (m/s) HUD

Fixes:
- Keyboard input: get_pressed() is scancode-indexed; keys are now built
  from the K_* constants via pressed_keys() with a regression test
- V equality uses math.isclose; load_image converts alpha images properly

Packaging and quality:
- Split into a package (constants, vectors, assets, terrain, sprites, game)
- pyproject.toml with lunar-lander console script and ruff/pytest config
- Ruff linting, type hints, GitHub Actions CI (pytest matrix + lint)
- 51 passing tests; added main.py local launcher
This CI workflow includes jobs for testing and linting with multiple Python versions.
Removed CI badge from README.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant