def __init__(self, x, y, screen): """ Constructor of this class. Parameters: :param x : the character's position x on the labyrinth's configuration. :type x : int. :param y : the character's position y on the labyrinth's configuration. :type y : int. :param screen : the game screen. :type screen : pygame.surface.Surface The constructor allows you to create a Guardian object with an image and a position. """ super(Guardian, self).__init__() self.screen = screen self.image = pygame.image.load(path_to_image('Gardien.png'))\ .convert_alpha() self.image = pygame.transform.scale(self.image, (20, 20)) self.rect = self.image.get_rect() self.x = x self.y = y self.pos = self.rect.move(x, y)
def __init__(self, file, screen): """ Constructor of this class. Parameters : :param file : a file of the labyrinth configuration. :type file : str :param screen : the game screen. :type screen : pygame.surface.Surface The constructor create a labyrinth with an image for each element, a wall list, a list of empty positions and a configuration. """ self.screen = screen self.fichier = file self.l_wall = [] self.l_none = [] self.config = [] self.wall = pygame.image.load(path_to_image('wall.png')).convert()
def __init__(self, image, list_, screen): """ Constructor of this class. Parameters : :param image : the image of the object we want to create. :type image : str :param list_ : position list where the object can be positioned. :type : list :param screen : the game screen. :type screen : pygame.surface.Surface The constructor create a Labobject object with an image and a random position. """ super(Labobject, self).__init__() self.screen = screen path = path_to_image(image) self.image = pygame.image.load(path).convert_alpha() self.image = pygame.transform.scale(self.image, (20, 20)) self.rect = self.image.get_rect() self.pos = random.choice(list_)
def main(): """Run the game.""" pygame.init() # Screen creation: screengame = pygame.display.set_mode((300, 300)) # Generate and diplay the labyrinth: labyrinth = Lab(path_to_labyrinth(), screengame) labyrinth.generate_lab() # Draw the characters and the objects on the labyrinth: player = create_character(labyrinth.config, "P", Player, screengame) guardian = create_character(labyrinth.config, "G", Guardian, screengame) path_ether = path_to_image('ether.png') ether = Labobject(path_ether, labyrinth.l_none, screengame) erase_pos_object(labyrinth.l_none, ether) path_needle = path_to_image('aiguille.png') needle = Labobject(path_needle, labyrinth.l_none, screengame) erase_pos_object(labyrinth.l_none, needle) path_pipe = path_to_image('tube.png') pipe = Labobject(path_pipe, labyrinth.l_none, screengame) erase_pos_character(labyrinth.config, "P") while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() old_posx = player.pos.x old_posy = player.pos.y if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player.move_up() if event.key == pygame.K_DOWN: player.move_down() if event.key == pygame.K_RIGHT: player.move_right() if event.key == pygame.K_LEFT: player.move_left() if player.pos.colliderect(ether.pos): player.obj1 = True if player.pos.colliderect(needle.pos): player.obj2 = True if player.pos.colliderect(pipe.pos): player.obj3 = True if player.pos.collidelist(labyrinth.l_wall) != -1: player.pos.x = old_posx player.pos.y = old_posy if player.pos.colliderect(guardian.my_rect()): if player.obj1 and player.obj2 and player.obj3: player.pos.y = guardian.pos.y + 20 print("YOU WIN") sys.exit() else: print("YOU LOOSE") sys.exit() time.sleep(1 / 60) labyrinth.display_lab() if not player.obj1: ether.draw_me() if not player.obj2: needle.draw_me() if not player.obj3: pipe.draw_me() guardian.draw_me() player.draw_me() pygame.display.flip()