-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
234 lines (198 loc) · 5.99 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import sys
import os
import string
import pygame
#from Character import character
window_size = 600
key = 12
end_level = False
tile_size = window_size / key
window = pygame.display.set_mode((window_size, window_size))
tile_color = (127, 127, 127)
winning_color = (255, 0, 0)
background_color = (0, 0, 0)
ending_y = -1
ending_x = -1
class Character:
def __init__(self):
self.x = window_size/2
self.y = 0
def update(self, level, change_x, change_y):
new_x = self.x + change_x
new_y = self.y + change_y
# Prevent player from moving off screen horizontally
if new_x < 0:
new_x = 0
elif new_x > 600:
new_x = 600
# Prevent player from moving off screen vertically
if new_y < 0:
new_y = 0
elif new_y > 600:
new_y = 600
# return if there's a collision (true)
if self.collide(level, new_x, new_y):
new_x = self.x #+ change_x
new_y = self.y #+ change_y # this stops the player from moving
# Update the player position to the new position
self.x = new_x
self.y = new_y
# Check if the player is in the end
def player_winning(self):
grid_x = int(self.x / (600 / key))
grid_y = int(self.y / (600 / key))
if self.x >= 0 and self.x < 600 and self.y >= 0 and self.y < 600 and level.maze[grid_y][grid_x] == 2:
return True
else: return False
def collide(self, level, check_x, check_y):
maze_x = int(check_x / (600 / key))
maze_y = int(check_y / (600 / key))
# Collisions happen if the player is on-screen and trying to move into a wall
# since I want a secret passage maze_x which is the index should only return a collsion when it is
# less than key not equal to that way they can be on index 12 without issues
if maze_x < key and maze_y < key and level.maze[maze_y][maze_x] == 1:
#print("Collide!")
return True
else:
return False
def draw(self):
pygame.draw.rect(window, (0,0,127), pygame.Rect(self.x, self.y, 600/key, 600/key))
class level_template:
def __init__(self, filename):
self.maze = []
with open("maze_code.txt", "r") as maze_data:
file = ''.join(maze_data.read().split()) # Read file & remove all whitespace
# Generate maze data from file
for i in range(0, len(file)):
if i % key == 0: # New row
self.maze.append([])
# Map '_' to 0 and 'x' to 1
if file[i] == '_':
self.maze[i // key].append(0)
elif file[i] == 'x':
self.maze[i // key].append(1)
elif file[i] == '+':
self.maze[i // key].append(2)
def draw(self):
for y in range(0, key):
for x in range(0, key):
color = background_color
if self.maze[y][x] == 1:
color = tile_color
elif self.maze[y][x] == 2:
color = winning_color
pygame.draw.rect(window, color, pygame.Rect(x * tile_size, y * tile_size, tile_size, tile_size))
class Level1(level_template):
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
sys.exit()
# Move the player
if event.key == pygame.K_RIGHT:
player.update(level, 600 / key, 0)
if event.key == pygame.K_LEFT:
player.update(level, -600 / key, 0)
if event.key == pygame.K_UP:
player.update(level, 0, -600 / key)
if event.key == pygame.K_DOWN:
player.update(level, 0, 600 / key)
player = Character()
level = Level1("maze_code.txt")
class Level2(level_template):
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
sys.exit()
# Move the player
if event.key == pygame.K_LEFT:
player.update(level, 600 / key, 0)
if event.key == pygame.K_RIGHT:
player.update(level, -600 / key, 0)
if event.key == pygame.K_DOWN:
player.update(level, 0, -600 / key)
if event.key == pygame.K_UP:
player.update(level, 0, 600 / key)
class Level3(level_template):
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
sys.exit()
# Move the player
if event.key == pygame.K_UP:
player.update(level, 600 / key, 0)
if event.key == pygame.K_DOWN:
player.update(level, -600 / key, 0)
if event.key == pygame.K_LEFT:
player.update(level, 0, -600 / key)
if event.key == pygame.K_RIGHT:
player.update(level, 0, 600 / key)
class Level4(level_template):
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
sys.exit()
# Move the player
if event.key == pygame.K_UP:
player.update(level, 600 / key, 0)
if event.key == pygame.K_DOWN:
player.update(level, -600 / key, 0)
if event.key == pygame.K_LEFT:
player.update(level, 0, -600 / key)
if event.key == pygame.K_RIGHT:
player.update(level, 0, 600 / key)
def run_level_1():
while True:
level.check_events()
level.draw()
player.draw()
if player.player_winning():
break
pygame.display.flip()
def run_level_2():
player.x = window_size/2
player.y = 0
level = Level2("maze_code.txt")
while True:
level.check_events()
level.draw()
player.draw()
if player.player_winning():
break
pygame.display.flip()
def run_level_3():
player.x = window_size/2
player.y = 0
level = Level3("maze_code.txt")
while True:
level.check_events()
level.draw()
player.draw()
if player.player_winning():
break
pygame.display.flip()
def run_level_4():
player.x = window_size/2
player.y = 0
level = Level4("maze_code.txt")
while True:
level.check_events()
level.draw()
player.draw()
if player.player_winning():
print("YOU WON")
break
pygame.display.flip()
player = Character()
if __name__ == '__main__':
pygame.init()
run_level_1()
print("Congratulations you beat level 1, but now it's going to get trickier.")
run_level_2()
print("Give up.")
run_level_3()
print("mid.")
run_level_4()