Simple plateformer game playing
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
Represent a map in the game.
|
||||
Contains the map data :
|
||||
- Map name
|
||||
- Map width and height
|
||||
- Map difficulty
|
||||
- The map layout (2D array of blocks)
|
||||
"""
|
||||
|
||||
from game.blocks.block import Block
|
||||
|
||||
|
||||
class Map:
|
||||
def __init__(self, map_data: dict):
|
||||
self.name = map_data.get('name', 'Unknown')
|
||||
self.width = int(map_data.get('width', 0))
|
||||
self.height = int(map_data.get('height', 0))
|
||||
self.difficulty = float(map_data.get('difficulty', 0.0))
|
||||
self.layout = map_data.get('layout', [])
|
||||
|
||||
def get_block_at(self, x, y) -> Block | None:
|
||||
if 0 <= x < self.width and 0 <= y < self.height:
|
||||
return self.layout[y][x]
|
||||
return None
|
||||
|
||||
def get_first_block_type_position(self, block_type):
|
||||
for y in range(self.height):
|
||||
for x in range(self.width):
|
||||
block = self.get_block_at(x, y)
|
||||
if block and block.type == block_type:
|
||||
return (x, y)
|
||||
return None
|
||||
|
||||
def get_block_type_at_position(self, position):
|
||||
x, y = position
|
||||
block = self.get_block_at(x, y)
|
||||
if block:
|
||||
return block.type
|
||||
return None
|
||||
|
||||
def to_string_dict(self):
|
||||
"""
|
||||
Returns a string matrix representation of the map layout.
|
||||
"""
|
||||
string_array = []
|
||||
for row in self.layout:
|
||||
string_array_row = []
|
||||
for block in row:
|
||||
if block is None:
|
||||
string_array_row.append(" ")
|
||||
else:
|
||||
string_array_row.append(block.to_string())
|
||||
string_array.append(string_array_row)
|
||||
return string_array
|
||||
Reference in New Issue
Block a user