-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_crawl.py
293 lines (230 loc) · 7.46 KB
/
map_crawl.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import time as t
import pygame as pg
from lib.box import Box, draw_box
from lib import color as Color, collision
class Bool2:
def __init__(self, x=False, y=False):
self.x = x
self.y = y
class Warp:
def __init__(self, id, collider: Box, color):
self.id = id
self.spawn = pg.Vector2()
self.collider = collider
self.color = color
class Entity:
def __init__(self, collider, max_speed, color):
self.collider = collider
self.speed = pg.Vector2()
self.max_speed = max_speed
self.color = color
self.collision = Bool2()
def update(self):
self.collider.x += self.speed.x
self.collider.y += self.speed.y
class Map:
def __init__(self, id):
self.id = id
self.start = pg.Vector2()
self.event_list = []
self.dynamic_list = []
self.static_list = []
self.render_list = []
def add_wall(self, wall):
self.static_list.append(wall)
self.render_list.append(wall)
def add_entity(self, entity):
self.dynamic_list.append(entity)
self.render_list.append(entity)
def add_event(self, event):
self.event_list.append(event)
self.render_list.append(event)
def check_collition(A: Box, B: Box):
# A Edges
A_IZQ = A.x
A_DER = A.x + A.w
A_ARR = A.y
A_ABJ = A.y + A.h
# B Edges
B_IZQ = B.x
B_DER = B.x + B.w
B_ARR = B.y
B_ABJ = B.y + B.h
# Restrictions
return (
(A_ABJ >= B_ARR) and (A_ARR <= B_ABJ) and
(A_DER >= B_IZQ) and (A_IZQ <= B_DER)
)
def create_box(x, y, wall_w, wall_h):
return [
Entity(Box(x,y,wall_w,wall_h), 0, Color.green),
Entity(Box(x,y,wall_h,wall_w), 0, Color.green),
Entity(Box(x,y+wall_w-wall_h,wall_w,wall_h), 0, Color.green),
Entity(Box(x+wall_w-wall_h,y,wall_h,wall_w), 0, Color.green)
]
fps = 60
step = 10
game_exit = False
transition = True
screen = Box(0,0,640,480)
p1 = Entity(Box(0,0,40,40), 5, Color.red)
event_list = []
dynamic_list = []
static_list = []
render_list = []
# MAP 1
map_1 = Map(1)
map_1.start = pg.Vector2(640/2-20,480/2+50)
map_1.add_entity(p1)
for wall in create_box(640/2-150,480/2-150,300,10):
map_1.add_wall(wall)
event1 = Warp(1,Box(640/2-10,480/2-150+20,20,20), Color.blue)
event1.spawn.x = 640/2 - p1.collider.w/2
event1.spawn.y = event1.collider.y + event1.collider.h + 10
map_1.add_event(event1)
# Map2
map_2 = Map(2)
map_2.start = pg.Vector2(640/2,480/2)
map_2.add_entity(p1)
for wall in create_box(640/2-200,480/2-200,400,10):
map_2.add_wall(wall)
event2 = Warp(2,Box(640/2-10,480/2-200+20,20,20), Color.blue)
event2.spawn.x = 640/2 - p1.collider.w/2
event2.spawn.y = event2.collider.y + event2.collider.h + 10
map_2.add_event(event2)
event4 = Warp(4,Box(640/2-10,480/2+200-40,20,20), Color.blue)
event4.spawn.x = 640/2 - p1.collider.w/2
event4.spawn.y = event4.collider.y - event4.collider.h - p1.collider.h
map_2.add_event(event4)
# Map3
map_3 = Map(3)
map_3.start = pg.Vector2(640/2,480/2)
map_3.add_entity(p1)
for wall in create_box(640/2-150,480/2-150,300,10):
map_3.add_wall(wall)
map_3.add_wall(Entity(Box(640/2-90,480/2-90,40,40),0, Color.green))
event3 = Warp(3,Box(640/2-10,480/2+ 150 - 40,20,20), Color.blue)
event3.spawn.x = 640/2 - p1.collider.w/2
event3.spawn.y = event3.collider.y - event3.collider.h - p1.collider.h
map_3.add_event(event3)
maps = [
map_1,
map_2,
map_3
]
map_active = map_1
pg.init()
pg.display.set_mode(screen.size)
while not game_exit:
ref_time = t.time()
for event in pg.event.get():
if event.type == pg.QUIT:
game_exit = True
# Map Transition
if transition:
for map in maps:
if map_active.id == map.id:
p1.collider.x = map.start.x
p1.collider.y = map.start.y
p1.speed = pg.Vector2()
transition = False
break
else:
print("unkonwn map id")
map_active = map_1
# Input
keys = pg.key.get_pressed()
if keys[pg.K_UP]:
p1.speed.y = -p1.max_speed
elif keys[pg.K_DOWN]:
p1.speed.y = p1.max_speed
else:
p1.speed.y = 0
if keys[pg.K_LEFT]:
p1.speed.x = -p1.max_speed
elif keys[pg.K_RIGHT]:
p1.speed.x = p1.max_speed
else:
p1.speed.x = 0
# Events
for event in map_active.event_list:
if collision.check(p1.collider, event.collider):
if event.id == event1.id and keys[pg.K_z]:
transition = True
map_active = map_2
map_1.start = event1.spawn
map_2.start = event4.spawn
elif event.id == event2.id and keys[pg.K_z]:
transition = True
map_active = map_3
map_2.start = event2.spawn
map_3.start = event3.spawn
elif event.id == event3.id and keys[pg.K_z]:
transition = True
map_active = map_2
map_2.start = event2.spawn
map_3.start = event3.spawn
elif event.id == event4.id and keys[pg.K_z]:
transition = True
map_active = map_1
map_1.start = event1.spawn
map_2.start = event4.spawn
# Collition
for box in map_active.dynamic_list:
box.collision.x = False
box.collision.y = False
speed_x = box.speed.x
speed_y = box.speed.y
for i in range(step+1):
for wall in map_active.static_list:
# Speed
speed = pg.Vector2()
speed.x = i*speed_x/step
speed.y = i*speed_y/step
f_speed = pg.Vector2()
f_speed.x = (i+1)*speed_x/step
f_speed.y = (i+1)*speed_y/step
# Collider
f_box = box.collider.copy()
f_box.x += f_speed.x
f_box.y += f_speed.y
f_box_x = box.collider.copy()
f_box_x.x += f_speed.x
f_box_x.y += speed.y
f_box_y = box.collider.copy()
f_box_y.x += speed.x
f_box_y.y += f_speed.y
# Flags
collision_x = collision.check(f_box_x, wall.collider)
collision_y = collision.check(f_box_y, wall.collider)
collision_xy = collision.check(f_box , wall.collider)
if collision_x and not collision_y:
box.collision.x = True
speed_x = speed.x
break
if collision_y and not collision_x:
box.collision.y = True
speed_y = speed.y
break
if collision_xy:
box.collision.x = True
box.collision.y = True
speed_x = (i-1)*speed_x/step
speed_y = (i-1)*speed_y/step
break
box.speed.x = speed_x
box.speed.y = speed_y
# Update
for box in map_active.dynamic_list:
box.update()
# Render
surface = pg.display.get_surface()
surface.fill(Color.black)
for box in map_active.render_list:
draw_box(box.collider, box.color)
pg.display.flip()
timestamp = t.time() - ref_time
if timestamp < 1/(fps+0.5):
t.sleep(1/(fps+0.5) - timestamp)
#print(round(1/(t.time() - ref_time)))
pg.quit()