Reviews comments

This commit is contained in:
Fred Z 2018-04-10 08:58:02 +02:00
parent 5a7224aaad
commit 69be4c1753
5 changed files with 46 additions and 36 deletions

View File

@ -15,7 +15,7 @@ ELEMENTS = (
'tile': 'img/rod.png'},
{'symbol': 'e', 'name': 'ether', 'cross': True, 'item': True,
'tile': 'img/ether.png'},
{'symbol': 'E', 'name': 'exit', 'cross': False, 'item': False,
{'symbol': 'E', 'name': 'guard', 'cross': False, 'item': False,
'tile': 'img/guardian.png'},
{'symbol': ' ', 'name': 'floor', 'cross': True, 'item': False,
'tile': 'img/floor1.png'},
@ -49,7 +49,7 @@ MSG_LOSER = "You lose! You were missing: {}."
MSG_OK = ""
MSG_QUIT = "You decide to quit the game"
MSG_WALL = "That's a wall!"
MSG_WINNER = "Cogratulations! You asleep the guard."
MSG_WINNER = "Congratulations! You asleep the guard."
# Files
MAZE_FILE = '01.maze'

14
gui.py
View File

@ -12,14 +12,16 @@ from conf import (
)
class GraphUI:
"""
GraphUI
"""
""" Manages graphic display with pygame """
def __init__(self):
""" Constructor """
"""
Starts pygames magic:
- `surface` object
- `font` object
- the display clock
"""
pygame.init()
pygame.time.Clock().tick(25)
pygame.display.set_caption(CAPTION)
@ -60,7 +62,7 @@ class GraphUI:
"""
Set the header message on the window
:param obj surface: surface surfaceect
:param obj surface: surface object
:param list/str messages: list of messages per place
"""
pygame.draw.rect(self.SURFACE, BLACK, (0, 0, WIN_SIZE_W, HEAD_SIZE_H))

12
main.py
View File

@ -20,17 +20,17 @@ from gui import GraphUI
from conf import MAZE_FILE, MSG_END, MSG_QUIT
GAME_KEYS = [K_UP, K_DOWN, K_RIGHT, K_LEFT]
last_message = False # Do not execute last message loop
last_message = False # do not execute last message loop
# initialize maze with file
game_maze = Maze(MAZE_FILE)
# Running graphic user interface & initialize player
# running graphic user interface & initialize player
if game_maze.status:
macgyver = Player(game_maze)
gui = GraphUI()
# Game loop
# game loop
while game_maze.status:
gui.set_header(macgyver.status_message)
gui.draw(game_maze)
@ -40,15 +40,15 @@ while game_maze.status:
last_message = False
if event.type == KEYDOWN:
last_message = True # Execute last_message loop
last_message = True # executes last_message loop
if event.key in GAME_KEYS:
macgyver.move_to(event.key)
macgyver.key_event(event.key)
else:
macgyver.status_message['status'] = MSG_QUIT
game_maze.status = False
# Allows reading the last_message (won, lost or quit)
# displays the last_message (won, lost or quit)
while last_message:
macgyver.status_message['title'] = MSG_END
gui.set_header(macgyver.status_message)

View File

@ -97,7 +97,7 @@ class Maze:
def set_symbol(self, symbol, pos):
"""
Set an symbol on the maze
Set a symbol on the maze
Used for 'player' and 'floor' after collecting items

View File

@ -18,7 +18,11 @@ class Player:
"""
def __init__(self, maze):
""" Constructor """
"""
Creates a player in the given maze
:param obj maze: Maze object
"""
self.maze = maze
self.position = maze.string.find(
elmt_val('symbol', 'name', 'player', 0)
@ -26,9 +30,11 @@ class Player:
# Element under player, default 'floor'
self.ground = elmt_val('symbol', 'name', 'floor', 0)
# Colleted items
self.stock = []
self.stock_num = 0
# Contextual messages to display on each turn
self.status_message = {}
self.status_message['title'] = HEAD_MESSAGES['title']
self.status_message['status'] = HEAD_MESSAGES['status']
@ -36,30 +42,27 @@ class Player:
self.stock_num, maze.MAX_ITEMS
)
def move_to(self, pressed_key):
def key_event(self, pressed_key):
"""
Move the player on the maze
Sets value of the new position and passes it to `move_to()`
:param str pressed_key: direction (pygame const)
:param int pressed_key: direction (pygame const)
"""
# Replace player symbol on the maze by 'ground'
self.maze.string = self.maze.string.replace(
elmt_val('symbol', 'name', 'player', 0), self.ground
)
if pressed_key == K_UP:
self.next_pos(self.position - self.maze.COL_NB)
self.move_to(self.position - self.maze.COL_NB)
elif pressed_key == K_DOWN:
self.next_pos(self.position + self.maze.COL_NB)
self.move_to(self.position + self.maze.COL_NB)
elif pressed_key == K_RIGHT:
self.next_pos(self.position + 1)
self.move_to(self.position + 1)
elif pressed_key == K_LEFT:
self.next_pos(self.position - 1)
self.move_to(self.position - 1)
def next_pos(self, next_position):
# ++Add other treatment for key events here (help, menu, etc.)++
def move_to(self, next_position):
"""
Next position treatment
@ -73,19 +76,18 @@ class Player:
:param int next_position: index in self.maze.string
"""
# is in the string range
# in the string range
if next_position in self.maze.RANGE:
next_symbol = self.maze.string[next_position]
# is a 'floor' element
# 'floor' element
if next_symbol == elmt_val('symbol', 'name', 'floor', 0):
self.position = next_position
self.status_message['status'] = MSG_OK
# is a 'item' element
# 'item' element
elif next_symbol in elmt_val('symbol', 'item', True):
self.position = next_position
self.ground = elmt_val('symbol', 'name', 'floor', 0)
self.stock.append(
elmt_val('name', 'symbol', next_symbol, 0)
)
@ -98,8 +100,8 @@ class Player:
self.stock_num, self.maze.MAX_ITEMS
)
# is an 'exit' element (aka the guard)
elif next_symbol == elmt_val('symbol', 'name', 'exit', 0):
# 'guard' element
elif next_symbol == elmt_val('symbol', 'name', 'guard', 0):
self.maze.status = False
# all 'item' are collected : player wins
@ -116,13 +118,19 @@ class Player:
missed_item_flist
)
# is all other element (wall or nline)
# for all other element (wall or nline)
else:
self.status_message['status'] = MSG_WALL
# out the string range
else:
self.status_message['status'] = MSG_WALL
# Replaces player symbol on the maze by 'ground' value
self.maze.string = self.maze.string.replace(
elmt_val('symbol', 'name', 'player', 0), self.ground
)
# Sets the player's new position
self.maze.set_symbol(
elmt_val('symbol', 'name', 'player', 0), self.position