惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
J
Java Code Geeks
博客园 - 聂微东
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
腾讯CDC
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
美团技术团队
博客园 - Franky
Google DeepMind News
Google DeepMind News
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
The Cloudflare Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
I Built Asteroids in ~120 Lines of Vanilla JavaScript
Devanshu Biswas · 2026-06-17 · via DEV Community

Devanshu Biswas

Asteroids feels like it needs a physics engine — momentum, rotation, splitting rocks, screen wrap. It doesn't. The whole thing is velocity vectors, one angle, and a screen that wraps, in about 120 lines of vanilla JavaScript on a canvas.

🎮 Play it: https://dev48v.infy.uk/game/day10-asteroids.html

1. Rotation is just one number

The ship doesn't move left/right — it rotates. Arrow keys nudge a single angle:

if (left)  ship.angle -= 0.07;
if (right) ship.angle += 0.07;

Everything else — which way you thrust, where bullets fly — is derived from that one angle with sin/cos.

2. Thrust adds momentum (you coast)

This is what makes it feel like space. Thrust adds to your velocity, not your position. Let go and you keep drifting:

if (up) {
  ship.vx += Math.cos(ship.angle) * 0.13;
  ship.vy += Math.sin(ship.angle) * 0.13;
}
ship.x += ship.vx; ship.y += ship.vy;
ship.vx *= 0.99; ship.vy *= 0.99;   // gentle "space friction"

3. Trig converts an angle into movement

cos(angle) is the x-component of where you're pointing, sin(angle) is the y. The same pair aims your bullets. Two functions turn "which way am I facing" into "how do I move."

4. The screen wraps (toroidal space)

Fly off one edge, reappear on the other — four lines, applied to the ship, every bullet, and every rock:

const wrap = o => {
  if (o.x < 0) o.x += W; if (o.x > W) o.x -= W;
  if (o.y < 0) o.y += H; if (o.y > H) o.y -= H;
};

5. Splitting rocks = recursion in disguise

Shoot a big rock and it becomes two smaller, faster ones; shoot those and they split again until they're tiny enough to vanish:

if (dist(bullet, rock) < rock.r) {
  if (rock.r > 14) for (let k = 0; k < 2; k++) spawnSmaller(rock);
  rocks.splice(i, 1); score += 20;
}

That cascade is what turns 4 asteroids into a tense, screen-filling swarm.

The takeaway

Arcade games feel complex but are usually a handful of simple rules running 60 times a second. Model the world as plain data, apply the rules each frame, draw. Play it here — the "Understand" tab walks through each step. ☄️