Building a Dungeon

Hands-on Rust — by Herbert Wolverson (47 / 120)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Adding the Adventurer | TOC | Graphics, Camera, Action 👉

You’ve built an empty map and have a player who can walk around it. The structure is in place to handle walls, so it’s time to add a simple dungeon. In this section, you’ll learn to randomly place rooms, connect them with corridors, and build a dungeon for your adventurer to explore.

Create a Map Builder Module

Make another new file named map_builder.rs. This file will contain the map building module. Add it to your prelude in main.rs:

BasicDungeonCrawler/dungeon_crawl_rooms/src/main.rs

​ ​mod​ map;
»​mod​ map_builder;
​ ​mod​ player;

​ ​mod​ prelude {
​ ​pub​ ​use​ ​bracket_lib​::​prelude​::*;
​ ​pub​ ​const​ SCREEN_WIDTH: i32 = 80;
​ ​pub​ ​const​ SCREEN_HEIGHT: i32 = 50;
​ ​pub​ ​use​ ​crate​::​map​::*;
​ ​pub​ ​use​ ​crate​::​player​::*;
» ​pub​ ​use​ ​crate​::​map_builder​::*;
​ }

Open map_builder.rs. Start by importing the main prelude, and defining a constant representing the maximum number of rooms you’d like to allow in your dungeon. Twenty rooms yield a nice looking dungeon. Experiment with it:

BasicDungeonCrawler/dungeon_crawl_rooms/src/map_builder.rs

​ ​use​ ​crate​::​prelude​::*;
​ ​const​ NUM_ROOMS: usize = 20;

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.