-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
187 lines (142 loc) · 6.1 KB
/
main.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
from maze import Maze
from constants import *
import pygame
from pygame.locals import *
class Main():
def __init__(self):
# Initialize pygame
pygame.init()
pygame.font.init()
# initialize font
self.font = pygame.font.SysFont(None, 40)
self.smallfont = pygame.font.SysFont(None, 25)
# Set the dimension the window
self.window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Maze")
pygame.display.flip()
self.game_over = False
self.game_exit = False
# Create and generatethe Maze
self.maze = Maze()
def text_objects(self, text, color, size):
if size == "small":
textSurface = self.smallfont.render(text, True, color)
elif size == "medium":
textSurface = self.font.render(text, True, color)
return textSurface, textSurface.get_rect()
def message_to_screen(self, msg, color, y_displace=0, size = "medium"):
textSurf, textRect = self.text_objects(msg, color, size)
textRect.center = (WINDOW_WIDTH / 2), (WINDOW_HEIGHT / 2) + y_displace
self.window.blit(textSurf, textRect)
def show_items_collected(self):
show = True
nb_items_left = len(self.maze.items) - len(self.maze.mac.backpack)
while show:
for event in pygame.event.get():
if event.type == QUIT:
self.game_over = True
elif event.type == KEYDOWN:
# Escape to quit
if event.key == K_TAB:
show = False
self.window.fill(WHITE)
self.message_to_screen("You picked up:", BLACK)
line = 320
position = 240
for char, img in self.maze.items.items():
for item in self.maze.mac.backpack:
if item == char:
self.window.blit(img, (position, line))
position += SIZE_SPRITE
if len(self.maze.mac.backpack) == len(self.maze.items):
self.message_to_screen("You can put the guardian to sleep now",GREEN, 80, size = "small")
else:
self.message_to_screen("You still have {} items to collect".format(nb_items_left), RED, 80, size = "small")
pygame.display.flip()
def screen_game_over(self):
display_screen = True
while display_screen:
winner = self.maze.winner()
mcgyver_win = pygame.image.load(MCGYVER_WIN).convert()
murdoc_win = pygame.image.load(MURDOC_WIN).convert()
if winner == "McGyver":
self.window.blit(mcgyver_win, (0, 0))
self.message_to_screen("You win", WHITE, -75)
pygame.display.update()
elif winner == "Murdoc":
self.window.blit(murdoc_win, (0, 0))
self.message_to_screen("You loose", WHITE, -75)
pygame.display.update()
self.message_to_screen("Press P to play agin or Q to quit", WHITE)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game_over = False
self.game_exit = True
if event.type == pygame.KEYDOWN:
if event.key == K_p:
self.game_over = False
self.game_exit = False
# Reset the object
self.maze.__init__()
self.maze.mac.__init__()
display_screen = False
elif event.key == K_q:
self.game_over = False
self.game_exit = True
display_screen = False
def event_in_pygame(self):
move = True
while move:
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
self.game_exit = True
self.game_over = False
move = False
elif event.type == KEYDOWN:
# Escape to quit
if event.key == K_ESCAPE:
self.game_exit = True
self.game_over = False
move = False
# Arrow keys to move McGyver
elif event.key == K_RIGHT:
self.maze.mac.move(self.maze, 'right')
elif event.key == K_LEFT:
self.maze.mac.move(self.maze, 'left')
elif event.key == K_UP:
self.maze.mac.move(self.maze, 'up')
elif event.key == K_DOWN:
self.maze.mac.move(self.maze, 'down')
# Show the items colectted
elif event.key == K_TAB:
self.show_items_collected()
elif event.key == K_ESCAPE:
self.game_over = False
self.game_exit = True
self.maze.display_maze(self.window)
pygame.display.flip()
self.game_over = self.maze.game_over()
if self.game_over:
move = False
# Main loop
def main_loop(self):
pygame.display.flip()
while not self.game_exit:
# Slow down the loop
pygame.time.Clock().tick(30)
# Display the maze
self.maze.display_maze(self.window)
# Refresh the screen
pygame.display.flip()
# While McGyver have not find the guardian, he can move around the map
if not self.game_over:
self.event_in_pygame()
# Show a screen where you can quit or play again
else:
self.screen_game_over()
self.game_exit = True
if __name__ == '__main__':
main = Main()
main.main_loop()