Python, a map, and a maze

Python terminal maze

So I was browsing the Learn Code the Hard Way Forum and came across the concept of making a map of x y coordinates, and realized I've never actually attempted something of that nature.



So,  I fired up that Visual Studio Code ,  and had at it. 
I'm trying to figure out why I've never attempted it before.  I think I might have just decided I wasn't ready for it and skipped on to less complicated things,  but today I really could see the fun in making a maze.  While running this prompt, for screen readers, to turn off the ascii just comment out the print_ascii() calls before and in the while loop.



*The ascii map as imaged above may not print pretty in your command/terminal if the font is not the same as mine.  Change it however you want.*

There are a ton of things you could modify and change.  For example, adding a system clear screen so that the map and instructions only appear at the top.
You could add ascii colors, or shapes with ascii codes.
You can make the map bigger by making your grid of 0's and 1's bigger, but don't forget if you want the ascii to match to match up the symbols to the 1's and 0's.
Maybe you want a challenge and find a way to make the maps auto generate and the ascii and the walls and passes.
Whatever it is you want to do, go for it.  Why not?


Gimme the Code





--start code block--
from sys import exit

map = [
    [0, 0, 1, 1, 0],
    [0, 0, 1, 0, 1],
    [1, 0, 0, 1, 1],
    [1, 1, 0, 0, 0],
    [1, 1, 0, 1, 1]   
    ]

ascii = [
    [' U ', '   ', '|#|', '|#|', '   '],
    ['   ', '   ', '|#|', '   ', '|#|'],
    ['|#|', '   ', '   ', '|#|', '|#|'],
    ['|#|', '|#|', '   ', '   ', '   '],
    ['|#|', '|#|', '   ', '|#|', '|#|']
    ]


def print_ascii():
    count = 0
    for item in ascii:
        string = ''
        for symbol in item:
            string = string + symbol
        if count == 0:
           
            print("#" * 19)
        string = "|#" + string + '#|'
        print(string)
        count += 1
        if count == 5:
            print("|#######   #######|")
           
        

def update_ascii(old_coord, new_coord):
    """
    if the move was a pass, replace old_coord with original ascii.
    replace the new coord with player ascii.
    """
    old_x = old_coord[0]
    old_y = old_coord[1]
    new_x = new_coord[0]
    new_y = new_coord[1]
    ascii[old_x][old_y] = '   '
    ascii[new_x][new_y] = ' U '

def get_item_in_map(x, y):
    """
    get the 1, or 0 from our map determined by the x, y coordinates
    """
    print("map coordinates:", x, y)
    return map[x][y]

def return_coords(inc_x, x, inc_y, y):
    """
    Check if the move is valid.  A wall is an invalid move, A pass is valid.
    the max, min is the outter edges of our map.
    It is a 5 by 5 grid,  so 0 by 4 *list indexes* are the limits of each side.
    """
    max = 4
    min = 0
    
    if inc_x + x < min or inc_x + x > max or inc_y + y < min or inc_y + y > max:
        return 'wall'
    else:
        val = get_item_in_map(x + inc_x, y + inc_y)
        if val == 0:
            return 'pass'
        else:
            return 'wall'

def run_maze():
    """
    We start the player at coordinate (0, 0)
    our end point of the maze is coordinate(4, 2)
    """
    x = 0
    y = 0
    up = -1
    down = 1
    left = -1
    right = 1
    # make a list to check for valid input directions
    possible_directions = ['u', 'up', 'd', 'down', 'l', 'left', 'r', 'right' ]
    run = True
    print_ascii()
    while run:

        direction = input("which direction would you like to go? (u)p, (d)own, (l)eft, (r)ight >>  ")
        direction = direction.lower()
        old_coord = (x, y)
        
        if direction in ['exit', 'quit']:
            exit()
        if direction in possible_directions:
            if direction in ['up', 'u']:
                move = return_coords(up, x, 0, y)
                if move == 'pass':
                    #this move is valid, change their x, y coordinates
                    print("You have moved up.")
                    x = x + up
                    new_coord = (x, y)
                    update_ascii(old_coord, new_coord)
                    # y is unchanged
                else:
                    # let them know it is a wall, continue from start of loop.
                    # go back to start of loop without changing x, y coordinates
                    print("There is a wall, you can not go this direction.")
            elif direction in ['down', 'd']:
                move = return_coords(down, x, 0, y)
                if move == 'pass':
                    #this move is valid, change their x, y coordinates
                    print("You have moved down.")
                    x = x + down
                    new_coord = (x, y)
                    update_ascii(old_coord, new_coord)
                    # y is unchanged
                else:
                    print("There is a wall, you can not go this direction.")
                    # let them know it is a wall, continue from start of loop.
                    # go back to start of loop without changing x, y coordinates
            elif direction in ['left', 'l']:
                move = return_coords(0, x, left, y)
                if move == 'pass':
                    print("You moved left")
                    # x is unchanged
                    y = y + left
                    new_coord = (x, y)
                    update_ascii(old_coord, new_coord)
                else:
                    print("There is a wall, you can not go this direction.")
            elif direction in ['right', 'r']:
                move = return_coords(0, x, right, y)
                if move == 'pass':
                    print("You have moved right.")
                    # x is unchanged
                    y = y + right 
                    new_coord = (x, y)
                    update_ascii(old_coord, new_coord)
                else:
                    print("There is a wall, you can not go this direction.")

            else:
                print("This is not a valid direction.  Try (l)eft, (r)ight, (d)own, (u)p letter inputs.")

            #print(x, y)
            print_ascii()
            # lastly, check if they have found the exit before the loop exits on x = 4, y = 2
            if x == 4 and y == 2:
                run = False
                print("You have found the exit, congratulations!" )
                exit()

run_maze()

       


       
   




    

--end code block--








Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame