-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
140 lines (117 loc) · 5.06 KB
/
Copy pathclasses.py
File metadata and controls
140 lines (117 loc) · 5.06 KB
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
"""MacGyver maze game classes"""
import random
import pygame
import constants
class Map:
"""Map generation"""
def __init__(self, maze_file):
self.maze_file = maze_file
self.structure = []
def create(self):
"""Create the map with the .txt file"""
with open(self.maze_file, "r") as maze_file:
for line in maze_file:
line_maze = []
for sprite in line:
if sprite != "\n":
line_maze.append(sprite)
self.structure.append(line_maze)
def display_map(self, screen):
"""Display map with the list return by create()"""
path = pygame.image.load(constants.PATH_IMG).convert()
guardian = pygame.image.load(constants.GUARDIAN_IMG).convert()
wall = pygame.image.load(constants.WALL_IMG).convert()
num_line = 0
for line in self.structure:
num_sprite = 0
for sprite in line:
x = num_sprite * constants.SPRITE_SIZE
y = num_line * constants.SPRITE_SIZE + constants.SPACE_LINE
if sprite == "w":
screen.blit(wall, (x, y))
elif sprite == "e":
screen.blit(guardian, (x, y))
elif sprite == "o" or "s":
screen.blit(path, (x, y))
num_sprite += 1
num_line += 1
class MacGyver:
"""Create MacGyver, move it and pickup the objects"""
def __init__(self, image, maze):
self.image = pygame.image.load(image).convert_alpha()
self.sprite_x = 0
self.sprite_y = 0
self.x = self.sprite_x * constants.SPRITE_SIZE
self.y = self.sprite_y * constants.SPRITE_SIZE + constants.SPACE_LINE
self.maze = maze
self.mg_counter = 0
def move(self, direction):
"""Move MacGyver with the sprite numbers"""
# right
if direction == "right":
if self.sprite_x < (constants.MAX_SPRITES_SIZE - 1):
if self.maze.structure[self.sprite_y][self.sprite_x+1] != "w":
self.sprite_x += 1
self.x = self.sprite_x * constants.SPRITE_SIZE
# left
if direction == "left":
if self.sprite_x > 0:
if self.maze.structure[self.sprite_y][self.sprite_x-1] != "w":
self.sprite_x -= 1
self.x = self.sprite_x * constants.SPRITE_SIZE
# up
if direction == "up":
if self.sprite_y > 0:
if self.maze.structure[self.sprite_y-1][self.sprite_x] != "w":
self.sprite_y -= 1
self.y = self.sprite_y *\
constants.SPRITE_SIZE + constants.SPACE_LINE
# down
if direction == "down":
if self.sprite_y < (constants.MAX_SPRITES_SIZE - 1):
if self.maze.structure[self.sprite_y+1][self.sprite_x] != "w":
self.sprite_y += 1
self.y = self.sprite_y *\
constants.SPRITE_SIZE + constants.SPACE_LINE
def counter(self):
"""Count the objects picked up by MacGyver
when it is on the right position"""
if self.maze.structure[self.sprite_y][self.sprite_x] == "b":
self.maze.structure[self.sprite_y][self.sprite_x] = "o"
self.mg_counter = self.mg_counter + 1
def display_counter(self, SCREEN):
"""Display the counter with a text way and not an image"""
counter = pygame.font.SysFont("monospace", 15, 0)
text_counter = counter.render(
"Objects : " + str(self.mg_counter), 0, (0, 128, 0))
SCREEN.blit(text_counter, (5, 5))
class Object:
"""Create objects on random location"""
def __init__(self, image, maze):
self.image = pygame.image.load(image).convert_alpha()
self.maze = maze
self.sprite_x, self.sprite_y = self.generate()
self.x = self.sprite_x * constants.SPRITE_SIZE
self.y = self.sprite_y * constants.SPRITE_SIZE + constants.SPACE_LINE
self.life = 1 # to erase objects image and location
def generate(self):
"""Generate objects with the .txt file"""
test = 0
while test < 1:
self.sprite_y = random.randrange(constants.MAX_SPRITES_SIZE)
self.sprite_x = random.randrange(constants.MAX_SPRITES_SIZE)
# make sure that the objets are where MacGyver can go
if self.maze.structure[self.sprite_y][self.sprite_x] == "o":
# make sure that two objects can't be on the same sprite
self.maze.structure[self.sprite_y][self.sprite_x] = "b"
test += 1
return self.sprite_x, self.sprite_y
def display(self, SCREEN):
"""Display the objects on the map"""
if self.life > 0: # erase object's images
SCREEN.blit(self.image, (self.x, self.y))
# when they have been picked up,
# the objects location is where MacGyver can't go
if self.life < 0:
self.x = 0
self.y = 0