From ed5380376f269c4eaa6ee5a46f1f511b297f102e Mon Sep 17 00:00:00 2001 From: Fred Z Date: Wed, 4 Apr 2018 14:40:05 +0200 Subject: [PATCH] Sets up file error (presence or line nb) Clean exit and handling error when a map file do not exist or have a wrong number of line (acording to the constant MAZE_SIZE in the conf TODO if a file error raises, do not warmup pygame --- conf.py | 3 ++- main.py | 15 ++++++----- map.py | 81 ++++++++++++++++++++++++++++++--------------------------- 3 files changed, 52 insertions(+), 47 deletions(-) diff --git a/conf.py b/conf.py index 35bcd5b..21bdeed 100644 --- a/conf.py +++ b/conf.py @@ -52,7 +52,8 @@ WHITE = (255, 255, 255) # Messages CAPTION = "OCP3, a maze based on pygame" -ERR_MAP = "ERR_MAP: «{}»" +ERR_FILE = "Map filename is not avaiable: «{}»" +ERR_LINE = "Map file has wrong lines number: «{}»" HEAD_MESSAGES = { 'title': "Welcome in OCP3.", 'status': "Use arrow keys to play, any other key to quit.", diff --git a/main.py b/main.py index 60c31e9..6c41567 100644 --- a/main.py +++ b/main.py @@ -21,6 +21,7 @@ from conf import ( ) GAME_KEYS = [K_UP, K_DOWN, K_RIGHT, K_LEFT] +last_wait = False pygame.init() WINDOW = pygame.display.set_mode(WIN_DIM) @@ -29,13 +30,13 @@ WINDOW.blit(pygame.image.load(BACKGRND_FILE).convert(), (0, HEAD_SIZE_H)) # Loading map MAP_GAME = Map(MAP_FILE) -set_header(WINDOW, MAP_GAME.status_message) -maze_draw(WINDOW, MAP_GAME.map_print().replace('\n', '')) # Game loop -# pygame.time.Clock().tick(25) -last_wait = True +pygame.time.Clock().tick(25) while MAP_GAME.status: + last_wait = True + set_header(WINDOW, MAP_GAME.status_message) + maze_draw(WINDOW, MAP_GAME.map_print().replace('\n', '')) for event in pygame.event.get(): if event.type == QUIT: MAP_GAME.status = False @@ -53,12 +54,12 @@ while MAP_GAME.status: # Draw maze maze_draw(WINDOW, MAP_GAME.map_print().replace('\n', '')) -MAP_GAME.status_message['title'] = MSG_END -set_header(WINDOW, MAP_GAME.status_message) -pygame.display.flip() # Loop for last messag before exit while last_wait: + MAP_GAME.status_message['title'] = MSG_END + set_header(WINDOW, MAP_GAME.status_message) + pygame.display.flip() for event in pygame.event.get(): if event.type == KEYDOWN: last_wait = False diff --git a/map.py b/map.py index 205b829..dfe3106 100644 --- a/map.py +++ b/map.py @@ -9,7 +9,7 @@ import os import random from pygame.locals import K_UP, K_DOWN, K_RIGHT, K_LEFT from conf import ( - elmt_val, ERR_MAP, MSG_COLLECT, MSG_LOSER, MSG_OK, + elmt_val, ERR_FILE, ERR_LINE, MSG_COLLECT, MSG_LOSER, MSG_OK, MSG_WALL, MSG_WINNER, HEAD_MESSAGES, MAZE_SIZE ) @@ -41,66 +41,69 @@ class Map: # Loading map file if os.path.isfile(map_file) is False: self.status = False - print(ERR_MAP.format(':'.join(["mapfile", map_file]))) + print(ERR_FILE.format(map_file)) else: with open(map_file, "r") as map_data: splited_map = map_data.read().splitlines() - self.check_file(splited_map) + if self.check_file(splited_map): - # Builds a square map (if end-line spaces are missing in the file) - self._map_in_a_string = '\n'.join( - (self.check_line(line) for line in splited_map) - ) + # Builds a square map (if end-line spaces are missing in the file) + self._map_in_a_string = '\n'.join( + (self.check_line(line) for line in splited_map) + ) - # Gets player initial position - self._player_position = self._map_in_a_string.find( - elmt_val('symbol', 'name', 'player', 0) - ) + # Gets player initial position + self._player_position = self._map_in_a_string.find( + elmt_val('symbol', 'name', 'player', 0) + ) - # Defines Element under player at start - self._element_under_player = elmt_val('symbol', 'name', 'void', 0) + # Defines Element under player at start + self._element_under_player = elmt_val('symbol', 'name', 'void', 0) - # Place collectables on the map - for symbol_to_place in elmt_val('symbol', 'collect', True): - position = random.choice( - [idx for (idx, value) in enumerate( - self._map_in_a_string - ) if value == elmt_val( - 'symbol', 'name', 'void', 0 - )]) - self.place_element(symbol_to_place, pos=position) + # Place collectables on the map + for symbol_to_place in elmt_val('symbol', 'collect', True): + position = random.choice( + [idx for (idx, value) in enumerate( + self._map_in_a_string + ) if value == elmt_val( + 'symbol', 'name', 'void', 0 + )]) + self.place_element(symbol_to_place, pos=position) - self.MAX_ITEMS = sum(1 for _ in elmt_val('name', 'collect', True)) - self._COLUM = MAZE_SIZE + 1 # List starts to zero - self._MAXIM = (self._COLUM * MAZE_SIZE) - 1 + self.MAX_ITEMS = sum(1 for _ in elmt_val('name', 'collect', True)) + self._COLUM = MAZE_SIZE + 1 # List starts to zero + self._MAXIM = (self._COLUM * MAZE_SIZE) - 1 - self.status = True - self.collected_items = [] - self.collected_items_num = 0 + self.status = True + self.collected_items = [] + self.collected_items_num = 0 - self.status_message = {} - self.status_message['title'] = HEAD_MESSAGES['title'] - self.status_message['status'] = HEAD_MESSAGES['status'] - self.status_message['items'] = HEAD_MESSAGES['items'].format( - self.collected_items_num, self.MAX_ITEMS - ) + self.status_message = {} + self.status_message['title'] = HEAD_MESSAGES['title'] + self.status_message['status'] = HEAD_MESSAGES['status'] + self.status_message['items'] = HEAD_MESSAGES['items'].format( + self.collected_items_num, self.MAX_ITEMS + ) - def check_file(self, splited_map): + else: + self.status = False + + @staticmethod + def check_file(splited_map): """ Checks the map conformity before starting the game :param list/str splited_map: Map splited in a list (line = index) """ - self.status = False - if len(splited_map) != MAZE_SIZE: - print(ERR_MAP.format("maplines: " + str(len(splited_map)))) + print(ERR_LINE.format(len(splited_map))) + return False # ++Add other checks here: elements inside, exit possible, etc++ else: - self.status = True + return True def map_print(self): """ Return a string of the map state """