diff --git a/main.py b/main.py index b4e1b05..9badd55 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import sys import random from world.world import World, Position -from world.render_objects import DebugRenderObject +from world.render_objects import DebugRenderObject, FoodObject from world.simulation_interface import Camera # Initialize Pygame @@ -25,11 +25,12 @@ GRID_WIDTH = 20 # Number of cells horizontally GRID_HEIGHT = 15 # Number of cells vertically CELL_SIZE = 20 # Size of each cell in pixels -DEFAULT_TPS = 5 # Amount of ticks per second for the simulation +DEFAULT_TPS = 20 # Number of ticks per second for the simulation +FOOD_SPAWNING = False def draw_grid(screen, camera, showing_grid=True): - # Fill screen with black + # Fill the screen with black screen.fill(BLACK) # Calculate effective cell size with zoom @@ -143,6 +144,9 @@ def main(): world.add_object(DebugRenderObject(Position(0, 0))) world.add_object(DebugRenderObject(Position(20, 0))) + # sets seed to 67 >_< + random.seed(67) + running = True while running: deltatime = clock.get_time() / 1000.0 # Convert milliseconds to seconds @@ -227,16 +231,16 @@ def main(): total_ticks += 1 # Add your tick-specific logic here - print("Tick logic executed") - world.tick_all() - # gets every object in the world and returns amount of FoodObjects objects = world.get_objects() + print(objects) food = len([obj for obj in objects if isinstance(obj, FoodObject)]) print(f"Food count: {food}") - if food < 10: - for i in range(10 - food): - world.add_object(FoodObject(Position(random.randint(-200, 200), random.randint(-200, 200)))) + if food < 10 and FOOD_SPAWNING == True: + world.add_object(FoodObject(Position(random.randint(-200, 200), random.randint(-200, 200)))) + + print("Tick logic executed") + world.tick_all() # Calculate TPS every second if current_time - last_tps_time >= 1.0: @@ -308,7 +312,7 @@ def main(): for each in selected_objects: obj = each obj_text = font.render( - f"Object: {str(obj)}, Neighbors: {obj.neighbors}", True, WHITE + f"Object: {str(obj)}", True, WHITE ) obj_rect = obj_text.get_rect() obj_rect.topleft = (10, 30 + i * 20) diff --git a/world/render_objects.py b/world/render_objects.py index 455a21a..a905198 100644 --- a/world/render_objects.py +++ b/world/render_objects.py @@ -39,17 +39,39 @@ class DebugRenderObject(BaseEntity): def __repr__(self): return f"DebugRenderObject({self.position}, neighbors={self.neighbors})" -class FoodObject: - def __init__(self, position: Position): - self.decay = 0 - self.position = position - def tick(self): +class FoodObject(BaseEntity): + def __init__(self, position: Position): + super().__init__(position) + + self.max_visual_width = 10 + self.decay = 0 + self.interaction_radius = 50 + self.flags = { + "death": False, + "can_interact": True, + } + + def tick(self, interactable=None): + if interactable is None: + interactable = [] + self.decay += 1 if self.decay > 255: - self.decay = 0 # eventually will raise a destroy flag + self.decay = 255 + self.flag_for_death() + + return self def render(self, camera, screen): if camera.is_in_view(*self.position.get_position()): - pygame.draw.circle(screen, (255-self.decay,food_decay_yellow(self.decay),0), camera.world_to_screen(*self.position.get_position()), 5 * camera.zoom) + pygame.draw.circle( + screen, + (255-self.decay,food_decay_yellow(self.decay),0), + camera.world_to_screen(*self.position.get_position()), + 5 * camera.zoom + ) + + def __repr__(self): + return f"FoodObject({self.position}, decay={self.decay})" \ No newline at end of file diff --git a/world/world.py b/world/world.py index e330709..85ab0a8 100644 --- a/world/world.py +++ b/world/world.py @@ -101,6 +101,12 @@ class World: closest_obj = obj return closest_obj + def get_objects(self): + all_objects = [] + for obj_list in self.buffers[self.current_buffer].values(): + all_objects.extend(obj_list) + print("All objects: ", all_objects) + return all_objects class BaseEntity(ABC): def __init__(self, position: "Position"): @@ -124,12 +130,8 @@ class BaseEntity(ABC): def flag_for_death(self): self.flags["death"] = True - - def get_objects(self): - return self.objects - class Position: - def __init__(self, x, y): + def __init__(self, x: int, y: int): self.x = x self.y = y