-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.py
144 lines (116 loc) · 4.68 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
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)
"""
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
class Player:
"""
Managing the player movement
"""
def __init__(self, maze):
"""
Creates a player in the given maze
:param obj maze: Maze object
"""
self.maze = maze
self.current_position = maze.string.find(
elmt_val('symbol', 'name', 'player', 0)
)
self.old_position = None
# 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']
self.status_message['items'] = HEAD_MESSAGES['items'].format(
self.stock_num, maze.MAX_ITEMS
)
def key_event(self, pressed_key):
"""
Sets value of the new position and passes it to `move_to()`
:param int pressed_key: direction (pygame const)
"""
if pressed_key == K_UP:
self.move_to(self.current_position - self.maze.COL_NB)
elif pressed_key == K_DOWN:
self.move_to(self.current_position + self.maze.COL_NB)
elif pressed_key == K_RIGHT:
self.move_to(self.current_position + 1)
elif pressed_key == K_LEFT:
self.move_to(self.current_position - 1)
# ++Add other treatment for key events here (help, menu, etc.)++
def move_to(self, next_position):
"""
Next position treatment
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)
- set symbol of the leaved position
- stop the game (win or lose)
:param int next_position: index in self.maze.string
"""
# in the string range
if next_position in self.maze.RANGE:
next_symbol = self.maze.string[next_position]
self.old_position = self.current_position
# 'floor' element
if next_symbol == elmt_val('symbol', 'name', 'floor', 0):
self.current_position = next_position
self.status_message['status'] = MSG_OK
# 'item' element
elif next_symbol in elmt_val('symbol', 'item', True):
self.current_position = next_position
self.stock.append(
elmt_val('name', 'symbol', next_symbol, 0)
)
self.stock_num += 1
self.status_message['status'] = MSG_COLLECT.format(
elmt_val('name', 'symbol', next_symbol, 0)
)
self.status_message['items'] \
= HEAD_MESSAGES['items'].format(
self.stock_num, self.maze.MAX_ITEMS
)
# 'guard' element
elif next_symbol == elmt_val('symbol', 'name', 'guard', 0):
self.maze.status = False
# all 'item' are collected : player wins
if sorted(self.stock) == sorted(
elmt_val('name', 'item', True)):
self.status_message['status'] = MSG_WINNER
# player lose
else:
missed_item_flist = ', '.join((item for item in elmt_val(
'name', 'item', True
) if item not in self.stock))
self.status_message['status'] = MSG_LOSER.format(
missed_item_flist
)
# for all other element (wall or nline)
else:
self.status_message['status'] = MSG_WALL
self.old_position = None
# out the string range
else:
self.status_message['status'] = MSG_WALL
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
)