Creates a new object : GraphUI

Manages graphic display with pygame
Move from `func.py`: set_`header()` & `maze_draw()` (renamed `draw()`)
Deletes `Maze.maze_print()` (integrated in `GraphicUI.draw()`)
Removes the background, using only tiles

TODO: Add a floor tile behind player, guard and items
This commit is contained in:
Fred Z 2018-04-05 15:37:11 +02:00
parent cea1f058e0
commit 411668ed8d
5 changed files with 101 additions and 71 deletions

50
conf.py
View File

@ -74,8 +74,8 @@ def elmt_val(kval, ksel, vsel, nline=False):
:param str kval: key of the value returned
:param str ksel: key of the selection criteria
:param str vsel: value of the selection criteria
:param bool/int nline: Default False, return value(s) in a list,
use a int() to return the `n` value from the list
:param bool/int nline: Default False, return value(s) in a generator,
use a int() to return the `n` value from a list
"""
try:
@ -87,49 +87,3 @@ def elmt_val(kval, ksel, vsel, nline=False):
if element[ksel] == vsel][nline]
except IndexError:
return False
def maze_draw(surface, maze_string):
"""
Take a maze string and generate a graphic maze
:param obj surface: a pygame surface object
:param str maze_string: maze modelized in a string
"""
back_tiles = []
for cell, element in enumerate(maze_string):
img = elmt_val('tile', 'symbol', element, 0)
if img is False:
back_tiles.append(pygame.image.load(UNKNOWN_FILE).convert())
else:
back_tiles.append(pygame.image.load(img).convert_alpha())
x = (cell % MAZE_SIZE) * CELL_SIZE
y = (cell // MAZE_SIZE) * CELL_SIZE + HEAD_SIZE_H
surface.blit(back_tiles[cell], (x, y))
# Refresh
pygame.display.flip()
def set_header(surface, messages):
"""
Set the header message on the window
:param obj surface: surface surfaceect
:param list/str messages: list of messages per place
"""
pygame.draw.rect(surface, BLACK, (0, 0, WIN_SIZE_W, HEAD_SIZE_H))
FONT = pygame.font.Font(None, FONT_SIZE)
h_title = FONT.render(messages['title'], True, BLUE, WHITE)
h_status = FONT.render(messages['status'], True, WHITE, BLACK)
h_items = FONT.render(messages['items'], True, GREEN, BLACK)
h_items_pos = h_items.get_rect(topright=(WIN_SIZE_W, 0))
surface.blit(h_title, (0, 0))
surface.blit(h_status, (0, CELL_SIZE))
surface.blit(h_items, h_items_pos)

76
gui.py Normal file
View File

@ -0,0 +1,76 @@
"""
Author: freezed <freezed@users.noreply.github.com> 2018-04-04
Version: 0.1
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
This file is part of [_ocp3_ project](https://github.com/freezed/ocp3)
"""
import pygame
from conf import (
BLACK, BLUE, CAPTION, CELL_SIZE, elmt_val, FONT_SIZE, GREEN,
HEAD_SIZE_H, MAZE_SIZE, UNKNOWN_FILE, WIN_DIM, WIN_SIZE_W, WHITE
)
class GraphUI:
"""
GraphUI
"""
def __init__(self):
""" Constructor """
pygame.init()
pygame.time.Clock().tick(25)
pygame.display.set_caption(CAPTION)
self.SURFACE = pygame.display.set_mode(WIN_DIM)
self.FONT = pygame.font.Font(None, FONT_SIZE)
def blit(self, img_file, position):
"""
Blit an image on display
:param str img_file:
:param tuple position: coordinates in pixels on x/y
"""
self.SURFACE.blit(
pygame.image.load(img_file).convert_alpha(), position
)
def draw(self, maze):
"""
Take a maze string and generate a graphic maze
:param obj maze: a Maze object
"""
for cell, element in enumerate(maze.string.replace('\n', '')):
img = elmt_val('tile', 'symbol', element, 0)
x = (cell % MAZE_SIZE) * CELL_SIZE
y = (cell // MAZE_SIZE) * CELL_SIZE + HEAD_SIZE_H
if img is False:
self.blit(UNKNOWN_FILE, (x, y))
else:
self.blit(img, (x, y))
# Refresh
pygame.display.flip()
def set_header(self, messages):
"""
Set the header message on the window
:param obj surface: surface surfaceect
:param list/str messages: list of messages per place
"""
pygame.draw.rect(self.SURFACE, BLACK, (0, 0, WIN_SIZE_W, HEAD_SIZE_H))
h_title = self.FONT.render(messages['title'], True, BLUE, WHITE)
h_status = self.FONT.render(messages['status'], True, WHITE, BLACK)
h_items = self.FONT.render(messages['items'], True, GREEN, BLACK)
h_items_pos = h_items.get_rect(topright=(WIN_SIZE_W, 0))
self.SURFACE.blit(h_title, (0, 0))
self.SURFACE.blit(h_status, (0, CELL_SIZE))
self.SURFACE.blit(h_items, h_items_pos)

23
main.py
View File

@ -16,29 +16,24 @@ from pygame.locals import (
)
from maze import Maze
from player import Player
from conf import (
BACKGRND_FILE, CAPTION, MAZE_FILE, HEAD_SIZE_H, maze_draw,
MSG_END, MSG_QUIT, set_header, WIN_DIM
)
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
# initialize maze and player
# initialize maze with file
game_maze = Maze(MAZE_FILE)
macgyver = Player(game_maze)
# Running graphic user interface & initialize player
if game_maze.status:
pygame.init()
pygame.time.Clock().tick(25)
pygame.display.set_caption(CAPTION)
WINDOW = pygame.display.set_mode(WIN_DIM)
WINDOW.blit(pygame.image.load(BACKGRND_FILE).convert(), (0, HEAD_SIZE_H))
macgyver = Player(game_maze)
gui = GraphUI()
# Game loop
while game_maze.status:
set_header(WINDOW, macgyver.status_message)
maze_draw(WINDOW, game_maze.maze_print())
gui.set_header(macgyver.status_message)
gui.draw(game_maze)
for event in pygame.event.get():
if event.type == QUIT:
game_maze.status = False
@ -56,7 +51,7 @@ while game_maze.status:
# Allows reading the last_message (won, lost or quit)
while last_message:
macgyver.status_message['title'] = MSG_END
set_header(WINDOW, macgyver.status_message)
gui.set_header(macgyver.status_message)
pygame.display.flip()
for event in pygame.event.get():
if event.type == KEYDOWN:

View File

@ -95,10 +95,6 @@ class Maze:
else:
return line
def maze_print(self):
""" Return a string of the maze state """
return self.string.replace('\n', '')
def set_symbol(self, symbol, pos):
"""
Set an symbol on the maze

View File

@ -5,7 +5,10 @@ Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
This file is part of [_ocp3_ project](https://github.com/freezed/ocp3)
"""
from conf import elmt_val, MSG_COLLECT, MSG_LOSER, MSG_OK, MSG_WALL, MSG_WINNER, HEAD_MESSAGES
from conf import (
elmt_val, MSG_COLLECT, MSG_LOSER, MSG_OK, MSG_WALL, MSG_WINNER,
HEAD_MESSAGES
)
from pygame.locals import K_UP, K_DOWN, K_RIGHT, K_LEFT
@ -17,7 +20,9 @@ class Player:
def __init__(self, maze):
""" Constructor """
self.maze = maze
self.position = maze.string.find(elmt_val('symbol', 'name', 'player', 0))
self.position = maze.string.find(
elmt_val('symbol', 'name', 'player', 0)
)
# Element under player, default 'void'
self.ground = elmt_val('symbol', 'name', 'void', 0)
@ -58,7 +63,8 @@ class Player:
"""
Next position treatment
For each movement, it checks the next symbol on the maze and apply the corresponding rule:
For each movement, it checks the next symbol on the maze and
apply the corresponding rule:
- set the new position
- updates messages
- collect item (if any)
@ -97,7 +103,8 @@ class Player:
self.maze.status = False
# all 'item' are collected : player wins
if sorted(self.stock) == sorted(elmt_val('name', 'item', True)):
if sorted(self.stock) == sorted(
elmt_val('name', 'item', True)):
self.status_message['status'] = MSG_WINNER
# player lose
@ -117,4 +124,6 @@ class Player:
self.status_message['status'] = MSG_WALL
# Sets the player's new position
self.maze.set_symbol(elmt_val('symbol', 'name', 'player', 0), self.position)
self.maze.set_symbol(
elmt_val('symbol', 'name', 'player', 0), self.position
)