diff --git a/README.md b/README.md index 4ed3f92..8a36755 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ guard without all the objects, he dies (according to the evolution theory). 3. Recovering objects * [x] Add object management * [x] The way he pick them up - * [ ] Add a counter that will list them. + * [x] Add a counter that will list them. 4. Win! * [x] _McGyver_ has picked up all the objects and asleep the guard diff --git a/conf.py b/conf.py index 9049c65..73302c2 100644 --- a/conf.py +++ b/conf.py @@ -5,6 +5,8 @@ Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/ This file is part of [_ocp3_ project](https://github.com/freezed/ocp3) """ +import pygame +import math # CONFIGURATION ELEMENT_LIST = [ @@ -30,6 +32,7 @@ MOVE_STATUS_MSG = { CELL_SIZE_PX = 30 # Size of the tiles, in pixels MAZE_SIZE_TIL = 15 # Size of a map, in tiles +FONT_SIZE = math.floor(0.9 * CELL_SIZE_PX) BLACK = (0, 0, 0) BLUE = (0, 0, 128) GREEN = (0, 255, 0) @@ -38,13 +41,23 @@ WHITE = (255, 255, 255) # Messages CAPTION = "OCP3, a pygame maze" ERR_MAP = "ERR_MAP: «{}»" -MSG_START_GAME = "Welcome in OCP3.\nUse arrow keys to play, any other key to quit." +HEADER_MESSAGES = { + 'title': "Welcome in OCP3.", + 'status': "Use arrow keys to play, any other key to quit.", + 'items': "Items: {}/{}", +} # Files BACKGROUND_IMG = 'img/zebra-800.png' MAP_FILE = '01.map' UNKNOWN_TILE = 'img/unknown-30.png' +# Constant calculation +HEADER_HEIGHT = (2 * CELL_SIZE_PX) +WINDOW_SIZE_PX_W = CELL_SIZE_PX * MAZE_SIZE_TIL +WINDOW_SIZE_PX_H = WINDOW_SIZE_PX_W + HEADER_HEIGHT +WIN_DIM = (WINDOW_SIZE_PX_W, WINDOW_SIZE_PX_H) + # FUNCTIONS @@ -72,7 +85,6 @@ def elmt_val(kval, ksel, vsel, nline=False): def maze_draw(WINDOW, map_string): """ Take a map string and generate a graphic maze """ - import pygame back_tiles = [] for cell, element in enumerate(map_string): img = elmt_val('tile', 'symbol', element, 0) @@ -83,8 +95,30 @@ def maze_draw(WINDOW, map_string): back_tiles.append(pygame.image.load(img).convert_alpha()) x = (cell % MAZE_SIZE_TIL) * CELL_SIZE_PX - y = (cell // MAZE_SIZE_TIL) * CELL_SIZE_PX + (2 * CELL_SIZE_PX) + y = (cell // MAZE_SIZE_TIL) * CELL_SIZE_PX + HEADER_HEIGHT WINDOW.blit(back_tiles[cell], (x, y)) # Refresh pygame.display.flip() + + +def set_header(WINDOW, messages): + """ + Set the header message on the window + + :param obj WINDOWS: display object + :param list/str messages: list of messages per place + """ + pygame.draw.rect(WINDOW, BLACK, (0, 0, WINDOW_SIZE_PX_W, HEADER_HEIGHT)) + + FONT = pygame.font.Font(None, FONT_SIZE) + + h_title = FONT.render(str(messages['title']), True, BLUE, WHITE) + h_status = FONT.render(str(messages['status']), True, WHITE, BLACK) + h_items = FONT.render(str(messages['items']), True, GREEN, BLACK) + + h_items_pos = h_items.get_rect(topright=(WINDOW_SIZE_PX_W, 0)) + + WINDOW.blit(h_title, (0, 0)) + WINDOW.blit(h_status, (0, CELL_SIZE_PX)) + WINDOW.blit(h_items, h_items_pos) diff --git a/main.py b/main.py index 278c2e1..b554b88 100644 --- a/main.py +++ b/main.py @@ -11,43 +11,30 @@ See [README](https://github.com/freezed/ocp3/blob/master/README.md) for details """ import pygame -import math from pygame.locals import ( K_UP, K_DOWN, K_RIGHT, K_LEFT, KEYDOWN, QUIT, RESIZABLE ) from map import Map -from conf import BACKGROUND_IMG, BLACK, BLUE, CAPTION, CELL_SIZE_PX, elmt_val, GREEN, MAP_FILE, maze_draw, MAZE_SIZE_TIL, WHITE +from conf import BACKGROUND_IMG, CAPTION, MAP_FILE, HEADER_HEIGHT, maze_draw, set_header, WIN_DIM # Constant calculation GAME_KEYS = [K_UP, K_DOWN, K_RIGHT, K_LEFT] -WINDOW_SIZE_PX_H = CELL_SIZE_PX * MAZE_SIZE_TIL -WINDOW_SIZE_PX_V = WINDOW_SIZE_PX_H + (2 * CELL_SIZE_PX) -WIN_DIM = (WINDOW_SIZE_PX_H, WINDOW_SIZE_PX_V) -FONT_SIZE = math.floor(0.9 * CELL_SIZE_PX) pygame.init() WINDOW = pygame.display.set_mode(WIN_DIM, RESIZABLE) pygame.display.set_caption(CAPTION) -WINDOW.blit(pygame.image.load(BACKGROUND_IMG).convert(), (0, (2 * CELL_SIZE_PX))) +WINDOW.blit(pygame.image.load(BACKGROUND_IMG).convert(), (0, HEADER_HEIGHT)) # Loading map MAP_GAME = Map(MAP_FILE) # Header messaging -h_msg = MAP_GAME.status_message.splitlines() -FONT = pygame.font.Font(None, FONT_SIZE) -h1_txt = FONT.render(h_msg[0], True, WHITE, BLACK) -h2_txt = FONT.render(h_msg[1], True, WHITE, BLACK) -WINDOW.blit(h1_txt, (0, 0)) -WINDOW.blit(h2_txt, (0, CELL_SIZE_PX)) +set_header(WINDOW, MAP_GAME.status_message) # Draw maze maze_draw(WINDOW, MAP_GAME.map_print().replace('\n', '')) -# system('clear') -print(MAP_GAME.status_message) -# MAP_GAME.map_print() - +pygame.time.Clock().tick(25) # Game loop while MAP_GAME.status: for event in pygame.event.get(): @@ -61,7 +48,6 @@ while MAP_GAME.status: else: MAP_GAME.status = False - print("status_message:{}".format(MAP_GAME.status_message)) - + set_header(WINDOW, MAP_GAME.status_message) # Draw maze maze_draw(WINDOW, MAP_GAME.map_print().replace('\n', '')) diff --git a/map.py b/map.py index ee88f5c..45d4254 100644 --- a/map.py +++ b/map.py @@ -8,7 +8,7 @@ This file is part of [_ocp3_ project](https://github.com/freezed/ocp3) import os import random from pygame.locals import K_UP, K_DOWN, K_RIGHT, K_LEFT -from conf import elmt_val, ERR_MAP, MOVE_STATUS_MSG, MSG_START_GAME, MAZE_SIZE_TIL +from conf import elmt_val, ERR_MAP, MOVE_STATUS_MSG, HEADER_MESSAGES, MAZE_SIZE_TIL class Map: @@ -67,17 +67,22 @@ class Map: self.place_element(symbol_to_place, pos=position) self.status = True - self.status_message = MSG_START_GAME self.collected_items = [] + self.collected_items_num = 0 + self.MAX_ITEMS = sum(1 for _ in elmt_val('name', 'collect', True)) + self.status_message = {} + self.status_message['title'] = HEADER_MESSAGES['title'] + self.status_message['status'] = HEADER_MESSAGES['status'] + self.status_message['items'] = HEADER_MESSAGES['items'].format(self.collected_items_num, self.MAX_ITEMS) else: self.status = False - self.status_message = ERR_MAP.format("maplines: " + str(len(map_in_a_list))) + print(ERR_MAP.format("maplines: " + str(len(map_in_a_list)))) # Erreur de chargement du fichier else: self.status = False - self.status_message = ERR_MAP.format(':'.join(["mapfile", map_file])) + print(ERR_MAP.format(':'.join(["mapfile", map_file]))) def map_print(self): """ Affiche la carte avec la position de jeu courante """ @@ -118,26 +123,28 @@ class Map: if next_char == elmt_val('symbol', 'name', 'void', 0): self._player_position = next_position - self.status_message = MOVE_STATUS_MSG['ok'] + self.status_message['status'] = MOVE_STATUS_MSG['ok'] elif next_char in elmt_val('symbol', 'collect', True): self._player_position = next_position - self.status_message = MOVE_STATUS_MSG['collect'].format(elmt_val('name', 'symbol', next_char, 0)) self._element_under_player = elmt_val('symbol', 'name', 'void', 0) self.collected_items.append(elmt_val('name', 'symbol', next_char, 0)) + self.collected_items_num += 1 + self.status_message['status'] = MOVE_STATUS_MSG['collect'].format(elmt_val('name', 'symbol', next_char, 0)) + self.status_message['items'] = HEADER_MESSAGES['items'].format(self.collected_items_num, self.MAX_ITEMS) elif next_char == elmt_val('symbol', 'name', 'exit', 0): self.status = False if sorted(self.collected_items) == sorted(elmt_val('name', 'collect', True)): - self.status_message = MOVE_STATUS_MSG['winner'] + self.status_message['status'] = MOVE_STATUS_MSG['winner'] else: missed_item_flist = ', '.join((item for item in elmt_val('name', 'collect', True) if item not in self.collected_items)) - self.status_message = MOVE_STATUS_MSG['looser'].format(missed_item_flist) + self.status_message['status'] = MOVE_STATUS_MSG['looser'].format(missed_item_flist) else: # wall, door or nline - self.status_message = MOVE_STATUS_MSG['wall'] + self.status_message['status'] = MOVE_STATUS_MSG['wall'] else: - self.status_message = MOVE_STATUS_MSG['wall'] + self.status_message['status'] = MOVE_STATUS_MSG['wall'] # place le plyr sur sa nouvelle position self.place_element(elmt_val('symbol', 'name', 'player', 0))