Build a maze with Python

Here's the code snippets in Repo form if you prefer...https://github.com/KenoLeon/Medium-Mazes
Notes: We are using square grids to make our life easier, so a grid with cellCount 10 is a 10x10 grid. There are drawGrid() and drawCell(coordinates) methods for drawing the grid and placing a single cell. In order to read a numpy array as the map for the grid, uncomment the placeCells() call, should give you a populated grid based on the random numpy cellMap array.
How it works: We now have a new variable: _VARS['playerPos': [x, y]] which is simply the player position, when you press the up/down/right/left arrows, this variable get's modified (add/subtract 40 to the x, y coordinate so it snaps to the grid), then we clear and redraw the grid and player and voila we have player control and movement. 
In order to enforce some boundaries around the grid, we need to modify the event loop, so on the upper limit it would look like this:if (_VARS['playerPos'][1] - 40 >= 0)... if the position is greater than zero we can draw the change in position, else we don't, for the opposite end we use 400 as a limit since our gridSize is 400.
The logic is identical to the one used for the outer grid walls but here we read the numpy array and check if the cell in question is not 1 ( which represents a wall ), here for instance we check before moving to the left if there's a wall:if cellMAP[yPos][xPos-1] != 1Note: There's a few other changes in this script, we now have an orange tile that stands for the player along with less hardcoded values, so you can experiment by changing the size and cell count for instance.
# This snippet creates a random maze and clears the entry and exit points.cellMAP = np.random.randint(2, size=(_VARS['cellCount'], _VARS['cellCount']))cellMAP[0][0] = 0
cellMAP[5][5] = 0
Just like the Kobayashi Maru !

Next steps & conclusion.

Speaking of improvements, if you were making a game you might want to use classes instead of a global dictionary to store things like position and command the player to move, so you would have a player class and even a grid/maze class with class methods and properties ( playerPos, movePlayer, createMaze etc, etc), I chose not to use classes because while handy they add another abstraction layer that might get in the way of learning the basics, in any case there should be plenty of examples of small games that do use classes out there to complement your learning, I didn't look at other maze tutorials while making this, but you probably should if you want a second or third opinion.

AI, Software Developer, Designer : www.k3no.com

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