AI Pong — Mechanics & AI


This one‑file canvas game implements a fast Pong with an AI opponent you can tune on the fly.

Key mechanics

  • Serve & countdown: Each point starts with a 3→2→1→Go! countdown (beeps), then the ball launches at 75% speed with a random direction. During countdown, ball velocity is zero and the trail/particles are cleared.
  • Spin & acceleration: Paddle collisions compute a contact ratio along the paddle (top→bottom) and convert it to a vertical velocity offset, adding controllable “spin.” After each hit the ball speed scales up by ~3% (both axes) to raise difficulty.
  • Walls & scoring: Top/bottom walls bounce the ball; crossing the left/right bounds scores and triggers a short score‑flash overlay, a particle burst, score text pulse, and a fresh countdown.
  • Particles & trail: A short trail is drawn from recent ball positions; hits and scores spawn HSL‑tinted particle bursts with gravity and decay.

Player & input

  • Movement: W/S or Arrow Up/Down move the left paddle. Position is clamped to canvas bounds.
  • Mobile controls: On‑screen ▲/▼ buttons synthesize keypresses so the same movement logic works on touch devices.
  • Paddle size: Grow/Shrink buttons symmetrically adjust both paddles while keeping their centers aligned and clamping to bounds.

AI model

  • IQ control: The “AI IQ” slider effectively sets a speed multiplier (IQ/100) and reduces mistake probability as IQ rises. Buttons bump IQ in ±20 steps with a bounded 30–200 range.
  • Tracking: The AI tries to align with the ball Y position; when a “mistake” triggers, it deliberately biases movement away from the ball for a short, randomized duration.
  • Speed & clamping: AI uses the same paddle speed base as the player scaled by IQ; position is clamped to stay on screen.

Sound design

  • Minimal Web Audio: Simple oscillator tones with short envelopes signal hits (different pitches per paddle), countdown ticks, and “Go!”

Rendering pipeline

  • Animated grid background with a gentle scroll; dashed center net with gradient; neon glows on paddles/ball; per‑frame dimming to produce subtle motion trails.

Extending ideas

  • Add win conditions, serve alternation, multi‑ball modes, or curve shots by modulating spin on subsequent collisions. Difficulty could ramp with speed caps or paddle size penalties when leading.