I want to draw a simple maze that is 10x10 in python but I am not sure where to
ID: 3815816 • Letter: I
Question
I want to draw a simple maze that is 10x10 in python but I am not sure where to start. I know I should use recursive to draw rectangles but I am stuck on how to start. I am using a sprite to navigate the maze and here is what I have so far. My animation cycle is broken but I will fix it later! Thanks for any help!
import pygame,sys
from pygame.locals import*
pygame.init()
#WHITE=(255,255,255)
BGimage= 'maze1.png'
DISPLAYSURF=pygame.display.set_mode((1000,600),DOUBLEBUF,32)
BG=pygame.image.load(BGimage).convert()
pygame.display.set_caption("ARGG")
player=pygame.image.load('happyGuy.png').convert_alpha()
player=pygame.transform.scale(player,(125,100))
walk=['happyGuy.png','happy2.png']
x=0
y=0
px=0
py=0
CLOCK=pygame.time.Clock()
count=0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if (event.type == pygame.KEYDOWN):
if (event.key==pygame.K_LEFT):
player=pygame.image.load(walk[count])
player=pygame.transform.scale(player,(125,100))
count=(count +1) % len(walk)
px=-5
if (event.key==pygame.K_RIGHT):
player=pygame.image.load(walk[count])
player=pygame.transform.scale(player,(125,100))
count=(count +1) % len(walk)
px=5
if (event.key==pygame.K_UP):
py=-5
if (event.key==pygame.K_DOWN):
py=5
if (event.type == pygame.KEYUP):
if (event.key==pygame.K_LEFT):
px=0
if (event.key==pygame.K_RIGHT):
px=0
if (event.key==pygame.K_UP):
py=0
if (event.key==pygame.K_DOWN):
py=0
DISPLAYSURF.blit(BG,(0,0))
DISPLAYSURF.blit(player,(x,y))
x+=px
y+=py
CLOCK.tick(50)
pygame.display.update()
Explanation / Answer
NOTE: I recommend you to use classes for your program. One class for player, one for maze, one for the application
I am assuming that the BG image you've put is an indivual block in the maze and not the complete maze. It should be an individual block and you can change the BG to something else
On a paper, note down which places do you want to allow the user to move. Let us assume in our code, user can move where we code it to 1 and user cant move where we code to 0.
In class Maze, we have two functions one for initializing and the other for drawing the maze.
This maze class helps you draw the maze on window. I would suggest you to create another Game Class and call these functions from the game class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.