-
Notifications
You must be signed in to change notification settings - Fork 0
/
escape-room.py
208 lines (155 loc) · 5.57 KB
/
escape-room.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Escape room simplifié sans interface graphique
# Pour pouvoir utiliser area dans nos futures fonctions,
# on devra utilisé le mot clé "global"
area = None
obstacleArea = None
playerPosLine = 0
playerPosColumn = 0
maxLine = None
maxColumn = None
obstacle1Line = None
obstacle1Column = None
obstacle2Line = None
obstacle2Column = None
def checkmindimensions(numberLine, numberColumn):
if numberLine < 4 or numberColumn < 4:
return False
else:
return True
def readdimensions():
numberline = int(input('Entrez le nombre de lignes (min 4)'))
numbercolumn = int(input('Entrez le nombre de colonnes (min 4)'))
while not checkmindimensions(numberline, numbercolumn):
print('Les dimensions minimum ne sont pas respectées (min 4x4)')
numberline, numbercolumn = readdimensions()
return numberline, numbercolumn
def checkobstaclecoord(obstacleline, obstaclecolumn):
global maxLine
global maxColumn
if (
obstacleline < 0
or obstacleline >= maxLine
or obstaclecolumn < 0
or obstaclecolumn >= maxColumn
or (
obstacleline == 0
and obstaclecolumn == 0
)
):
return False
else:
return True
def readobstaclecoord(i):
obstacleline = int(input(f'Entrez la ligne de l\'obstacle: {i}'))
obstaclecolumn = int(input(f'Entrez la colonne de l\'obstacle: {i}'))
while not checkobstaclecoord(obstacleline, obstaclecolumn):
print('La position entrée n\'est pas valide')
obstacleline, obstaclecolumn = readobstaclecoord(i)
return obstacleline, obstaclecolumn
def savemaxdimensions(line, column):
global maxLine
global maxColumn
maxLine = line
maxColumn = column
def initareas(numberLine, numberColumn, obstacle1Line, obstacle1Column, obstacle2Line, obstacle2Column):
global area
global obstacleArea
area = [["*" for x in range(numberColumn)] for y in range(numberLine)]
obstacleArea = [["*" for x in range(numberColumn)] for y in range(numberLine)]
obstacleArea[obstacle1Line][obstacle1Column] = "O"
obstacleArea[obstacle2Line][obstacle2Column] = "O"
def initplayerpos():
global area
area[0][0] = 'P'
def displayArea():
global area
global obstacleArea
displayedarea = area.copy()
for i in range(len(displayedarea)):
for j in range(len(displayedarea[i])):
if obstacleArea[i][j] == 'O' and area[i][j] == 'P':
displayedarea[i][j] = 'X'
elif obstacleArea[i][j] == 'O':
displayedarea[i][j] = 'O'
for i in range(len(displayedarea)):
print(area[i])
def readdirection():
direction = int(input('Entrez votre direction, 2: en bas, 4: à gauche, 8: en haut, 6: à droite'))
return direction
def checkdirection(direction):
if direction != 8 and direction != 6 and direction != 2 and direction != 4:
return False
else:
return True
def playeroutofarea(playerPosLine, playerPosColumn):
global maxLine
global maxColumn
if playerPosLine >= maxLine or playerPosLine < 0 or playerPosColumn >= maxColumn or playerPosColumn < 0:
return True
else:
return False
def playerinobstacle(playerPosLine, playerPosColumn):
global obstacleArea
if obstacleArea[playerPosLine][playerPosColumn] == '*':
return False
else:
return True
def checkifgameiswon():
global playerPosLine
global playerPosColumn
return playeroutofarea(playerPosLine, playerPosColumn)
def checkfifgameislost():
global playerPosLine
global playerPosColumn
return playerinobstacle(playerPosLine, playerPosColumn)
def moveplayer(direction):
global playerPosLine
global playerPosColumn
# Supprimer la position précédente
area[playerPosLine][playerPosColumn] = '*'
match direction:
case 2:
playerPosLine += 1
case 4:
playerPosColumn -= 1
case 8:
playerPosLine -= 1
case 6:
playerPosColumn += 1
# Apliquer la nouvelle position si on est pas en dehors du tableau
if not playeroutofarea(playerPosLine, playerPosColumn):
area[playerPosLine][playerPosColumn] = 'P'
def play():
direction = readdirection()
while not checkdirection(direction):
print('Entrez une direction valide, 2: en bas, 4: à gauche, 8: en haut, 6: à droite')
direction = readdirection()
moveplayer(direction)
displayArea()
def replay():
replay = input('Souhaitez-vous rejouer (oui ou non) ?')
while replay != 'oui' and replay != 'non':
replay = input('Je n\'ai pas compris, souhaitez-vous rejouer (oui ou non) ?')
if replay == 'oui':
return True
else:
return False
def launchgame():
numberLine, numberColumn = readdimensions()
savemaxdimensions(numberLine, numberColumn)
obstacle1Line, obstacle1Column = readobstaclecoord(1)
obstacle2Line, obstacle2Column = readobstaclecoord(2)
initareas(numberLine, numberColumn, obstacle1Line, obstacle1Column, obstacle2Line, obstacle2Column)
initplayerpos()
print('C\'est parti')
displayArea()
while not (checkifgameiswon() or checkfifgameislost()):
play()
if (checkifgameiswon()):
print('Vous avez gagné !')
else:
print('Vous avez perdu !')
if replay():
print('__________')
launchgame()
launchgame()