Finalise the general use of ELEMENT_LIST

Suppress MAZE_ELEMENTS_TILES & MAZE_ELEMENTS
TODO move maze_draw() to func.py
FIXME walk over collectables items
This commit is contained in:
Fred Z 2018-03-24 22:24:11 +01:00
parent 5ae9de6060
commit f021ab5fa2
4 changed files with 74 additions and 13 deletions

View File

@ -44,12 +44,12 @@ guard without all the objects, he dies (according to the evolution theory).
* [x] Represent the guard, _McGyver_ and the objects in your program
and placed them at the beginning of the game
2. Animate the character
* [ ] The only moving element is _McGyver_, create classes methods for
* [x] The only moving element is _McGyver_, create classes methods for
animation and finding the exit
* [x] make a simplified version of the game in which _McGyver_ wins
when he arrives in front of the goalkeeper
3. Recovering objects
* [ ] Add object management
* [x] Add object management
* [ ] The way he pick them up
* [ ] Add a counter that will list them.
4. Win!

65
conf.py Normal file
View File

@ -0,0 +1,65 @@
"""
Author: freezed <freezed@users.noreply.github.com> 2018-02-11
Version: 0.1
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
This file is part of [_ocp3_ project](https://github.com/freezed/ocp3)
"""
# CONFIGURATION
ELEMENT_LIST = [
{'symbol': 'n', 'name': 'needle', 'cross': True, 'ressurect': False, 'collect': True, 'tile': 'img/3-blue-transp-30.png'},
{'symbol': 't', 'name': 'tube', 'cross': True, 'ressurect': False, 'collect': True, 'tile': 'img/1-blue-transp-30.png'},
{'symbol': 'e', 'name': 'ether', 'cross': True, 'ressurect': False, 'collect': True, 'tile': 'img/2-blue-transp-30.png'},
{'symbol': 'E', 'name': 'exit', 'cross': False, 'ressurect': False, 'collect': False, 'tile': 'img/g-orange-transp-30.png'},
{'symbol': ' ', 'name': 'void', 'cross': True, 'ressurect': True, 'collect': False, 'tile': 'img/blue-white-30.png'},
{'symbol': '.', 'name': 'wall', 'cross': False, 'ressurect': False, 'collect': False, 'tile': 'img/transp-30.png'},
{'symbol': 'X', 'name': 'player', 'cross': False, 'ressurect': False, 'collect': False, 'tile': 'img/player-30.png'},
{'symbol': '\n', 'name': 'nlin', 'cross': False, 'ressurect': False, 'collect': False, 'tile': False},
]
# Possible returns of a move, keep the «ok» at the end
MOVE_STATUS = ['bad', 'wall', 'exit', 'door', 'ok']
MOVE_STATUS_MSG = {
'bad': "Le déplacement «{}» n'est pas autorisé.",
'wall': "Le déplacement est stoppé par un mur.",
'exit': "Vous êtes sortit du labyrinte",
'door': "Vous passez une porte",
'ok': "Jusqu'ici, tout va bien…"
}
CELL_SIZE_PX = 30 # Size of the tiles, in pixels
MAZE_SIZE_TIL = 15 # Size of a map, in tiles
# Messages
ERR_MAP = "ERR_MAP: «{}»"
MSG_START_GAME = "Welcome in OCP3.\nUse arrow keys to play, any other key to quit."
# Files
BACKGROUND_IMG = 'img/zebra-800.png'
MAP_FILE = '01.map'
UNKNOWN_TILE = 'img/unknown-30.png'
# FUNCTIONS
def elmt_val(kval, ksel, vsel, nline=False):
"""
Return value(s) from ELEMENT_LIST
:param str kval: key of the value returned
:param str ksel: key of the selection criteria
:param str vsel: value of the selection criteria
:param bool/int nline: Default False, return value(s) in a list,
use a int() to return the `n` value from the list
:return str/bool/:
"""
try:
if nline is False:
return (element[kval] for element in ELEMENT_LIST if element[ksel] == vsel)
else:
return [element[kval] for element in ELEMENT_LIST if element[ksel] == vsel][nline]
except IndexError:
return False

13
main.py
View File

@ -15,22 +15,19 @@ from pygame.locals import (
K_UP, K_DOWN, K_RIGHT, K_LEFT, KEYDOWN, QUIT, RESIZABLE
)
from map import Map
from conf import BACKGROUND_IMG, CELL_SIZE_PX, ELEMENT_LIST, MAP_FILE, MAZE_ELEMENTS, MAZE_ELEMENTS_TILES, MAZE_SIZE_TIL
from conf import BACKGROUND_IMG, CELL_SIZE_PX, elmt_val, MAP_FILE, MAZE_SIZE_TIL
# FUNCTIONS
def maze_draw():
""" Take a map string and generate a graphic maze """
back_tiles = []
for cell, element in enumerate(MAP_GAME.map_print().replace('\n', '')):
key = [key for (key, val) in MAZE_ELEMENTS.items() if val == element][0]
img = elmt_val('tile', 'symbol', element, 0)
if key in MAZE_ELEMENTS_TILES:
back_tiles.append(
pygame.image.load(MAZE_ELEMENTS_TILES[key]).convert_alpha()
)
else:
if img is False:
back_tiles.append(pygame.image.load(UNKNOWN_TILE).convert())
else:
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

5
map.py
View File

@ -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 ELEMENT_LIST, elmt_val, ERR_MAP, MOVE_STATUS_MSG, MSG_START_GAME, MAZE_SIZE_TIL
from conf import elmt_val, ERR_MAP, MOVE_STATUS_MSG, MSG_START_GAME, MAZE_SIZE_TIL
class Map:
@ -62,8 +62,7 @@ class Map:
self._element_under_player = elmt_val('symbol', 'name', 'void', 0)
# Place collectables on the map
collectables = (element['symbol'] for element in ELEMENT_LIST if element['collect'] is True)
for symbol_to_place in collectables:
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)