Moving conf & func in a new file

And renames some const/var to facilitate the reorganisation
TODO finalise the general use of ELEMENT_LIST (suppr. 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 21:12:31 +01:00
parent 4ebc10c2ab
commit 5ae9de6060
3 changed files with 26 additions and 86 deletions

7
func.py Normal file
View File

@ -0,0 +1,7 @@
"""
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)
"""

36
main.py
View File

@ -10,33 +10,14 @@ Main file for [_ocp3_ project](https://github.com/freezed/ocp3)
See [README](https://github.com/freezed/ocp3/blob/master/README.md) for
details
"""
from os import system
import pygame
from pygame.locals import (
K_UP, K_DOWN, K_RIGHT, K_LEFT, KEYDOWN, QUIT, RESIZABLE
)
from map import Map, MAZE_ELEMENTS, MAZE_SIZE
# Configuration
CELL_SIZE_PX = 30
MAZE_ELEMENTS_TILES = {
'wall': 'img/transp-30.png',
'exit': 'img/g-orange-transp-30.png',
'plyr': 'img/player-30.png',
'void': 'img/blue-white-30.png',
'tube': 'img/1-blue-transp-30.png',
'needle': 'img/2-blue-transp-30.png',
'ether': 'img/3-blue-transp-30.png',
}
BACKGROUND_IMG = 'img/brick-800.png'
UNKNOWN_TILE = 'img/unknown-30.png'
MAP_FILE = '01.map'
MAZE_SIZE_CEL = MAZE_SIZE
GAME_KEYS = [K_UP, K_DOWN, K_RIGHT, K_LEFT]
WINDOW_SIZE_PX = CELL_SIZE_PX * MAZE_SIZE_CEL
from map import Map
from conf import BACKGROUND_IMG, CELL_SIZE_PX, ELEMENT_LIST, MAP_FILE, MAZE_ELEMENTS, MAZE_ELEMENTS_TILES, MAZE_SIZE_TIL
# FUNCTIONS
def maze_draw():
""" Take a map string and generate a graphic maze """
back_tiles = []
@ -51,13 +32,17 @@ def maze_draw():
else:
back_tiles.append(pygame.image.load(UNKNOWN_TILE).convert())
x = (cell % MAZE_SIZE_CEL) * CELL_SIZE_PX
y = (cell // MAZE_SIZE_CEL) * CELL_SIZE_PX
x = (cell % MAZE_SIZE_TIL) * CELL_SIZE_PX
y = (cell // MAZE_SIZE_TIL) * CELL_SIZE_PX
WINDOW.blit(back_tiles[cell], (x, y))
# Refresh
pygame.display.flip()
# MAIN SCRIPT
GAME_KEYS = [K_UP, K_DOWN, K_RIGHT, K_LEFT]
WINDOW_SIZE_PX = CELL_SIZE_PX * MAZE_SIZE_TIL
pygame.init()
WINDOW = pygame.display.set_mode(
@ -86,13 +71,10 @@ while MAP_GAME.status:
if event.key in GAME_KEYS:
MAP_GAME.move_to(event.key)
else:
MAP_GAME.status = False
# system('clear')
print("status_message:{}".format(MAP_GAME.status_message))
# MAP_GAME.map_print()
# Draw maze
maze_draw()

69
map.py
View File

@ -7,58 +7,9 @@ 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
# CONFIGURATION
# Error message
ERR_MAP = "ERR_MAP: «{}»"
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': True, 'ressurect': False, 'collect': False, 'tile': 'd'},
{'symbol': ' ', 'name': 'void', 'cross': True, 'ressurect': True, 'collect': False, 'tile': 'e'},
{'symbol': '.', 'name': 'wall', 'cross': False, 'ressurect': False, 'collect': False, 'tile': 'f'},
{'symbol': 'X', 'name': 'player', 'cross': False, 'ressurect': False, 'collect': False, 'tile': 'g'},
{'symbol': '\n', 'name': 'nlin', 'cross': False, 'ressurect': False, 'collect': False, 'tile': 'h'},
]
MAZE_SIZE = 15
# Issue possible d'un mouvement, garder le OK toujours en fin de liste
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…"
}
MSG_START_GAME = "Welcome in OCP3.\nUse arrow keys to play, any other key to quit."
# 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/:
"""
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]
# CLASS
class Map:
"""
@ -92,9 +43,9 @@ class Map:
map_in_a_list = map_data.read().splitlines()
# map line number
if len(map_in_a_list) == MAZE_SIZE:
self._COLUM = MAZE_SIZE + 1
self._LINES = MAZE_SIZE
if len(map_in_a_list) == MAZE_SIZE_TIL:
self._COLUM = MAZE_SIZE_TIL + 1
self._LINES = MAZE_SIZE_TIL
self._MAXIM = (self._COLUM * self._LINES) - 1
# Add map checking here
@ -111,7 +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'] == True)
collectables = (element['symbol'] for element in ELEMENT_LIST if element['collect'] is True)
for symbol_to_place in collectables:
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)
@ -140,8 +91,6 @@ class Map:
:param str pressed_key: mouvement souhaite
:return int: une cle de la constante MOVE_STATUS
"""
from pygame.locals import K_UP, K_DOWN, K_RIGHT, K_LEFT
# supprime le plyr de son emplacement actuel et on replace
# l'elements «dessous»
self._map_in_a_string = self._map_in_a_string.replace(
@ -198,6 +147,8 @@ class Map:
:param str element: element a placer sur la carte
"""
# FIXME cannot find a way to define default value to the
# method's arguments with class attributes
if 'pos' in kwargs:
pos = kwargs['pos']
else:
@ -216,9 +167,9 @@ class Map:
Checks if a line has a good length, fill it if it's too small,
truncate if it's too long
"""
differance = MAZE_SIZE - len(str(line))
differance = MAZE_SIZE_TIL - len(str(line))
if differance < 0:
return line[:MAZE_SIZE]
return line[:MAZE_SIZE_TIL]
elif differance > 0:
return line + (differance * elmt_val('symbol', 'name', 'void', 0))
else: