# How do I create a maze without drawing each individual line or using sprites?

I am currently trying to create a maze game by following some online tutorials, and putting my own spin on them. I found a pretty good tutorial at pythonspot.com, but I am not sure what some of the code means, and if I have to use images and sprites to create this maze.

The piece that is confusing me is this:

class Maze:
def __init__(self):
self.M = 10
self.N = 8
self.maze = [ 1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,1,1,1,1,1,1,0,1,
1,0,1,0,0,0,0,0,0,1,
1,0,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,]

def draw(self,display_surf,image_surf):
bx = 0
by = 0
for i in range(0,self.M*self.N):
if self.maze[ bx + (by*self.M) ] == 1:
display_surf.blit(image_surf,( bx * 44 , by * 44))

bx = bx + 1
if bx &gt; self.M-1:
bx = 0
by = by + 1


I know that you can make a maze with 1s and 0s to print to the screen, and this is easier for me to make levels with, because I can visualize them in the 1s and 0s.

Do I have to use images like the tutorial is (display_surf.blit(image_surf)), or can I use pygame.draw to create a square for use multiple times as a wall?

I was also wondering what the self.M, self.N, bx and by variables do

I have tried looking at other mazes, on the Internet, but they tend to either draw all of the walls as lines in individual draw statements without using 1s and 0s, or they use sprites to create the player and walls. This leads me to believe that I have to use sprites for this drawing method, but I thought I would check here to make sure. I would ask the creator of the tutorial, but I can not create an account for the website, and can not ask questions or leave comments without an account.

• He is just using a square sprite to make it look less pale. Of course you can do it by drawing rectangles. – Bálint Jan 7 '17 at 0:31
• Okay, so my second part of my question would be how do I replace his image_surf and blit code with a basic pygame.draw statement. I assume that is where the variables that I don't understand come into play. – N.Huggett Jan 7 '17 at 0:34

Yes, you can use simple rectangles instead of the sprites. The tutorial uses sprites to probably make it more interesting.

self.M is width of the map and self.N is the height. bx and by are used to loop trough the tiles. Basically it scans trough the tiles from left to right, and when it reaches the right side it goes 1 line lower. It basically works like an old typewriter.

I seriously have no idea why he chose to do this instead if 2 simple nested for loops, or why he uses such obscure variable names in a tutorial.

Your other question is about replacing the current draw function to pygame.draw. Looking at the code it looks like the tiles are 44 pixels wide (I take this opportunity to say that such hardcoded values are bad), so you can just draw a rectangle from (bx * 44; by * 44) to ((bx + 1) * 44; ((by + 1) * 44). I don't know pygame, but I assume it's something like this:

DISPLAY = pygame.display.set_mode((500,400),0,32)
pygame.draw.rect(DISPLAY, (255, 0, 0), (bx * 44, by * 44, 44, 44)


Edit: I actually looked it up, this should work.

• hey i don't get it can you pls explain – Johnny Sep 11 '18 at 8:45
• @Johnny if you can describe in detail where you got lost, we can target explanations to better help you. Since you're a new user without commenting privileges yet, you can as a new question using the "Ask Question" button, and in your question link to this one. Describe step by step what you've tried and where you're having trouble putting this answer into practice. – DMGregory Sep 11 '18 at 10:42