网页
综合
更新时间 2026-02-09 15:00:00
Build a dinosaur runner game with Deno, pt. 5 | Deno
自动蜘蛛
deno.com
正文约 3855 字
This series of blog posts will guide you through building a simple browser-based dinosaur runner game using Deno.
In Stage 4, we wired Oak, PostgreSQL, and a leaderboard route so every run could be recorded. The UI was still anonymous though: everyone shared the same dino, the same desert background, and you had to hard refresh the page each time you wanted to switch difficulty. In Stage 5, we’ll keep that leaderboard code, but layer on player identity and customization so the experience matches what ships in the game-tutorial-stage-4 repository.
By the end of this stage you will have:
Start by extending the markup in public/index.html. We add three things near the game canvas:
The modal triggers call helper functions we’ll add in game.js so there’s no framework dependency—plain DOM APIs keep things deploy-friendly.
Layer in the modal utility classes inside public/css/styles.css. They reuse the existing btn palette from earlier stages, so everything feels cohesive.
Feel free to tweak the palette! The logic we’ll add next pulls the chosen colors right into the canvas background and dino sprite.
With the UI in place we can extend the DinoGame class inside public/js/game.js, the key additions are:
showPlayerNamePrompt() looks for saved state; if none is found it opens the modal and focuses the input.
Once a player name exists we look up settings on the server, fall back to localStorage, and immediately apply the palette + difficulty multipliers.
Instead of wiring everything through class instances, game.js exposes a few functions on window. They open/close modals, persist names, and populate the customization controls.
Hook everything up inside window.addEventListener("load", ...) so the health check runs, the DinoGame instance is created, and your modals are ready the moment the DOM finishes loading.
Stage 4 already introduced a database connection pool, migrations runner, and a high_scores table. To store customization data we add players and player_settings tables to src/database/schema.sql:
high_scores still stores anonymous runs (player_name), but if someone has a registered player_id we can connect their customization + leaderboard data in one query.
All API logic lives in src/routes/customization.routes.ts. We reuse the ctx.state.db pool set by middleware, so every handler can grab a client from the pool, execute queries, and release connections.
There’s also a simple /api/customization/options handler that returns available themes, colors, and difficulty presets for future UI work.
The rest of the backend is Oak boilerplate. src/middleware/database.ts attaches the pool to every request. src/main.ts initializes the schema, registers middleware, and mounts the routers before falling back to the static file server:
With those pieces in place the leaderboard API, customization API, and static assets all share a single Deploy project (or run locally via deno task dev).
You can view and edit your PostgreSQL databases directly from the Deno Deploy dashboard. This is especially useful for inspecting the players, player_settings, and high_scores tables as you test your game. To access the database:
At this point you should have a fully functional game with player identities, customizable themes, and persistent preferences. To validate everything is wired up correctly:
You can explore the full reference implementation and Deploy link here: https://game-tutorial-stage-4.thisisjofrank.deno.net/
Stage 6 will concentrate on observability—metrics, logging, and alerting—so you can understand how players behave at scale. We’ll wire up structured logs, shipping analytics events, and dashboards that track leaderboard health and customization adoption. For now, enjoy the custom player experience you just delivered! 🦕✨
What else are you building with Deno? Let us know on Twitter, Bluesky, or Discord.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。