diff --git a/conf.py b/conf.py index 7614c56..c4af4d1 100644 --- a/conf.py +++ b/conf.py @@ -6,7 +6,6 @@ Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/ This file is part of [_ocp3_ project](https://github.com/freezed/ocp3) """ from math import floor -import pygame ELEMENTS = ( {'symbol': 'n', 'name': 'needle', 'cross': True, 'item': True, @@ -24,7 +23,7 @@ ELEMENTS = ( {'symbol': 'X', 'name': 'player', 'cross': False, 'item': False, 'tile': 'img/macgyver.png'}, {'symbol': '\n', 'name': 'nlin', 'cross': False, 'item': False, - 'tile': False}, + 'tile': None}, ) CELL_SIZE = 30 # Size of the tiles, in pixels diff --git a/gui.py b/gui.py index d13819a..fdbe543 100644 --- a/gui.py +++ b/gui.py @@ -39,11 +39,12 @@ class GraphUI: pygame.image.load(img_file).convert_alpha(), position ) - def draw(self, maze): + def draw(self, maze, messages): """ Take a maze string and generate a graphic maze :param obj maze: a Maze object + :param list/str messages: list of messages for header """ for cell, element in enumerate(maze.string.replace('\n', '')): img = elmt_val('tile', 'symbol', element, 0) @@ -55,6 +56,8 @@ class GraphUI: else: self.blit(img, (x, y)) + self.set_header(messages) + # Refresh pygame.display.flip() @@ -63,7 +66,7 @@ class GraphUI: Set the header message on the window :param obj surface: surface object - :param list/str messages: list of messages per place + :param list/str messages: list of messages per emplacement """ pygame.draw.rect(self.SURFACE, BLACK, (0, 0, WIN_SIZE_W, HEAD_SIZE_H)) @@ -84,21 +87,31 @@ class GraphUI: :param obj player: a Player object """ changed_tiles = [ - (elmt_val('tile', 'symbol', player.ground, 0), player.old_position), - (elmt_val('tile', 'name', 'player', 0), player.current_position), + ( + elmt_val('tile', 'symbol', player.ground, 0), + player.old_position + ), + ( + elmt_val('tile', 'name', 'player', 0), + player.current_position + ), ] - # [print('tile[0]:{} tile[1]:{}'.format(tile[0], tile[1])) for num, tile in enumerate(changed_tiles) if tile[0] is not False] - [self.blit(tile[0], self.coord_from_index(tile[1])) for num, tile in enumerate(changed_tiles) if False not in tile] + [self.blit(tile[0], self.coord_from_index(tile[1])) + for num, tile in enumerate(changed_tiles) if None not in tile] # Refresh pygame.display.flip() - def coord_from_index(self, idx): + @staticmethod + def coord_from_index(idx): """ Gets 2 dimensions coordinates - Converts a string index (the position in the maze.string: beware of EOL) to 2 dimensions coordinates (necessary for positionning objects in pygame) + Converts a string index (the position in the maze.string: beware + of EOL) to 2 dimensions coordinates (necessary for positionning + objects in pygame) + :param int index: string index """ diff --git a/main.py b/main.py index 38de63a..328cb9c 100644 --- a/main.py +++ b/main.py @@ -23,39 +23,37 @@ GAME_KEYS = [K_UP, K_DOWN, K_RIGHT, K_LEFT] last_message = False # do not execute last message loop # initialize maze with file -game_maze = Maze(MAZE_FILE) +GAME_MAZE = Maze(MAZE_FILE) # running graphic user interface & initialize player -if game_maze.status: - macgyver = Player(game_maze) - gui = GraphUI() - # TODO include set_header() in draw() - gui.set_header(macgyver.status_message) - gui.draw(game_maze) +if GAME_MAZE.status: + MACGYVER = Player(GAME_MAZE) + GUI = GraphUI() + GUI.draw(GAME_MAZE, MACGYVER.status_message) # game loop -while game_maze.status: +while GAME_MAZE.status: for event in pygame.event.get(): if event.type == QUIT: - game_maze.status = False + GAME_MAZE.status = False last_message = False if event.type == KEYDOWN: last_message = True # executes last_message loop if event.key in GAME_KEYS: - macgyver.key_event(event.key) - gui.set_header(macgyver.status_message) - gui.update(macgyver) + MACGYVER.key_event(event.key) + GUI.set_header(MACGYVER.status_message) + GUI.update(MACGYVER) else: - macgyver.status_message['status'] = MSG_QUIT - game_maze.status = False + MACGYVER.status_message['status'] = MSG_QUIT + GAME_MAZE.status = False # displays the last_message (won, lost or quit) while last_message: - macgyver.status_message['title'] = MSG_END - gui.set_header(macgyver.status_message) + MACGYVER.status_message['title'] = MSG_END + GUI.set_header(MACGYVER.status_message) pygame.display.flip() for event in pygame.event.get(): if event.type == KEYDOWN: diff --git a/player.py b/player.py index 586e307..abeb5f4 100644 --- a/player.py +++ b/player.py @@ -27,7 +27,7 @@ class Player: self.current_position = maze.string.find( elmt_val('symbol', 'name', 'player', 0) ) - self.old_position = 0 + self.old_position = None # Element under player, default 'floor' self.ground = elmt_val('symbol', 'name', 'floor', 0) @@ -125,10 +125,20 @@ class Player: # for all other element (wall or nline) else: self.status_message['status'] = MSG_WALL - self.old_position = False + self.old_position = None # out the string range else: self.status_message['status'] = MSG_WALL - self.old_position = False + self.old_position = None + + # Replaces player symbol in maze.string by 'ground' value + self.maze.string = self.maze.string.replace( + elmt_val('symbol', 'name', 'player', 0), self.ground + ) + + # Sets the player's new position in maze.string + self.maze.set_symbol( + elmt_val('symbol', 'name', 'player', 0), self.current_position + )