Enhance World initialization with size parameters and adjust food spawning range
All checks were successful
Build Simulation and Test / Run All Tests (push) Successful in 27s
All checks were successful
Build Simulation and Test / Run All Tests (push) Successful in 27s
This commit is contained in:
parent
f0e00d66b6
commit
dad79942c2
4
main.py
4
main.py
@ -140,7 +140,7 @@ def main():
|
|||||||
print("ESC or close window - Exit")
|
print("ESC or close window - Exit")
|
||||||
|
|
||||||
# Initialize world
|
# Initialize world
|
||||||
world = World(CELL_SIZE)
|
world = World(CELL_SIZE, (CELL_SIZE * GRID_WIDTH, CELL_SIZE * GRID_HEIGHT))
|
||||||
|
|
||||||
world.add_object(DebugRenderObject(Position(x=0, y=0)))
|
world.add_object(DebugRenderObject(Position(x=0, y=0)))
|
||||||
world.add_object(DebugRenderObject(Position(x=20, y=0)))
|
world.add_object(DebugRenderObject(Position(x=20, y=0)))
|
||||||
@ -238,7 +238,7 @@ def main():
|
|||||||
food = len([obj for obj in objects if isinstance(obj, FoodObject)])
|
food = len([obj for obj in objects if isinstance(obj, FoodObject)])
|
||||||
|
|
||||||
if food < 10 and FOOD_SPAWNING == True:
|
if food < 10 and FOOD_SPAWNING == True:
|
||||||
world.add_object(FoodObject(Position(x=random.randint(-200, 200), y=random.randint(-200, 200))))
|
world.add_object(FoodObject(Position(x=random.randint(-100, 100), y=random.randint(-100, 100))))
|
||||||
|
|
||||||
# ensure selected objects are still valid or have not changed position, if so, reselect them
|
# ensure selected objects are still valid or have not changed position, if so, reselect them
|
||||||
selected_objects = [
|
selected_objects = [
|
||||||
|
|||||||
@ -90,7 +90,7 @@ class World:
|
|||||||
A world-class that contains and manages all objects in the game using spatial partitioning.
|
A world-class that contains and manages all objects in the game using spatial partitioning.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, partition_size: int = 10) -> None:
|
def __init__(self, partition_size: int = 10, world_size: tuple[int, int] = (400, 300)) -> None:
|
||||||
"""
|
"""
|
||||||
Initializes the world with a partition size.
|
Initializes the world with a partition size.
|
||||||
|
|
||||||
@ -98,6 +98,7 @@ class World:
|
|||||||
"""
|
"""
|
||||||
self.partition_size: int = partition_size
|
self.partition_size: int = partition_size
|
||||||
self.buffers: List[Dict[Tuple[int, int], List[BaseEntity]]] = [defaultdict(list), defaultdict(list)]
|
self.buffers: List[Dict[Tuple[int, int], List[BaseEntity]]] = [defaultdict(list), defaultdict(list)]
|
||||||
|
self.world_size: Tuple[int, int] = world_size
|
||||||
self.current_buffer: int = 0
|
self.current_buffer: int = 0
|
||||||
|
|
||||||
def _hash_position(self, position: Position) -> Tuple[int, int]:
|
def _hash_position(self, position: Position) -> Tuple[int, int]:
|
||||||
@ -107,6 +108,11 @@ class World:
|
|||||||
:param position: A Position object representing the position in the world.
|
:param position: A Position object representing the position in the world.
|
||||||
:return: Tuple (cell_x, cell_y) representing the cell coordinates.
|
:return: Tuple (cell_x, cell_y) representing the cell coordinates.
|
||||||
"""
|
"""
|
||||||
|
# Ensure position is within world bounds, considering a center origin
|
||||||
|
if position.x < -self.world_size[0] / 2 or position.x >= self.world_size[0] / 2 or position.y < - \
|
||||||
|
self.world_size[1] / 2 or position.y >= self.world_size[1] / 2:
|
||||||
|
raise ValueError(f"Position is out of world bounds. {position} is out of bounds.")
|
||||||
|
|
||||||
return int(position.x // self.partition_size), int(position.y // self.partition_size)
|
return int(position.x // self.partition_size), int(position.y // self.partition_size)
|
||||||
|
|
||||||
def render_all(self, camera: Any, screen: Any) -> None:
|
def render_all(self, camera: Any, screen: Any) -> None:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user