-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapDesign.py
More file actions
214 lines (172 loc) · 7.39 KB
/
Copy pathmapDesign.py
File metadata and controls
214 lines (172 loc) · 7.39 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
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
209
210
211
212
213
214
import pickle
import sys
import pygame
from mapCreator import create_map
class CustomMap:
"""
Description: This class is used to create a map object so it can be
saved.
Arguments:
map (list): This is a list of all map nodes
"""
def __init__(self, map, begin):
# Stores array of map nodes
self.map = map
# Stores finder beginning locations
self.begin = begin
def map_maker(game_size, resolution):
"""
Description: The screen when a user wants to create a custom map
Arguments:
game_size (int): How many rows and columns there are
resolution (int): How wide each node is
Returns:
None
"""
pygame.init()
# Create surface
screen = pygame.display.set_mode(
(game_size * resolution, game_size * resolution))
screen.fill((255, 255, 255))
# Load fonts
font = pygame.font.Font("media/ChakraPetch-Regular.ttf", 30)
# Keep track if new map needs to be created
update_map = True
# Keeps track of where finders are added
begin = []
# Used in saving the map
save_map = False
# Name of map
map_text = ""
def map_saver(map, begin, text):
"""
Description: This saves the custom map to a file for later use
Arguments:
map (list): List of all map nodes
begin (tuple): contains the x y coordinates of the beginning
text (string): Name of map
Returns:
"""
# Creates object to save
custom_map1 = CustomMap(map, begin)
# Creates file name
file_name = text + ".txt"
# Attempts to save map with pickle
try:
with open(file_name, 'wb') as fid:
pickle.dump(custom_map1, fid)
print("Saved map")
except:
print("Failed to save map")
# Main loop
map_make = True
while map_make:
# Creates a new blank map array if needed
# Just returns a blank map with snakes and end flag
if update_map:
map, __, __, __ = create_map(
"maker", game_size, resolution, "game", 0, None)
update_map = False
# If the user is not attempting to save
# Run the creation process
if not save_map:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# Press return to exit without saving
if event.key == pygame.K_RETURN:
map_make = False
break
# press s to save the map
if event.key == pygame.K_s:
save_map = True
# Places the first players finder down
if event.key == pygame.K_1:
mouse_pos = pygame.mouse.get_pos()
m_pos = [mouse_pos[0], mouse_pos[1]]
# Scaled the mouse position to correspond to nodes
m_pos[0] = round(m_pos[0] / resolution)
m_pos[1] = round(m_pos[1] / resolution)
# Update the node
map[m_pos[0]][m_pos[1]].player1_finder = True
begin.append((m_pos[0], m_pos[1]))
# Places the second players finder down
if event.key == pygame.K_2:
mouse_pos = pygame.mouse.get_pos()
m_pos = [mouse_pos[0], mouse_pos[1]]
# Scaled the mouse position to correspond to nodes
m_pos[0] = round(m_pos[0] / resolution)
m_pos[1] = round(m_pos[1] / resolution)
# Update the node
map[m_pos[0]][m_pos[1]].player2_finder = True
begin.append((m_pos[0], m_pos[1]))
# Press to place a barriers
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
m_pos = [mouse_pos[0], mouse_pos[1]]
# Scaled the mouse position to correspond to nodes
m_pos[0] = round(m_pos[0] / resolution)
m_pos[1] = round(m_pos[1] / resolution)
# Update the node
if m_pos[0] < game_size and m_pos[1] < game_size:
map[m_pos[0]][m_pos[1]].barrier = True
# User is trying to save the map
if save_map:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
# Trigger the pickling and exit the map creator screen
map_saver(map, begin, map_text)
map_make = False
break
else:
# Get the name of the map to save the map as
map_text = map_text + event.unicode
# Updates all the nodes for drawing purposes
for i in range(0, game_size):
for j in range(0, game_size):
# Checks if empty and sets colour
map[i][j].set_empty()
map[i][j].set_color()
# Draws all of the nodes
pygame.draw.rect(screen, map[i][j].color, (
i * resolution, j * resolution,
i * resolution + resolution,
j * resolution + resolution))
# Creates the rectangle along the bottom of the screen
pygame.draw.rect(screen, (48, 48, 48), [
0, game_size * resolution * 0.95,
game_size * resolution, game_size * resolution * 0.05])
# Displays the text when user is saving
# ie the name of map and prompt of what the name should be
if save_map:
if CustomMap:
text = font.render(
"Enter name of map to save:", True, (244, 163, 0))
text_rect = text.get_rect(center=(game_size * resolution / 2,
game_size * resolution * 0.05))
screen.blit(text, text_rect)
text = font.render(
map_text, True, (244, 163, 0))
text_rect = text.get_rect(center=(game_size * resolution / 2,
game_size * resolution * 0.1))
screen.blit(text, text_rect)
text = font.render(
"Press Enter to continue", True, (244, 163, 0))
text_rect = text.get_rect(
center=(game_size * resolution / 2,
game_size * resolution * 0.97))
screen.blit(text, text_rect)
# Display creation instructions
else:
text = font.render("'S' save;'Enter' exit;'1' Blue flag '2';" +
"Orange flag;'Press' Barrier",
True, (244, 163, 0))
text_rect = text.get_rect(
center=(game_size * resolution / 2,
game_size * resolution * 0.97))
screen.blit(text, text_rect)
pygame.display.update()
return