How to Create A 90s Era Game In Terminal

For my second assignment at Coder Academy’s Bootcamp, I had to create a terminal game. I ended up recreating the first level of Pokemon Ruby. I have received a lot of requests as to how I built it since then so here is how I went about it.

The code is written in Ruby. (This may have played a part in choosing which game to recreate..)

Objective

To create a terminal game where you will:
-Print a two-dimensional map
-Make the map look visually nice
-Place a character on said map
-Move the character throughout the map
-Have the character interact with different things on the map
-Change maps when the character enters a new area
-Conditionally load the map based on the character’s progress

Don’t worry if you don’t understand what some of that is or how we will do it, I’ll explain it as we go. Have a little video of the final project we will build:

Print an Identifiable Map

The map will be stored as a two-dimensional array (basically, an array containing multiple arrays). This will let us print in two directions — up and down, left and right.

map = [
['Grass', 'House', 'Grass'],
['Grass', 'House', 'Grass'],
['Tree', 'Grass', 'Tree']
]

If we run the print command, we will see:

Now, this doesn’t look good! What is with all these quote marks and brackets?? Let’s modify the print statement a little bit and take the chance to move it into a function.

def print_map(map)
for row in map
for column in row
print column
end
end
end
print_map(map)

What we’re doing here is using our map to get each row and column and then print it. We could call these anything we want, but because we’re using a map it makes more sense to use this naming convention. This returns us with:

Making progress! But this still isn’t quite right. We need our map to be in two dimensions — left and right, up and down. Luckily, we can change this with the smallest tweak.

def print_map(map)
for row in map
for column in row
print column
end
print "\n"
end
end
print_map(map)

What does this little line do? "\n" is a special character. Note that it is in double quotation marks — for Ruby to print these special characters, it requires double, not single quote marks. This is the symbol for a new line. We placed it after the for column in row but within the for row in map command. This means it will execute at the end of the row before the beginning of the new row. Let’s see what we end up with:

Okay, now we have a two-dimensional map printed out on the screen. That concludes step one.

Make the map look visually nice

First, we want to install the gem colorize. In terminal, we type gem install colorize and at the top of the file, require 'colorize'

Now, we can use the colorize gem to colorize things! For example,
print 'grass'.colorize(:red) Now we want to colorize our map.

But because we want to do it in multiple different colors, we have to introduce a legend for our map.

Since for every different type of symbol there is (eg, grass, tree, house) a different thing needs to be printed, we need to use a conditional for loop to iterate over the map.

def print_map(map)
for row in map
for column in row
case column
when 'Grass'
print '██'.colorize(:light_black)
when 'House'
print '██'.colorize(:cyan)
when 'Tree'
print '██'.colorize(:green)
end
end
print "\n"
end
end

Now, our map doesn’t look like words anymore. However, it doesn’t look much like a map at all either — it’s much too small. Go ahead and design your map however you want now.

The outside blue lines I created when I couldn’t find an easy way to visually represent the map’s walls and entrances/exits. Overall, we have completed our objective of printing a map that looks like a map.

But there’s one thing we can do now that will make it look absolutely amazing: emojis 😲!! You just need to copy-paste them where you want them. For example:

case column
when 'Tree'
print '🌲'
when 'Grass'
print '🌿'
when 'House'
print '██'.colorize(:cyan)
end

Emojis are great for conveying images but they are limited. There isn’t an emoji for everything and only come in one size. That means that larger objects (such as a house) cannot be represented by an emoji. But they can transform your printout when used correctly.

Alright! The map is printed and looks visually appealing. Right now it is a static image, but stay tuned for part two where we will introduce our player and movement!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store