-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
93 lines (70 loc) · 2.97 KB
/
utils.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
import arcade
import json
def compute_first_free_slots(arcade_game):
"""
Computes the first_free_slots list containing the first y coordinate available to place a block for each x position.
The function computes the list for the current level.
It should be used only once each time a level is edited and is not a user feature.
The list is saved in levels.json file.
If an element of the list is -1, it means the column is full.
Args:
arcade_game: game instance
Returns: List (first_free_slots)
"""
first_free_slots = []
tile_size = arcade_game.tile_size * arcade_game.level_data["scaling"]
offset = arcade_game.level_data["offset"]
# Calculate the number of rows of blocks in the level
columns_num = int(arcade_game.screen_resolution[0] // tile_size)
# For each column, get first free vertical slot
for column in range(offset, columns_num):
# Get the first row available of the column
current_row = 0
column_coord = column * tile_size
row_coord = current_row * tile_size
# Check if the first row is immediately free
if not (arcade.get_sprites_at_point(
(column_coord + 1, row_coord + 1), arcade_game.scene["Platforms"])
or
arcade.get_sprites_at_point(
(column_coord + 1, row_coord + 1), arcade_game.scene["BackgroundPlatforms"])):
# + 1 on the coords makes sure we're not checking at zero which will never be True
free = True
else:
free = False
# Check if the row above is free, repeat until one is
while not free:
current_row += 1
row_coord = current_row * tile_size
if not (arcade.get_sprites_at_point(
(column_coord + 1, row_coord + 1), arcade_game.scene["Platforms"])
or
arcade.get_sprites_at_point(
(column_coord + 1, row_coord + 1), arcade_game.scene["BackgroundPlatforms"])):
free = True
first_free_slots.append(current_row)
return first_free_slots
def save_free_slots(arcade_game):
"""
Calls compute_first_free_slots and saves the result in levels.json.
Args:
arcade_game: game instance
Returns:
"""
# Compute
first_free_slots = compute_first_free_slots(arcade_game)
# Print in console (again, this should not be performed by players)
print(first_free_slots)
# Update the levels.json file
arcade_game.level_data["first_free_slots"] = first_free_slots
with open("levels.json", "w") as levels_file:
json.dump(arcade_game.levels, levels_file, indent=2)
def write_save(arcade_game):
"""
Writes in the save.json file the new save file. Called when the game is closed.
Args:
arcade_game: arcade game instance object
Returns:
"""
with open("save.json", "w") as save_file:
json.dump(arcade_game.save, save_file, indent=2)