View the step-by-step solution to:

I am unable to get maze.py into a list so that every time player collides with maze it will move the player away from the maze wall. Can you help me

I am unable to get maze.py into a list so that every time player collides with maze it will move the player away from the maze wall.

Can you help me out?. The "*******" shows that they are different files. Also self.maze didn't come out right when copying and pasting but that doesn't really matter


import pygame

import sys

import player

import move

import maze


def main():

   main_window = move.Move()

   main_window.mainloop()

main()


**********************************************


import sys

import pygame

import player

import maze


class Move:

   def __init__(self, width=800, height=600):

       pygame.init()

       self.width = width

       self.height = height

       self.screen = pygame.display.set_mode((self.width, self.height))

       #self.background = pygame.Surface(self.screen.get_size()).convert()

       "This will init all the classes needed"

       self.player = player.Player(0,0,'player.png')

       self.maze = maze.Maze()

       self.maze.game_loop()

       self.walls = self.maze.return_walls()

       print(self.walls)

       self.background = self.maze.game_display

       self.all_sprites = pygame.sprite.Group(self.player)

   def mainloop(self):

       #pygame.key.set_repeat(1,50)

       while True:

           #self.background.fill((0, 0, 0))

           for event in pygame.event.get():

               if event.type == pygame.QUIT:

                   sys.exit()

               #wall = pygame.sprite.spritecollide(self.player, self.maze, True)

               #if (wall) == False:

               if event.type == pygame.KEYDOWN:

                   if(event.key == pygame.K_UP):

                       self.player.move_up()

                       down = self.player.collided(self.walls)

                       if (down) == False:

                           self.player.move_down()

                   elif(event.key == pygame.K_DOWN):

                       self.player.move_down()

                       up = self.player.collided(self.walls)

                       if (up) == False:

                           self.player.move_up()

                   elif(event.key == pygame.K_LEFT):

                       self.player.move_left()

                       right = self.player.collided(self.walls)

                       if (right) == False:

                           self.player.move_right()

                   elif(event.key == pygame.K_RIGHT):

                       self.player.move_right()

                       left = self.player.collided(self.walls)

                       if (left) == False:

                           self.player.move_left()

           self.screen.blit(self.background,(0,0))

           self.all_sprites.draw(self.screen)

           pygame.display.flip()



*****************************************************


import pygame


pygame.init()

global black, white, red

white = (255,255,255)

black = (0,0,0)

red = (255, 0, 0)

display_width = 800

display_height = 600

#gameDisplay = pygame.display.set_mode((display_width,display_height))



class Maze(object):

   def __init__(self):

       pygame.sprite.Sprite.__init__(self)

       self.WALL_LENGTH = 10

       self.x=self.y=self.x2=self.y2=self.START= 10

       self.game_display = pygame.Surface((display_width,display_height))

       self.walls = []

   def draw_line(self,surface,color,start_pos,end_pos,width):

       pygame.draw.line(surface,color,start_pos,end_pos,width)


   def game_loop(self):

       self.maze = "

       +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|n

       |       |    |                   |    |    |n

       | +--+ | + +--+ + +--+ +--+--+ + +--+ |n

       | |    | |       | |             | |    |n

       | +--+ + | +--+--| | +--+--+--+--+ + + |n

       |    |    | |    | | |             | | |n

       +--+ +--+--+--+ + +--+ +--+--+--+--+--+ | |n

       |    |          |                         | |n

       | + | +--+--+--+--+--+--+--+--+--+--+--| +--|n

       | | | |       |                      |    |n

       | +--+ | +--+ + +--+--+--+--+--+--+ | + |n

       |       | |                |       | | | |n

       | +--+--| +--+--+--+--+--+ + + +--+ | | |n

       |       |             |    | |       | | |n

       +--+--+ +--+--+--+--| | +--+ +--+--+--+ | |n

       |                   | |          |       | |n

       | ---| +--+--+--| + +--+--+--+ + +--+--+ |n

       |    | | |    | |    |    | | |       |n

       +--+ | + | + | +--+ +--+ + + | +--+--|n

       |    |    | | |    |    |       |       |n

       | +--| +--+ | +--+ +--+ +--+--+--+--+--+--|n

       |             |          |                   |n

       +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--"



       gameExit = False

       self.game_display.fill(white)

       self.linecount = 0

       for i in self.maze:

           if i == "-" or i == "+":

               self.x2+=self.WALL_LENGTH

               self.draw_line(self.game_display,(0,0,0),(self.x,self.y),(self.x2,self.y2),5)



               self.walls.append(self.draw_line(self.game_display,(0,0,0),(self.x,self.y),(self.x2,self.y2),5))

               self.x+=self.WALL_LENGTH


           elif i == "n":


               self.linecount+=1

               if(self.linecount % 2 == 0):

                   self.y+=self.WALL_LENGTH*2

                   self.y2=self.y


               self.x=self.START

               self.x2=self.START

           elif i == "|":

               self.y2-=self.WALL_LENGTH*2

               self.draw_line(self.game_display,black,(self.x,(self.y+20)),(self.x2,(self.y2+20)),5)

               self.walls.append(self.draw_line(self.game_display,black,(self.x,(self.y+20)),(self.x2,(self.y2+20)),5))

               self.x+=self.WALL_LENGTH

               self.x2=self.x

               self.y2=self.y


           elif i == " ":

               self.x+=self.WALL_LENGTH

               self.x2=self.x



   def return_walls(self):

       walls = self.walls


       return walls


********************************************************

import pygame


global red

red = (255,0,0)


class Player(pygame.sprite.Sprite):

   """

   This class represent the player image

   and has the player actions.

   """


   def __init__(self, x, y, img_file):

       # Pygame constructor

       pygame.sprite.Sprite.__init__(self)

       picture = pygame.image.load(img_file)

       self.image = pygame.transform.scale(picture, (10, 10))

       #self.image = pygame.image.load(picture).convert()

       self.rect = self.image.get_rect()

       # Init. variables

       self.rect.x = x

       self.rect.y = y

       self.speed = 10

       print('player created')


   def move_up(self):

       self.rect.y += self.speed

   def move_down(self):

       self.rect.y -= self.speed

   def move_left(self):

       self.rect.x -= self.speed

   def move_right(self):

       self.rect.x += self.speed

   def collided(self,walls):

       if self.rect.collidelist(walls) == -1:

           return False

       else:

           return True

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question