Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

having trouble getting this python code to work. Just want to make a maze appear

ID: 3817427 • Letter: H

Question

having trouble getting this python code to work. Just want to make a maze appear but i get an error when calling my maze class. First chunk of code is my main the secod is my mase_class.

main

import pygame,sys
from pygame.locals import*
from maze_class import Maze

pygame.init()

WHITE=(255,255,255)
block='square1.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
  
  
  
Maze.draw(DISPLAYSURF,block)
DISPLAYSURF.fill(WHITE)
#DISPLAYSURF.blit(BG,(0,0))
DISPLAYSURF.blit(player,(x,y))
x+=px
y+=py
CLOCK.tick(50)
pygame.display.update()

MAZECLASS!!!!

import pygame,sys
from pygame.locals import*

pygame.init()

class Maze:
def __init__(self):
self.row=10
self.col=10
self.maze=[1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,1,1,1,0,0,1,
1,0,0,1,1,1,1,0,0,1,1,
1,0,0,0,1,1,1,0,0,1,1,
1,0,0,1,1,1,1,1,1,0,1,
1,0,0,1,1,1,1,1,1,0,1,
1,0,0,1,1,1,1,0,0,0,1,
1,1,0,1,1,1,1,0,0,1,1,
1,0,1,0,1,0,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,0]

  
def draw(self,window,block):
x=0
y=0
for i in range(0,self.row*self.col):
if self.maze[x+(y*self.col)]==0:
window.blit(block,(x*44,y*44))
x=x+1
if x&gt:
self.col-1
x=0
y=y+1

Explanation / Answer

Answer:

Note the following:

1. You have not loaded the image 'square1.png'. First load it like as:

--------------------------------------

block=pygame.image.load('square1.png')

----------------------------------------

2. You have not instantiaed the class Maze. Instantiate it like as:

-----------------------------------------

maze=Maze()          
maze.draw(DISPLAYSURF,block)

---------------------------------------

3. Reference to 'gt' in draw method of Maze class is not clear. In what sense you are using it is not clear. It is not initialized also. If it is some variable, it should be created and initialized. If it is some constant from some package, then it should called accordingly. Nither in pygame nor in pygame.local, anything is corresponding to 'gt'. So check it.

Hope this helps you.