Currently making a maze game. Maze is drawn, everything is drawn with pygame.draw.rect. I have tried using pygame.coliderect() to check for colisions, but it isn't working. The current error message I keep receiving says that my player rectangle (drawn with a class called Sprite) has no attribute 'colliderect'
The code below shows how I am currently drawing the maze and character, and how I am attempting to call colliderect. Please explain to me what it is I am doing wrong or why this code isn't working.
# Hit variable to tell if the player is touching the wall# hit = False #Draw command for moving box set to Sprite# class Sprite: def __init__(self,x,y,width,height): self.x=x self.y=y self.width=15 self.height=15 #Used to draw the character to the screen def render(self): pygame.draw.rect(screen,YELLOW,(self.x,self.y,self.width,self.height)) sprite1 = Sprite(30,40,15,15) moveX,moveY=0,0 #Create the Maze class Maze: #Maze class to make maze creation and editing easier def __init__(self): self.W = 20 #Sets the width of the map self.H = 10 # Sets the heigth of the map #Create a series of 1s and 0s to indicate where there is and isn't a wall. #0 means nothing is there or the space is 0pen (pun intended) #1 means something is there or no 1 shall pass. self.maze = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,1,0,0,0,1,0,0,1,0,0,1,0,1, 1,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,1,1, 1,1,0,1,1,1,1,1,1,0,0,1,0,1,0,1,0,0,1,1, 1,1,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1, 1,0,0,1,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,1, 1,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1, 1,1,1,0,1,1,0,1,1,0,0,0,1,1,1,1,1,1,0,1, 1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1, 0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1] def draw(self): bx = 0 #Used to loop horizontally through the 1s and 0s as the game draws by = 0 #Used to loope vertically through the 1s and 0s as the game draws self.surface = screen self.color = BLUE for i in range(0, self.W*self.H): if self.maze[ bx + (by*self.W) ] == 1: pygame.draw.rect(self.surface, self.color, ( bx * 25, by * 25, 25, 25)) bx = bx + 1 if bx > self.W-1: bx = 0 by = by + 1 maze = Maze() ## check if the player is still in the maze## if sprite1.colliderect(maze): hit = True else: hit = False ##reverse player's movement if they hit the wall## if hit == True: moveX *= -1 moveY *= -1 # --- Drawing code should go here screen.fill(BLACK) # First, clear the screen to black. Don't put other drawing commands # above this, or they will be erased with this command. maze.draw() sprite1.x += moveX sprite1.y += moveY sprite1.render() # --- Go ahead and update the screen with what we've drawn. pygame.display.flip()