Uses the mapfile to generate the maze

No gui object finally
Changing the window resolution to 30px
TODO animate the player
This commit is contained in:
Fred Z 2018-03-23 10:20:39 +01:00
parent 30be7539de
commit 0c954f53ee
3 changed files with 53 additions and 10 deletions

View File

@ -40,7 +40,7 @@ guard without all the objects, he dies (according to the evolution theory).
1. Create the starting frame
* [x] Initialize a Git repo on Github
* [x] Create the labyrinth without the graphical user interface
* [ ] Start the graphical interface with PyGame
* [x] Start the graphical interface with PyGame
* [ ] Represent the guard, _McGyver_ and the objects in your program
and placed them at the beginning of the game
2. Animate the character
@ -68,4 +68,9 @@ guard without all the objects, he dies (according to the evolution theory).
* version your code and publish it on Github
* follow the good practices of PEP 8
* develop in a virtual environment using Python 3
* code must be written in English
* code must be written in English
## Dependances
* pygame 1.9.3
* python 3.6.4

51
main.py
View File

@ -12,18 +12,55 @@ details
"""
from os import system
import pygame
from pygame.locals import K_UP, K_DOWN, K_RIGHT, K_LEFT, KEYDOWN, QUIT
from map import Map
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/zebra-30.png',
'exit': 'img/g-orange-transp-30.png',
'plyr': 'img/player-30.png',
'void': 'img/blue-transp-30.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
pygame.init()
SCREEN = pygame.display.set_mode((100, 100))
MAP_FILE = '01.map'
GAME_KEYS = [K_UP, K_DOWN, K_RIGHT, K_LEFT]
WINDOW = pygame.display.set_mode(
(WINDOW_SIZE_PX, WINDOW_SIZE_PX), RESIZABLE
)
# Loading map
MAP_GAME = Map(MAP_FILE)
# Draw 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]
if key in MAZE_ELEMENTS_TILES:
back_tiles.append(
pygame.image.load(MAZE_ELEMENTS_TILES[key]).convert()
)
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
WINDOW.blit(back_tiles[cell], (x, y))
# Refresh
pygame.display.flip()
system('clear')
print(MAP_GAME.status_message)
MAP_GAME.map_print()
@ -43,4 +80,4 @@ while MAP_GAME.status:
system('clear')
print("status_message:{}".format(MAP_GAME.status_message))
MAP_GAME.map_print()
MAP_GAME.map_print()

3
map.py
View File

@ -101,6 +101,7 @@ class Map:
def map_print(self):
""" Affiche la carte avec la position de jeu courante """
print(self._map_in_a_string)
return self._map_in_a_string
def move_to(self, pressed_key):
"""
@ -183,4 +184,4 @@ class Map:
elif differance > 0:
return line + (differance * MAZE_ELEMENTS['void'])
else:
return line
return line