Fixes pylint & flake8 warnings [deliverable]

This commit is contained in:
Fred Z 2018-04-10 18:53:00 +02:00
parent eb68f7c04d
commit f1141158d8
4 changed files with 49 additions and 29 deletions

View File

@ -6,7 +6,6 @@ Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
This file is part of [_ocp3_ project](https://github.com/freezed/ocp3) This file is part of [_ocp3_ project](https://github.com/freezed/ocp3)
""" """
from math import floor from math import floor
import pygame
ELEMENTS = ( ELEMENTS = (
{'symbol': 'n', 'name': 'needle', 'cross': True, 'item': True, {'symbol': 'n', 'name': 'needle', 'cross': True, 'item': True,
@ -24,7 +23,7 @@ ELEMENTS = (
{'symbol': 'X', 'name': 'player', 'cross': False, 'item': False, {'symbol': 'X', 'name': 'player', 'cross': False, 'item': False,
'tile': 'img/macgyver.png'}, 'tile': 'img/macgyver.png'},
{'symbol': '\n', 'name': 'nlin', 'cross': False, 'item': False, {'symbol': '\n', 'name': 'nlin', 'cross': False, 'item': False,
'tile': False}, 'tile': None},
) )
CELL_SIZE = 30 # Size of the tiles, in pixels CELL_SIZE = 30 # Size of the tiles, in pixels

29
gui.py
View File

@ -39,11 +39,12 @@ class GraphUI:
pygame.image.load(img_file).convert_alpha(), position pygame.image.load(img_file).convert_alpha(), position
) )
def draw(self, maze): def draw(self, maze, messages):
""" """
Take a maze string and generate a graphic maze Take a maze string and generate a graphic maze
:param obj maze: a Maze object :param obj maze: a Maze object
:param list/str messages: list of messages for header
""" """
for cell, element in enumerate(maze.string.replace('\n', '')): for cell, element in enumerate(maze.string.replace('\n', '')):
img = elmt_val('tile', 'symbol', element, 0) img = elmt_val('tile', 'symbol', element, 0)
@ -55,6 +56,8 @@ class GraphUI:
else: else:
self.blit(img, (x, y)) self.blit(img, (x, y))
self.set_header(messages)
# Refresh # Refresh
pygame.display.flip() pygame.display.flip()
@ -63,7 +66,7 @@ class GraphUI:
Set the header message on the window Set the header message on the window
:param obj surface: surface object :param obj surface: surface object
:param list/str messages: list of messages per place :param list/str messages: list of messages per emplacement
""" """
pygame.draw.rect(self.SURFACE, BLACK, (0, 0, WIN_SIZE_W, HEAD_SIZE_H)) pygame.draw.rect(self.SURFACE, BLACK, (0, 0, WIN_SIZE_W, HEAD_SIZE_H))
@ -84,21 +87,31 @@ class GraphUI:
:param obj player: a Player object :param obj player: a Player object
""" """
changed_tiles = [ changed_tiles = [
(elmt_val('tile', 'symbol', player.ground, 0), player.old_position), (
(elmt_val('tile', 'name', 'player', 0), player.current_position), elmt_val('tile', 'symbol', player.ground, 0),
player.old_position
),
(
elmt_val('tile', 'name', 'player', 0),
player.current_position
),
] ]
# [print('tile[0]:{} tile[1]:{}'.format(tile[0], tile[1])) for num, tile in enumerate(changed_tiles) if tile[0] is not False] [self.blit(tile[0], self.coord_from_index(tile[1]))
[self.blit(tile[0], self.coord_from_index(tile[1])) for num, tile in enumerate(changed_tiles) if False not in tile] for num, tile in enumerate(changed_tiles) if None not in tile]
# Refresh # Refresh
pygame.display.flip() pygame.display.flip()
def coord_from_index(self, idx): @staticmethod
def coord_from_index(idx):
""" """
Gets 2 dimensions coordinates Gets 2 dimensions coordinates
Converts a string index (the position in the maze.string: beware of EOL) to 2 dimensions coordinates (necessary for positionning objects in pygame) Converts a string index (the position in the maze.string: beware
of EOL) to 2 dimensions coordinates (necessary for positionning
objects in pygame)
:param int index: string index :param int index: string index
""" """

30
main.py
View File

@ -23,39 +23,37 @@ 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 # initialize maze with file
game_maze = Maze(MAZE_FILE) GAME_MAZE = Maze(MAZE_FILE)
# running graphic user interface & initialize player # running graphic user interface & initialize player
if game_maze.status: if GAME_MAZE.status:
macgyver = Player(game_maze) MACGYVER = Player(GAME_MAZE)
gui = GraphUI() GUI = GraphUI()
# TODO include set_header() in draw() GUI.draw(GAME_MAZE, MACGYVER.status_message)
gui.set_header(macgyver.status_message)
gui.draw(game_maze)
# game loop # game loop
while game_maze.status: while GAME_MAZE.status:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == QUIT: if event.type == QUIT:
game_maze.status = False GAME_MAZE.status = False
last_message = False last_message = False
if event.type == KEYDOWN: if event.type == KEYDOWN:
last_message = True # executes last_message loop last_message = True # executes last_message loop
if event.key in GAME_KEYS: if event.key in GAME_KEYS:
macgyver.key_event(event.key) MACGYVER.key_event(event.key)
gui.set_header(macgyver.status_message) GUI.set_header(MACGYVER.status_message)
gui.update(macgyver) GUI.update(MACGYVER)
else: else:
macgyver.status_message['status'] = MSG_QUIT MACGYVER.status_message['status'] = MSG_QUIT
game_maze.status = False GAME_MAZE.status = False
# displays the last_message (won, lost or quit) # displays the last_message (won, lost or quit)
while last_message: while last_message:
macgyver.status_message['title'] = MSG_END MACGYVER.status_message['title'] = MSG_END
gui.set_header(macgyver.status_message) GUI.set_header(MACGYVER.status_message)
pygame.display.flip() pygame.display.flip()
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == KEYDOWN: if event.type == KEYDOWN:

View File

@ -27,7 +27,7 @@ class Player:
self.current_position = maze.string.find( self.current_position = maze.string.find(
elmt_val('symbol', 'name', 'player', 0) elmt_val('symbol', 'name', 'player', 0)
) )
self.old_position = 0 self.old_position = None
# Element under player, default 'floor' # Element under player, default 'floor'
self.ground = elmt_val('symbol', 'name', 'floor', 0) self.ground = elmt_val('symbol', 'name', 'floor', 0)
@ -125,10 +125,20 @@ class Player:
# for all other element (wall or nline) # for all other element (wall or nline)
else: else:
self.status_message['status'] = MSG_WALL self.status_message['status'] = MSG_WALL
self.old_position = False self.old_position = None
# out the string range # out the string range
else: else:
self.status_message['status'] = MSG_WALL self.status_message['status'] = MSG_WALL
self.old_position = False self.old_position = None
# Replaces player symbol in maze.string by 'ground' value
self.maze.string = self.maze.string.replace(
elmt_val('symbol', 'name', 'player', 0), self.ground
)
# Sets the player's new position in maze.string
self.maze.set_symbol(
elmt_val('symbol', 'name', 'player', 0), self.current_position
)