Terminal–based game in 150 lines

submited by
Style Pass
2024-09-25 01:30:03

In this article we will write a terminal–based real–time dungeon crawler. I will to keep it under 150 lines with idiomatic (no code golfing!) Ruby code.

I want to start with something basic. We need a class that will contain our game state and draw the screen. Let’s create a game.rb with the following content:

Our next goal is to render a level map. Let’s keep the map in the file called map.txt, which will be in the same directory as a game.rb. We need five types of objects:

Note that we do not handle start location, final location and enemies yet. Instead, we treat them as empty spaces: since we will have a specific logic tied to them, we will add it later.

The only thing it does is just prints whatever we have in in the map field. The only thing that might be unfamiliar to you is system "clear", which just cleans up the terminal.

Now we need to render player, enemies and door. Let’s extend our Level class definition to support them and decide how we will draw them on the screen:

Leave a Comment