Rename every occurences of map by maze

Map is a keyword and I using ma & maze in the script may be confusing
This commit is contained in:
Fred Z 2018-04-04 18:51:23 +02:00
parent 2fa3e6ff86
commit bc024f9a9c
5 changed files with 63 additions and 63 deletions

View File

View File

@ -36,10 +36,10 @@ Feel free to contribute, I do not use other OS
* `main.py`, main script the one to run
* `conf.py`, place for variables, constants and fuctions
* `map.py`, map class
* `maze.py`, maze class
* `README.md`, you're reading it!
* `requirement.txt`, dependences for feeding pip
* `01.map`, file of the map
* `01.maze`, file of the maze
* `img/`, place for images (tiles) files
### Features
@ -58,7 +58,7 @@ the exit from the maze. If he stupid enough to comes in front of the the
guard without all the objects, he dies (according to the evolution theory).
* The program will be standalone, i. e. it can be run on any computer.
### Roadmap
### Roadmaze
1. Create the starting frame
* [x] Initialize a Git repo on Github

16
conf.py
View File

@ -44,7 +44,7 @@ ELEMENTS = (
)
CELL_SIZE = 30 # Size of the tiles, in pixels
MAZE_SIZE = 15 # Size of a map, in tiles
MAZE_SIZE = 15 # Size of a maze, in tiles
BLACK = (0, 0, 0)
BLUE = (0, 0, 128)
GREEN = (0, 255, 0)
@ -52,8 +52,8 @@ WHITE = (255, 255, 255)
# Messages
CAPTION = "OCP3, a maze based on pygame"
ERR_FILE = "Map filename is not avaiable: «{}»"
ERR_LINE = "Map file has wrong lines number: «{}»"
ERR_FILE = "Maze filename is not avaiable: «{}»"
ERR_LINE = "Maze file has wrong lines number: «{}»"
HEAD_MESSAGES = {
'title': "Welcome in OCP3.",
'status': "Use arrow keys to play, any other key to quit.",
@ -69,7 +69,7 @@ MSG_WINNER = "Cogratulations! You asleep the guard."
# Files
BACKGRND_FILE = 'img/back-800.png'
MAP_FILE = '01.map'
MAZE_FILE = '01.maze'
UNKNOWN_FILE = 'img/unknown-30.png'
# Constant calculation
@ -105,15 +105,15 @@ def elmt_val(kval, ksel, vsel, nline=False):
return False
def maze_draw(surface, map_string):
def maze_draw(surface, maze_string):
"""
Take a map string and generate a graphic maze
Take a maze string and generate a graphic maze
:param obj surface: a pygame surface object
:param str map_string: map modelized in a string
:param str maze_string: maze modelized in a string
"""
back_tiles = []
for cell, element in enumerate(map_string):
for cell, element in enumerate(maze_string):
img = elmt_val('tile', 'symbol', element, 0)
if img is False:

28
main.py
View File

@ -14,19 +14,19 @@ import pygame
from pygame.locals import (
K_UP, K_DOWN, K_RIGHT, K_LEFT, KEYDOWN, QUIT
)
from map import Map
from maze import Maze
from conf import (
BACKGRND_FILE, CAPTION, MAP_FILE, HEAD_SIZE_H, maze_draw,
BACKGRND_FILE, CAPTION, MAZE_FILE, HEAD_SIZE_H, maze_draw,
MSG_END, MSG_QUIT, set_header, WIN_DIM
)
GAME_KEYS = [K_UP, K_DOWN, K_RIGHT, K_LEFT]
last_message = False # Do not execute last message loop
# Loading map
MAP_GAME = Map(MAP_FILE)
# Loading maze
MAZE_GAME = Maze(MAZE_FILE)
if MAP_GAME.status:
if MAZE_GAME.status:
pygame.init()
pygame.time.Clock().tick(25)
pygame.display.set_caption(CAPTION)
@ -34,27 +34,27 @@ if MAP_GAME.status:
WINDOW.blit(pygame.image.load(BACKGRND_FILE).convert(), (0, HEAD_SIZE_H))
# Game loop
while MAP_GAME.status:
set_header(WINDOW, MAP_GAME.status_message)
maze_draw(WINDOW, MAP_GAME.map_print())
while MAZE_GAME.status:
set_header(WINDOW, MAZE_GAME.status_message)
maze_draw(WINDOW, MAZE_GAME.maze_print())
for event in pygame.event.get():
if event.type == QUIT:
MAP_GAME.status = False
MAZE_GAME.status = False
last_message = False
if event.type == KEYDOWN:
last_message = True # Execute last_message loop
if event.key in GAME_KEYS:
MAP_GAME.move_to(event.key)
MAZE_GAME.move_to(event.key)
else:
MAP_GAME.status_message['status'] = MSG_QUIT
MAP_GAME.status = False
MAZE_GAME.status_message['status'] = MSG_QUIT
MAZE_GAME.status = False
# Allows reading the last_message (won, lost or quit)
while last_message:
MAP_GAME.status_message['title'] = MSG_END
set_header(WINDOW, MAP_GAME.status_message)
MAZE_GAME.status_message['title'] = MSG_END
set_header(WINDOW, MAZE_GAME.status_message)
pygame.display.flip()
for event in pygame.event.get():
if event.type == KEYDOWN:

View File

@ -14,59 +14,59 @@ from conf import (
)
class Map:
class Maze:
"""
Provides a usable map from a text file
Checks the map compatibility
Provides a usable maze from a text file
Checks the maze compatibility
Moves the player to it
"""
def __init__(self, map_file):
def __init__(self, maze_file):
"""
Initialise map
Initialise maze
The Map object has given attributes:
The Maze object has given attributes:
:var int status: move status (what append after a move command)
:var str status_message: feedback message for player
:var lst splited_map: splited map in a list
:var str _map_in_a_string: map string
:var lst splited_maze: splited maze in a list
:var str _maze_in_a_string: maze string
:var str _element_under_player: Element under player
:var int _player_position: Player index in _map_in_a_string
:var int _player_position: Player index in _maze_in_a_string
:param map_file: map filename
:rtype map: str()
:param maze_file: maze filename
:rtype maze: str()
:return: None
"""
# Loading map file
if os.path.isfile(map_file) is False:
# Loading maze file
if os.path.isfile(maze_file) is False:
self.status = False
print(ERR_FILE.format(map_file))
print(ERR_FILE.format(maze_file))
else:
with open(map_file, "r") as map_data:
splited_map = map_data.read().splitlines()
with open(maze_file, "r") as maze_data:
splited_maze = maze_data.read().splitlines()
if self.check_file(splited_map):
if self.check_file(splited_maze):
# 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 maze (if end-line spaces are missing in the file)
self._maze_in_a_string = '\n'.join(
(self.check_line(line) for line in splited_maze)
)
# Gets player initial position
self._player_position = self._map_in_a_string.find(
self._player_position = self._maze_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)
# Place collectables on the map
# Place collectables on the maze
for symbol_to_place in elmt_val('symbol', 'collect', True):
position = random.choice(
[idx for (idx, value) in enumerate(
self._map_in_a_string
self._maze_in_a_string
) if value == elmt_val(
'symbol', 'name', 'void', 0
)])
@ -91,32 +91,32 @@ class Map:
self.status = False
@staticmethod
def check_file(splited_map):
def check_file(splited_maze):
"""
Checks the map conformity before starting the game
Checks the maze conformity before starting the game
:param list/str splited_map: Map splited in a list (line = index)
:param list/str splited_maze: Maze splited in a list (line = index)
"""
if len(splited_map) != MAZE_SIZE:
print(ERR_LINE.format(len(splited_map)))
if len(splited_maze) != MAZE_SIZE:
print(ERR_LINE.format(len(splited_maze)))
return False
# ++Add other checks here: elements inside, exit possible, etc++
else:
return True
def map_print(self):
""" Return a string of the map state """
return self._map_in_a_string.replace('\n', '')
def maze_print(self):
""" Return a string of the maze state """
return self._maze_in_a_string.replace('\n', '')
def move_to(self, pressed_key):
"""
Move the player on the map
Move the player on the maze
:param str pressed_key: direction (pygame const)
"""
# Replace player on the map by the under-element
self._map_in_a_string = self._map_in_a_string.replace(
# Replace player on the maze by the under-element
self._maze_in_a_string = self._maze_in_a_string.replace(
elmt_val('symbol', 'name', 'player', 0),
self._element_under_player
)
@ -136,7 +136,7 @@ class Map:
# Next position treatment
if next_position >= 0 and next_position <= self._MAXIM:
next_char = self._map_in_a_string[next_position]
next_char = self._maze_in_a_string[next_position]
if next_char == elmt_val('symbol', 'name', 'void', 0):
self._player_position = next_position
@ -185,7 +185,7 @@ class Map:
def place_element(self, element, **kwargs):
"""
Set an element on the map
Set an element on the maze
The position used is in ._player_position attribute
Used for player and void after collecting items
@ -202,9 +202,9 @@ class Map:
if 'txt' in kwargs:
txt = kwargs['txt']
else:
txt = self._map_in_a_string
txt = self._maze_in_a_string
self._map_in_a_string = txt[:pos] + element + txt[pos + 1:]
self._maze_in_a_string = txt[:pos] + element + txt[pos + 1:]
@staticmethod
def check_line(line):