Verify player is in bound of the map
This commit is contained in:
+19
-1
@@ -23,6 +23,10 @@ class Map:
|
|||||||
if 0 <= x < self.width and 0 <= y < self.height:
|
if 0 <= x < self.width and 0 <= y < self.height:
|
||||||
return self.layout[y][x]
|
return self.layout[y][x]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def is_position_in_bounds(self, position):
|
||||||
|
x, y = position
|
||||||
|
return 0 <= x < self.width and 0 <= y < self.height
|
||||||
|
|
||||||
def get_first_block_type_position(self, block_type):
|
def get_first_block_type_position(self, block_type):
|
||||||
for y in range(self.height):
|
for y in range(self.height):
|
||||||
@@ -31,6 +35,13 @@ class Map:
|
|||||||
if block and block.type == block_type:
|
if block and block.type == block_type:
|
||||||
return (x, y)
|
return (x, y)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def count_blocks_of_type(self, block_type):
|
||||||
|
return sum(
|
||||||
|
block is not None and block.type == block_type
|
||||||
|
for row in self.layout
|
||||||
|
for block in row
|
||||||
|
)
|
||||||
|
|
||||||
def get_block_type_at_position(self, position):
|
def get_block_type_at_position(self, position):
|
||||||
x, y = position
|
x, y = position
|
||||||
@@ -55,4 +66,11 @@ class Map:
|
|||||||
else:
|
else:
|
||||||
string_array_row.append(block.to_string())
|
string_array_row.append(block.to_string())
|
||||||
string_array.append(string_array_row)
|
string_array.append(string_array_row)
|
||||||
return string_array
|
return string_array
|
||||||
|
|
||||||
|
def to_type_dict(self):
|
||||||
|
"""Returns a matrix containing block types and None for empty spaces."""
|
||||||
|
return [
|
||||||
|
[block.type if block is not None else None for block in row]
|
||||||
|
for row in self.layout
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user