-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcreate_level.py
More file actions
420 lines (376 loc) 路 15 KB
/
Copy pathcreate_level.py
File metadata and controls
420 lines (376 loc) 路 15 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""
This code creates the layout of level one.
"""
import pymunk
import arcade
import random
import sys
import logging
from random import randint, uniform
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
from physics_utility import (
PymunkSprite,
)
# from constants import *
import constants
from k8s_kill_pod import count_pods
def create_floor(space, sprite_list):
"""Create a bunch of blocks for the floor."""
# Layer of grass
for x in range(-constants.MAP_SIZE, constants.MAP_SIZE, constants.SPRITE_SIZE):
sprite = arcade.Sprite("./images/tiles/grassMid.png", constants.SPRITE_SCALING)
sprite.center_y = 0
sprite.center_x = x
sprite_list.append(sprite)
# print(sprite.center_y)
# Create one big rectangle as the floor physic body, instead of one per tile
xpos = constants.SCREEN_WIDTH / 2
ypos = 0
body = pymunk.Body(body_type=pymunk.Body.STATIC)
body.position = pymunk.Vec2d(xpos, ypos)
shape = pymunk.Poly.create_box(
body, (constants.MAP_SIZE * 2, constants.SPRITE_SIZE)
)
shape.friction = constants.DEFAULT_FRICTION
space.add(body, shape)
# First layer of dirt
for x in range(-constants.MAP_SIZE, constants.MAP_SIZE, constants.SPRITE_SIZE):
y = -constants.SPRITE_SIZE + 1
# y = constants.SPRITE_SIZE - (constants.SPRITE_SIZE * 2)
sprite = PymunkSprite(
"./images/tiles/grassCenter.png",
x,
y,
scale=constants.SPRITE_SCALING,
body_type=pymunk.Body.STATIC,
)
sprite_list.append(sprite)
# Extra layer of dirt
for x in range(-constants.MAP_SIZE, constants.MAP_SIZE, constants.SPRITE_SIZE):
y = -constants.SPRITE_SIZE - constants.SPRITE_SIZE + 1
# y = constants.SPRITE_SIZE - (constants.SPRITE_SIZE * 3)
sprite = PymunkSprite(
"./images/tiles/grassCenter.png",
x,
y,
scale=constants.SPRITE_SCALING,
body_type=pymunk.Body.STATIC,
)
sprite_list.append(sprite)
def create_walls(space, sprite_list):
"""Create side walls"""
# Left wall
for y in range(96, 2000, constants.SPRITE_SIZE): # was 96 - constants.SPRITE_SIZE
# x = constants.SPRITE_SIZE / 4
x = -1200 - constants.SPRITE_SIZE
sprite = PymunkSprite(
"./images/tiles/brickBrown.png",
x,
y,
scale=0.5,
body_type=pymunk.Body.STATIC,
)
sprite_list.append(sprite)
space.add(sprite.body, sprite.shape)
# Right wall
for y in range(96, 2000, constants.SPRITE_SIZE):
# x = constants.SPRITE_SIZE / 4
x = 2200 - constants.SPRITE_SIZE
sprite = PymunkSprite(
"./images/tiles/brickBrown.png",
x,
y,
scale=0.5,
body_type=pymunk.Body.STATIC,
)
sprite_list.append(sprite)
space.add(sprite.body, sprite.shape)
def create_platform(space, sprite_list, start_x, y, count):
"""Create a platform"""
for x in range(
start_x, start_x + count * constants.SPRITE_SIZE + 1, constants.SPRITE_SIZE
):
sprite = PymunkSprite(
"./images/tiles/grassMid.png", x, y, scale=0.5, body_type=pymunk.Body.STATIC
)
sprite_list.append(sprite)
space.add(sprite.body, sprite.shape)
def create_hill(space, sprite_list, start_x, y, count):
"""Create a hill"""
# Left edge
sprite = PymunkSprite(
"./images/tiles/grassHill_right.png",
start_x - constants.SPRITE_SIZE,
y,
scale=constants.SPRITE_SCALING,
body_type=pymunk.Body.STATIC,
)
t = pymunk.Transform(tx=-60, ty=-96)
sprite.shape = pymunk.Poly(
sprite.body, [(80, 120), (80, 60), (20, 60)], transform=t
)
sprite.shape.friction = constants.DEFAULT_FRICTION / 4
sprite_list.append(sprite)
space.add(sprite.body, sprite.shape)
# Add dirt under
dirt_y = 0
sprite = PymunkSprite(
"./images/tiles/grassCorner_right.png",
start_x - constants.SPRITE_SIZE,
dirt_y,
scale=constants.SPRITE_SCALING,
body_type=pymunk.Body.STATIC,
)
sprite_list.append(sprite)
# Middle
for x in range(
start_x, start_x + count * constants.SPRITE_SIZE + 1, constants.SPRITE_SIZE
):
sprite = PymunkSprite(
"./images/tiles/grassMid.png",
x,
y,
scale=constants.SPRITE_SCALING,
body_type=pymunk.Body.STATIC,
)
sprite_list.append(sprite)
space.add(sprite.body, sprite.shape)
# Add dirt under
dirt_y = 0
sprite = PymunkSprite(
"./images/tiles/grassCenter.png",
x,
dirt_y,
scale=constants.SPRITE_SCALING,
body_type=pymunk.Body.STATIC,
)
sprite_list.append(sprite)
# Right edge
sprite = PymunkSprite(
"./images/tiles/grassHill_left.png",
start_x + constants.SPRITE_SIZE * count + constants.SPRITE_SIZE,
y,
scale=constants.SPRITE_SCALING,
body_type=pymunk.Body.STATIC,
)
sprite.shape.friction = constants.DEFAULT_FRICTION / 4
t = pymunk.Transform(tx=-60, ty=-96)
sprite.shape = pymunk.Poly(
sprite.body, [(20, 120), (20, 60), (80, 60)], transform=t
)
sprite_list.append(sprite)
space.add(sprite.body, sprite.shape)
# Add dirt under
dirt_y = 0
sprite = PymunkSprite(
"./images/tiles/grassCorner_left.png",
start_x + constants.SPRITE_SIZE * count + constants.SPRITE_SIZE,
dirt_y,
scale=constants.SPRITE_SCALING,
body_type=pymunk.Body.STATIC,
)
sprite_list.append(sprite)
def decorate_cactus(sprite_list, start_x, y, count):
"""Create a cactus"""
for x in range(0, count):
# print(x)
X_POSITION = randint(-3000, 3000) # Initial random position
POSITION_OK = False
while POSITION_OK == False:
if any(
X_POSITION in range(s.center_x - 40, s.center_x + 40)
for s in sprite_list
):
# print(X_POSITION, s)
X_POSITION = randint(-3000, 3000)
else:
# print("Outside of if any, cactus position good!")
POSITION_OK = True
sprite = arcade.Sprite("./images/tiles/cactus.png", 0.5)
sprite.center_y = y
sprite.center_x = X_POSITION # Randomly place cacti
sprite_list.append(sprite)
# print(len(sprite_list))
def decorate_cactus_large(sprite_list, start_x, y, count):
"""Create a cactus"""
for x in range(0, count):
# print(x)
X_POSITION = randint(-3000, 3000) # Initial random position
POSITION_OK = False
while POSITION_OK == False:
if any(
X_POSITION in range(s.center_x - 100, s.center_x + 100)
for s in sprite_list
):
# print(X_POSITION, s)
X_POSITION = randint(-3000, 3000)
else:
# print("Outside of if any, cactus position good!")
POSITION_OK = True
sprite = arcade.Sprite("./images/tiles/cactus.png", 1)
sprite.center_y = y
sprite.center_x = X_POSITION # Randomly place cacti
sprite_list.append(sprite)
def decorate_cactus_tiny(sprite_list, start_x, y, count):
"""Create a distant cactus"""
for x in range(0, count):
# print(x)
X_POSITION = randint(-3000, 3000) # Initial random position
POSITION_OK = False
while POSITION_OK == False:
if any(
X_POSITION in range(s.center_x - 200, s.center_x + 200)
for s in sprite_list
):
# print(X_POSITION, s)
X_POSITION = randint(-3000, 3000)
else:
# print("Outside of if any, cactus position good!")
POSITION_OK = True
sprite = arcade.Sprite("./images/tiles/cactus2.png", 0.2)
sprite.center_y = y
sprite.center_x = X_POSITION # Randomly place cacti
sprite_list.append(sprite)
def decorate_grass(sprite_list, start_x, y, count):
"""Create some grass"""
for x in range(0, count):
X_POSITION = randint(-3000, 3000) # Initial random position
POSITION_OK = False
while POSITION_OK == False:
if any(
X_POSITION in range(s.center_x - 20, s.center_x + 20)
for s in sprite_list
):
# print(X_POSITION, s)
X_POSITION = randint(-3000, 3000)
else:
# print("Outside of if any, grass position good!")
POSITION_OK = True
sprite = arcade.Sprite("./images/tiles/grass_sprout.png", 0.5)
sprite.center_y = y
sprite.center_x = X_POSITION # Randomly place grass
sprite_list.append(sprite)
def decorate_rock(sprite_list, start_x, y, count):
"""Create some rocks"""
for x in range(0, count):
X_POSITION = randint(-3000, 3000) # Initial random position
POSITION_OK = False
while POSITION_OK == False:
if any(
X_POSITION in range(s.center_x - 10, s.center_x + 10)
for s in sprite_list
):
# print(X_POSITION, s)
X_POSITION = randint(-1000, 2000)
else:
# print("Outside of if any, rock position good!")
POSITION_OK = True
sprite = arcade.Sprite("./images/tiles/rock.png", 0.5)
sprite.center_y = y
sprite.center_x = X_POSITION # Randomly place rock
sprite_list.append(sprite)
def decorate_rock_small(sprite_list, start_x, y, count):
"""Create some rocks"""
for x in range(0, count):
X_POSITION = randint(-3000, 3000) # Initial random position
POSITION_OK = False
while POSITION_OK == False:
if any(
X_POSITION in range(s.center_x - 10, s.center_x + 10)
for s in sprite_list
):
# print(X_POSITION, s)
X_POSITION = randint(-3000, 3000)
else:
# print("Outside of if any, rock position good!")
POSITION_OK = True
sprite = arcade.Sprite("./images/tiles/rock.png", 0.2)
sprite.center_y = y
sprite.center_x = X_POSITION # Randomly place rock
sprite_list.append(sprite)
def decorate_clouds(sprite_list, count):
"""Create some clouds"""
for x in range(0, count):
# sprite = arcade.Sprite("./images/misc/cloud" + str(randint(1, 5)) + ".png", random.uniform(0.3,1))
sprite = arcade.Sprite(
"./images/misc/mbcloud" + str(randint(1, 2)) + ".png",
random.uniform(0.2, 1),
)
sprite.center_y = randint(700, 3000)
sprite.center_x = randint(-2500, 2500)
# print(sprite.center_x)
sprite_list.append(sprite)
def create_level_1(
space, static_sprite_list, dynamic_sprite_list, bg_sprite_list, fg_sprite_list
):
"""Create level one."""
create_floor(space, static_sprite_list)
# Add hills
create_hill(
space, static_sprite_list, -constants.SPRITE_SIZE * 40, constants.SPRITE_SIZE, 3
)
create_hill(
space, static_sprite_list, constants.SPRITE_SIZE * 34, constants.SPRITE_SIZE, 4
)
# create_walls(space, static_sprite_list)
create_platform(space, static_sprite_list, 200, constants.SPRITE_SIZE * 3, 3)
create_platform(space, static_sprite_list, 500, constants.SPRITE_SIZE * 6, 3)
create_platform(space, static_sprite_list, 200, constants.SPRITE_SIZE * 9, 3)
create_platform(space, static_sprite_list, -300, constants.SPRITE_SIZE * 3, 3)
create_platform(space, static_sprite_list, -600, constants.SPRITE_SIZE * 6, 2)
# Far left platforms
create_platform(space, static_sprite_list, -900, constants.SPRITE_SIZE * 9, 3)
create_platform(space, static_sprite_list, -1200, constants.SPRITE_SIZE * 3, 1)
create_platform(space, static_sprite_list, -1600, constants.SPRITE_SIZE * 6, 2)
create_platform(space, static_sprite_list, -1800, constants.SPRITE_SIZE * 13, 4)
create_platform(space, static_sprite_list, -2100, constants.SPRITE_SIZE * 9, 3)
create_platform(space, static_sprite_list, -840, constants.SPRITE_SIZE * 13, 1)
create_platform(space, static_sprite_list, 0, constants.SPRITE_SIZE * 13, 3)
# Add some more to the right
create_platform(space, static_sprite_list, 1040, constants.SPRITE_SIZE * 4, 3)
create_platform(space, static_sprite_list, 1440, constants.SPRITE_SIZE * 6, 2)
create_platform(space, static_sprite_list, 1840, constants.SPRITE_SIZE * 8, 1)
create_platform(space, static_sprite_list, 800, constants.SPRITE_SIZE * 8, 1)
create_platform(space, static_sprite_list, 2300, constants.SPRITE_SIZE * 10, 1)
# Add decorations
decorate_cactus(bg_sprite_list, 0, constants.SPRITE_SIZE, 12) # Cacti along ground
decorate_cactus_large(bg_sprite_list, 0, constants.SPRITE_SIZE + 30, 4)
decorate_cactus_tiny(bg_sprite_list, 0, constants.SPRITE_SIZE - 20, 4)
decorate_grass(bg_sprite_list, 0, constants.SPRITE_SIZE, 20)
decorate_rock(fg_sprite_list, 0, constants.SPRITE_SIZE, 3)
decorate_rock_small(bg_sprite_list, 0, constants.SPRITE_SIZE - 20, 10)
decorate_clouds(bg_sprite_list, 20)
# Create the stacks of boxes based on number of running pods or create random ones if offline mode
# print(constants.OFFLINE_MODE)
if constants.OFFLINE_MODE == True:
CRATE_COUNT = constants.OFFLINE_CRATE_COUNT
else:
logging.info("Attempting to connect to Kubernetes API host..")
try:
CRATE_COUNT = count_pods()
except:
logging.error("Unable to connect to Kubernetes API host")
logging.error("Check your Kubernetes environment/kubeconfig file")
logging.error("Or feel free to start with --offline yes set")
sys.exit(1)
logging.info("Creating %s crates", int(CRATE_COUNT * constants.CONTAINER_FACTOR))
# Create crates in random locations, based on number of pods * CONTAINER_FACTOR
i = 0
while i < int(CRATE_COUNT * constants.CONTAINER_FACTOR):
x = random.randrange(-2000, 2200)
y = random.randrange(200, 7000) # Drop crates in from random heights
# print(x)
# print(y)
sprite = PymunkSprite(
"./images/tiles/boxCrate_double.png",
x,
y,
scale=constants.SPRITE_SCALING,
friction=0.6,
)
dynamic_sprite_list.append(sprite)
space.add(sprite.body, sprite.shape)
# fall_sound = arcade.load_sound("./sounds/wooddrop.wav")
# arcade.play_sound(fall_sound)
i += 1
# logging.info("Number of crates created: %s", len(dynamic_sprite_list))