Adds first files and catchs key events
Display the maze map in console, use `pygame.event` to intercept keyboard arrows (and escape for quitting). `pygame.event` do not work without a window displayed, so a small 100x100 window is used. The console displays a text for each arrow key pressed.
This commit is contained in:
parent
b93c30e767
commit
dc5c100663
15
01.map
Normal file
15
01.map
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
. . . .
|
||||||
|
... . . . .
|
||||||
|
. . . . . . .
|
||||||
|
. . . . . . .
|
||||||
|
. . . . . .
|
||||||
|
... . . . .E
|
||||||
|
. ..... . ....
|
||||||
|
. . . .
|
||||||
|
... . . . . .
|
||||||
|
. . . .
|
||||||
|
. ... . .
|
||||||
|
. . . . . . .
|
||||||
|
. . . . . .
|
||||||
|
. . . . . .
|
||||||
|
. . . .
|
51
main.py
Normal file
51
main.py
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
Author: freezed <freezed@users.noreply.github.com> 2018-03-17
|
||||||
|
Version: 0.1
|
||||||
|
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
|
||||||
|
"""
|
||||||
|
import pygame
|
||||||
|
from map import Map
|
||||||
|
from pygame.locals import KEYDOWN, K_ESCAPE, QUIT, K_DOWN, K_LEFT, K_UP, K_RIGHT
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode((100, 100))
|
||||||
|
# Variables
|
||||||
|
MAP_FILE = '01.map'
|
||||||
|
|
||||||
|
# Class
|
||||||
|
MAP_GAME = Map(MAP_FILE)
|
||||||
|
print(MAP_GAME.status)
|
||||||
|
print(MAP_GAME.status_message)
|
||||||
|
|
||||||
|
# Loading map
|
||||||
|
MAP_GAME.map_print()
|
||||||
|
|
||||||
|
# Event loop
|
||||||
|
game_on = True
|
||||||
|
while game_on:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == QUIT:
|
||||||
|
game_on = False
|
||||||
|
|
||||||
|
if event.type == KEYDOWN:
|
||||||
|
if event.key == K_ESCAPE:
|
||||||
|
game_on = False
|
||||||
|
|
||||||
|
if event.type == KEYDOWN:
|
||||||
|
if event.key == K_DOWN:
|
||||||
|
print('down')
|
||||||
|
|
||||||
|
if event.type == KEYDOWN:
|
||||||
|
if event.key == K_LEFT:
|
||||||
|
print('left')
|
||||||
|
|
||||||
|
if event.type == KEYDOWN:
|
||||||
|
if event.key == K_UP:
|
||||||
|
print('up')
|
||||||
|
|
||||||
|
if event.type == KEYDOWN:
|
||||||
|
if event.key == K_RIGHT:
|
||||||
|
print('right')
|
95
map.py
Normal file
95
map.py
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
"""
|
||||||
|
Author: freezed <freezed@users.noreply.github.com> 2018-02-06
|
||||||
|
Version: 0.1
|
||||||
|
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
|
||||||
|
|
||||||
|
Ce fichier fait partie du projet `ocp3`
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
|
||||||
|
# CONFIGURATION
|
||||||
|
|
||||||
|
# Elements dispo dans le labyrinthe
|
||||||
|
MAZE_ELEMENTS = {'wall': '.',
|
||||||
|
'exit': 'U',
|
||||||
|
'robo': 'X',
|
||||||
|
'void': ' '}
|
||||||
|
# Messages d'erreurs
|
||||||
|
ERR_ = "#!@?# Oups… "
|
||||||
|
ERR_MAP_FILE = ERR_ + "carte «{}» inaccessible!"
|
||||||
|
|
||||||
|
MIN_MAP_SIDE = 3
|
||||||
|
|
||||||
|
MSG_DISCLAMER = "Bienvenue dans Roboc."
|
||||||
|
MSG_START_GAME = "Votre partie commence"
|
||||||
|
MSG_END_GAME = "Fin du jeu."
|
||||||
|
|
||||||
|
class Map:
|
||||||
|
"""
|
||||||
|
Fourni les moyens necessaire a l'utilisation d'un fichier carte.
|
||||||
|
|
||||||
|
Controle de coherance sur la carte choise, deplace le robo en
|
||||||
|
fonction des commandes du joueur jusqu'en fin de partie.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, map_file):
|
||||||
|
"""
|
||||||
|
Initialisation de la carte utilise
|
||||||
|
|
||||||
|
Instancie un objet Map avec les attributs suivant:
|
||||||
|
|
||||||
|
:var int status: Etat de l'objet apres le deplacement
|
||||||
|
:var str status_message: Message relatif au deplacement
|
||||||
|
:var int _column_nb: Nbre de colonne du labyrinte (1ere ligne)
|
||||||
|
:var str _data_text: Contenu du labyrinte
|
||||||
|
:var str _element_under_robo: Element sous le robot
|
||||||
|
:var int _line_nb: Nbre de ligne du labyrinte
|
||||||
|
:var int _robo_position: position du robo dans _data_text
|
||||||
|
|
||||||
|
:param map_file: fichier «carte» avec chemin relatif
|
||||||
|
:rtype map: str()
|
||||||
|
:return: None
|
||||||
|
"""
|
||||||
|
# Chargement du fichier carte choisi
|
||||||
|
if os.path.isfile(map_file) is True:
|
||||||
|
with open(map_file, "r") as map_data:
|
||||||
|
|
||||||
|
# contenu de la carte en texte
|
||||||
|
self._data_text = map_data.read()
|
||||||
|
|
||||||
|
# contenu de la carte ligne a ligne
|
||||||
|
map_data_list = self._data_text.splitlines()
|
||||||
|
|
||||||
|
# nbre de colonne de la 1ere ligne de la carte
|
||||||
|
try:
|
||||||
|
self._column_nb = len(map_data_list[0]) + 1
|
||||||
|
except IndexError:
|
||||||
|
self._column_nb = 0
|
||||||
|
|
||||||
|
# nbre de ligne de la carte
|
||||||
|
try:
|
||||||
|
self._line_nb = len(map_data_list)
|
||||||
|
except IndexError:
|
||||||
|
self._line_nb = 0
|
||||||
|
|
||||||
|
# position du robot
|
||||||
|
self._robo_position = self._data_text.find(
|
||||||
|
MAZE_ELEMENTS['robo']
|
||||||
|
)
|
||||||
|
|
||||||
|
# element «sous» le robo, au depart
|
||||||
|
self._element_under_robo = MAZE_ELEMENTS['void']
|
||||||
|
|
||||||
|
# Add map checking here
|
||||||
|
|
||||||
|
self.status = True
|
||||||
|
self.status_message = MSG_START_GAME
|
||||||
|
|
||||||
|
# Erreur de chargement du fichier
|
||||||
|
else:
|
||||||
|
self.status = False
|
||||||
|
self.status_message = ERR_MAP_FILE.format(map_file)
|
||||||
|
|
||||||
|
def map_print(self):
|
||||||
|
""" Affiche la carte avec la position de jeu courante """
|
||||||
|
print(self._data_text)
|
Loading…
Reference in New Issue
Block a user