Project: Lost on the Station
No new theory here: this project uses everything from part 9 and the parts before it. Modules, export default and import, a class with private fields, a getter and a method, destructuring, and buttons made by a script. You build a game of your own from an empty folder: a text adventure. In a text adventure, you find your way through a space station by choosing where to go.
This is a project of its own, outside the station site. When you're not sure how to do a step, open the lesson it comes from again.
Your files
The folder starts empty. Make four files with New file: index.html, style.css, rooms.js and game.js.
The map and the game, apart
The map of the station lives in a module of its own, rooms.js. The game lives in another one, game.js. To make a new adventure, someone changes only the map, and the same game plays it.
The steps
Build it in this order:
-
The frame. An English page that can show any letter and is made for the width of a phone. It has one sentence about the game for search engines, a name of your own on its tab, and one biggest heading.
-
The parts. Under the heading: a paragraph with the id
room-text, a box with the idexits, a paragraph with the idmoves, a paragraph with the idgame-statusthat a screen reader reads out politely, and a button of typebuttonwith the idrestart. -
The map.
rooms.jsexports by default an object with two things.startis the name of the first room.roomsis an object of rooms, by their names. Each room has itstext, and itsexits: an object whose keys are the directions, and whose values are the names of the rooms they lead to. The last room has no exits and hasend: true, like this:export default { start: "kitchen", rooms: { kitchen: { text: "You're in the kitchen.", exits: { east: "garden" } }, garden: { text: "The sun is out. You win!", exits: {}, end: true }, }, };Draw a station of your own, with at least four rooms.
-
The game. The page loads
game.jsas a module, andgame.jsimports the map. A class keeps two things in private fields: the room the player is in, and the number of moves. It gives the room through a getter, and has a method to go in a direction. -
A room. The page shows the room the player is in. Its text goes in
#room-text. In#exits, there is a button of typebuttonfor each of its exits, in the map's order. Each button saysGoand the direction, likeGo north.#movessaysMoves:and the number of moves, 0 at the start. -
Moving. A click on an exit takes the player to the room it leads to, counts one move, and shows the new room.
-
The end. In a room with
end, there are no exit buttons, and#game-statussaysYou made it in 4 moves!, with the real number of moves. -
Start again.
#restarttakes the player back to the first room, with 0 moves and#game-statusempty. -
On a phone. The room's text and the buttons are easy to read, with a contrast of at least 4.5, and nothing sticks out sideways.
The tests also play your page with a map of their own in rooms.js. So the game must work with any map written the same way.
Run the tests whenever you like: they tell you which steps are still missing.