Displaying the Current Level on the HUD

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Tracking Game Level | TOC | Wrap-Up 👉

Currently, the Heads-Up Display shows the player’s health and current inventory, but it doesn’t indicate the player’s current level. Adding the current level to the HUD can help players feel a sense of achievement when they see the number go up, so let’s add it.

The hud_system handles Heads-Up Display rendering. Open systems/hud.rs, and add the following code (just before you start displaying the player’s inventory):

DeeperDungeons/more_levels/src/systems/hud.rs

​​①​​let​ (player, map_level) = <(Entity, &Player)>::​query​()
​ ​.iter​(ecs)
​ ​.find_map​(|(entity, player)| ​Some​((*entity, player.map_level)))
​ ​.unwrap​();

​​②​draw_batch​.print_color_right​(
​ ​Point​::​new​(SCREEN_WIDTH*2, 1),
​​③​ format!(​"Dungeon Level: {}"​, map_level+1),
​ ​ColorPair​::​new​(YELLOW, BLACK)
​ );

​①​
Locate the Player component and entity.

​②​
print_color_right right justifies text output. All of the text will appear to the left of the specified coordinates.

​③​
format! provides a convenient macro to include the current level in the displayed string. Note that you’re adding +1 to the map level. (Rust programmers tend to start counting at zero, but most people prefer to start…

--

--

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.