-
Notifications
You must be signed in to change notification settings - Fork 0
/
labyrinth.py
77 lines (70 loc) · 2.17 KB
/
labyrinth.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
import pygame
from constantes import (
WALL_IMAGE,
FLOOR_IMAGE,
END_IMAGE,
SPRITE_SIZE,
STRUCTURE_SIZE
)
class Labyrinth:
def __init__(self):
self.structure = []
def labyrinth_construction(self):
"""
Init labyrinth
"""
with open("labyrinth.txt", "r") as file:
for line in file:
row = []
for letter in line:
if letter != "\n":
row.append(letter)
self.structure.append(row)
def display_level(self, window):
"""
Display level
"""
wall = pygame.image.load(WALL_IMAGE).convert_alpha()
floor = pygame.image.load(FLOOR_IMAGE).convert_alpha()
end = pygame.image.load(END_IMAGE).convert_alpha()
x = 0
y = 0
for line in self.structure:
for letter in line:
if letter == "W":
window.blit(wall, [x, y])
elif letter == "F":
window.blit(floor, [x, y])
else:
window.blit(end, [x, y])
x += SPRITE_SIZE
if x >= len(line) * SPRITE_SIZE:
x = 0
y += SPRITE_SIZE
def test_next_position(self, next_position):
"""
Test if player position is available
"""
if (
next_position[0] < STRUCTURE_SIZE[0]
and next_position[1] < STRUCTURE_SIZE[1]
):
if next_position[0] >= 0 and next_position[1] >= 0:
if self.structure[next_position[1]][next_position[0]] != "W":
return True
def end_game(self, player):
"""
Game finished if player is in the end position
"""
if player.position == [14, 14]:
return False
else:
return True
def response(self, player, objects):
"""
Check if player win or loose
"""
if player.position == [14, 14] and objects.objects_collected == 3:
return True
elif player.position == [14, 14] and objects.objects_collected != 3:
return False