网页
综合
更新时间 2025-12-08 15:00:00
Build a dinosaur runner game with Deno, pt. 1 | Deno
自动蜘蛛
deno.com
正文约 3609 字
This series of blog posts will guide you through building a simple browser-based dinosaur runner game using Deno.
The first thing we’ll need in order to build a browser-based game is a web server to serve our static files (HTML, CSS, JS). In Stage 1, we will set up a basic Deno project that serves these files locally and we’ll deploy it to the web with Deno Deploy.
Keep reading and build along or view the entire source here.
Setting up a basic Deno project is straightforward. We can use the deno init command to create a new project folder with a deno.json configuration file.
This will set you up with a new directory called dino-runner containing a deno.json file where you can define tasks and configurations for your Deno project.
We’re going to build out a basic server that serves static files from a public/ directory and will serve API requests in later stages.
Set up the following structure in your new project, create empty files as needed, we’ll fill them in as we go:
First up, we’ll install the packages that we’ll need to use in this project. Firstly we’ll need the Oak framework to help us set up the server.
Then we’ll add some tasks to our deno.json file to help us run the server easily. In your deno.json, overwrite the tasks section with the following:
Lets set up some environment variables in our newly created .env file at the root the project folder:
Next we’ll set up a basic index.html file in the public/ folder with links to the assets we’ll use later:
We’ll create a very simple server using the Oak framework to serve static files from the public/ directory.
This code spins up a local HTTP server that maps incoming requests directly to your /public folder. Deno.cwd() gets the current working directory, ensuring that the server correctly locates the public/ folder regardless of where the script is run from.
You should now be able to run the server locally with:
This should show you a very basic web page when you navigate to http://localhost:8000 in your browser:
Next, we’ll set up a basic routing structure for our API endpoints. For now, this file will be mostly empty, but it sets the stage for future development.
This sets up a simple health check endpoint at /api/health that we can use to verify that our server is running correctly.
Then we’ll update the main server file to use this router. First, add an import for the router at the top of src/main.ts:
Then, after the app.use for static files, add the following:
This tells the Oak application to use the routes defined in apiRouter.
Now, when you run the server, you can check the health endpoint by navigating to http://localhost:8000/api/health:
Let’s deploy your new project to Deno Deploy, so that you can share it with the world!
First, make sure you have a Deno Deploy account. If you don’t have one, you can sign up at https://console.deno.com/.
Then, you can use the deno deploy command to deploy your project:
This command will guide you through the deployment process. It will ask you to select a project name and will upload your code to Deno Deploy. Make sure to select “Edit app config” and set the entry point for your application to src/main.ts:
Once the deployment is complete, you’ll receive a URL where your application is live.
You can visit this URL in your browser to see your basic HTML page ready to be developed into a dinosaur runner game!
Stage 2 introduces enhanced features — more interactivity, more refined structure, and the beginnings of real game logic. We’ll publish it next week! Stay tuned.
What are you building with Deno? Let us know on Twitter, Bluesky, or Discord.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。