-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrolling_fun_bug.py
More file actions
312 lines (241 loc) · 7.83 KB
/
scrolling_fun_bug.py
File metadata and controls
312 lines (241 loc) · 7.83 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import math
import time as t
import random as r
import pygame as pg
import json
from pathlib import Path
class Bool2:
def __init__(self, x=False, y=False):
self.x = x
self.y = y
class Color:
black = ( 25, 25, 25)
white = (200,200,200)
red = (200, 25, 25)
green = ( 25,200, 25)
blue = ( 25, 25,200)
class Box:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def copy(self):
return Box(self.x, self.y, self.w, self.h)
@property
def rect(self):
return pg.Rect(self.x, self.y, self.w, self.h)
@property
def size(self):
return (self.w, self.h)
class Entity:
def __init__(self, x=0, y=0, w=0, h=0, color=Color.black):
self.collider = Box(x, y, w, h)
self.collision = Bool2()
self.speed = pg.Vector2()
self.color = color
def update(self):
self.collider.x += self.speed.x
self.collider.y += self.speed.y
class Warp:
def __init__(self, x=0, y=0, w=0, h=0, color=Color.black, destination=''):
self.collider = Box(x, y, w, h)
self.color = color
self.destination = destination
class Key:
def __init__(self, key):
self.id = key
self.state = False
self.press = False
self.press_lock = False
def update(self, keys):
value = keys[self.id]
self.state = value
if not self.press_lock:
self.press = True
self.press_lock = True
else:
self.press = False
if not value:
self.press = False
self.press_lock = False
class Controller:
def __init__(self):
self.up = Key(pg.K_UP)
self.left = Key(pg.K_LEFT)
self.right = Key(pg.K_RIGHT)
self.down = Key(pg.K_DOWN)
self.accept = Key(pg.K_z)
self.refect = Key(pg.K_x)
self.keys = [
self.up,
self.left,
self.right,
self.accept,
self.refect
]
def update(self):
keys = pg.key.get_pressed()
for key in self.keys:
key.update(keys)
def draw_box(box: Box, color: Color):
surface = pg.display.get_surface()
pg.draw.rect(surface, color, box.rect)
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 load_level(level_path):
# TODO: Exceptions?
# Maybe parse game objects here instead that on lists later?
with open(level_path) as f:
data = json.load(f)
return data['name'], data
def main():
root = Path(__file__).parent
fps = 60
step = 10
gravity = 5
speed_limit = 20
speed_lock_left = False
speed_lock_right = False
game_exit = False
jump_lock = False
next_level = None
screen = Box(0, 0, 640, 480)
levels = {k: v for k, v in map(load_level, (root / 'levels').iterdir())}
data = levels['level1']
p1 = Entity(**data['player'])
floors = [Entity(**i) for i in data['walls']]
warps = [Warp(**i) for i in data['warp']]
dynamic_list = [p1]
static_list = floors
render_list = dynamic_list + floors + warps
pg.init()
pg.display.set_mode(screen.size)
ref_time = t.time()
controls = Controller()
while not game_exit:
for event in pg.event.get():
if event.type == pg.QUIT:
game_exit = True
break
keys = pg.key.get_pressed()
controls.update()
print(controls.accept.state)
# Input
if controls.accept.state:
if not jump_lock:
p1.speed.y += -8
if p1.speed.y < -30:
jump_lock = True
else:
if p1.speed.y == 0:
jump_lock = False
else:
jump_lock = True
if controls.left.state:
if not speed_lock_left:
p1.speed.x = 0
p1.speed.x -= 1
if p1.speed.x < -speed_limit:
p1.speed.x = -speed_limit
speed_lock_left = True
elif controls.right.state:
if not speed_lock_right:
p1.speed.x = 0
p1.speed.x += 1
if p1.speed.x > speed_limit:
p1.speed.x = speed_limit
speed_lock_right = True
else:
p1.speed.x = 0
if not controls.left.state:
speed_lock_left = False
if not controls.right.state:
speed_lock_right = False
# Apply Force
for box in dynamic_list:
box.speed.y += gravity
# Collision
if p1.collider.y > 10000: # Return to stage
p1.collider.x = screen.w/2 - p1.collider.w/2
p1.collider.y = 0
p1.speed.y = 0
for box in dynamic_list:
box.collision.x = False
box.collision.y = False
for wall in static_list:
for i in range(1, step+1):
f_box = box.collider.copy()
f_box.x += i*box.speed.x/step
f_box.y += i*box.speed.y/step
f_box_x = box.collider.copy()
f_box_x.x += i*box.speed.x/step
f_box_x.y += (i-1)*box.speed.y/step
f_box_y = box.collider.copy()
f_box_y.x += (i-1)*box.speed.x/step
f_box_y.y += i*box.speed.y/step
colition_x = check_collition(f_box_x, wall.collider)
colition_y = check_collition(f_box_y, wall.collider)
colition_xy = check_collition(f_box, wall.collider)
if colition_x and not colition_y:
box.collision.x = True
box.speed.x = (i-1)*box.speed.x/step
break
if colition_y and not colition_x:
box.collision.y = True
box.speed.y = (i-1)*box.speed.y/step
break
if colition_xy:
box.collision.x = True
box.collision.y = True
box.speed.x = (i-1)*box.speed.x/step
box.speed.y = (i-1)*box.speed.y/step
break
for box in warps:
if check_collition(p1.collider, box.collider):
next_level = box.destination
# Update
for box in dynamic_list:
box.update()
screen.x = p1.collider.x + p1.collider.w/2 - screen.w/2
#screen.y = p1.collider.y + p1.collider.h/2 - screen.h/2
# Render
surface = pg.display.get_surface()
surface.fill(Color.black)
for box in render_list:
temp_box = box.collider.copy()
temp_box.x -= screen.x
temp_box.y -= screen.y
draw_box(temp_box, box.color)
pg.display.flip()
if next_level:
data = levels[next_level]
p1 = Entity(**data['player'])
floors = [Entity(**i) for i in data['walls']]
warps = [Warp(**i) for i in data['warp']]
dynamic_list = [p1]
static_list = floors
render_list = dynamic_list + floors + warps
next_level = None
timestamp = t.time() - ref_time
if timestamp < 1/(fps):
timestamp1 = t.time() - ref_time
t.sleep(1/(fps) - timestamp1)
#print(round(1/(t.time() - ref_time)))
ref_time = t.time()
pg.quit()
main()