Learn to Code via Tutorials on Repl.it!

← Back to all posts
2D console games tutorial!
xolyon (341)
  • really bad title, I know *
    Are you tired of making those text and decision-based RPG games? but feel like your game in PyGame just wouldn't suit?

Well, you've come to the right place, learn how to make, matrixes, dictionaries maps and scenes!

creating your Map/Scene

to start off you want to make a dictionary.
Whats a dictionary? it has two parts a key and a value
dict = {"key" : "value"}
calling the key would return a value

so with this knowledge, we create our scene:

scenes = {
    1: {
 {  "look" : [[map],
  • i am using map instead of 1,1,1,0,0,1 etc because that takes forever and i have no time for it , pretend it has value *

here I named the first scene 1 (an integer) this is so it can be referenced easily later

why another dictionary, this is so we can have more references inside the same scene(let's call these costumes) (for example if it is the night the character might not be able to see anything but if they had the night vision goggles the could)

We are using matrixes for our map for an x,y coordinate position (y being each list and x being each individual object inside the list)

now to print all we have to do is reference all the keys:

and the output would be this:

hmmm, that doesn't look very nice. since Y is the vertical axis and our lists inside the matrix are the y-axis we should stack them and get rid of those pesky commas, we can easily do this like:

for i in scenes[1]["look"]: #i is y-axis
   for l in i: #l is x-axis
                sys.stdout.write(f"{l}" #this prints but on the same line also has to be string not int or error
            print(" ") #when that list is printed it goes to a new line

you have to import sys for this to work
the output would be :

Much better but all those numbers look ugly and unusable so how about we change that around!

Printing your Scene

we can make another dictionary to reference the numbers, you can go wild and do what you want but for the purposes of this tutorial, I am going to keep simple and stick to a maze-style game.

convert = {
    1: "🟥", #wall
    0: " ", #empty space but you can make this grass or carpet etc, it's just something the user can walk on
    3: "🧍", # character
    4: "🟩", #finish or teleport to next scene

you can also add a scene variable to depict which scene the user is on/whats printed
scene = 1#because that's our first scene

now the output would be more like this:

much nicer now isn't it?


input(for the movement we are going to need a few new variables!)

we are going to need 4 new variables for this
x = 1#set to what you want your spawn point to be every scene
y = 1 # same - remember python arrays start at 0
newY = 1 #make these the same as x and y or your game will break
newX = 1

choice = input("where would you like to go> ")
        if choice == "w":
            if maps[map]["look"][y - 1][x] == 1:
                print("you cant go there") #check for obstruction here it is wall
                newY -= 1 #do movement , not y because we need to update pos
# minus because positive y will go down the list

now repeat this for every option.
add in a while loop...(before the printing)
and euraka you are early finished!

now to just update the position and check if they need to move scenes...

Updating Position

if maps[map]["look"][newY][newX] == 4:
            # map += 1 #for if you have more maps
            print("the end")  # if u got new maps
            exit() #u could just ad if statement to exit but because i have no more maps
        maps[map]["look"][y][x] = 0  # replaces old position
        x = newX #updares all positions
        y = newY 
        maps[map]["look"][y][x] = 3  # moves character to new position

and now we are truly finished yay
Hope you enjoyed advanced tutorial coming soon!


because of how fast the console clears you may want to create a function to clear it to keep it clean :

import os
import sys #dont if you already have

def Clear():
   os.system("clear") #cls in windows

#and call


Advanced coming soon with way more concepts here's a teaser: you can make procedural generation by randomly randinting each object in the list and to leave open space to maximize the number of walls etc!

this is basic knowledge but it should be enough to build of and do cool things like battles or platformers (PyGame is better for that) I 

made this because I personally struggled with stuff like this when I wanted to make a game in the. early days and all the current projects were so complex i couldn't comprehend them.