food work :3
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Build Simulation and Test / Run All Tests (push) Successful in 2m18s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Build Simulation and Test / Run All Tests (push) Successful in 2m18s
				
			This commit is contained in:
		
							parent
							
								
									da355dbc6c
								
							
						
					
					
						commit
						5eb2079da1
					
				
							
								
								
									
										22
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								main.py
									
									
									
									
									
								
							| @ -4,7 +4,7 @@ import sys | |||||||
| import random | import random | ||||||
| 
 | 
 | ||||||
| from world.world import World, Position | 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 | from world.simulation_interface import Camera | ||||||
| 
 | 
 | ||||||
| # Initialize Pygame | # Initialize Pygame | ||||||
| @ -25,11 +25,12 @@ GRID_WIDTH = 20  # Number of cells horizontally | |||||||
| GRID_HEIGHT = 15  # Number of cells vertically | GRID_HEIGHT = 15  # Number of cells vertically | ||||||
| CELL_SIZE = 20  # Size of each cell in pixels | 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): | def draw_grid(screen, camera, showing_grid=True): | ||||||
|     # Fill screen with black |     # Fill the screen with black | ||||||
|     screen.fill(BLACK) |     screen.fill(BLACK) | ||||||
| 
 | 
 | ||||||
|     # Calculate effective cell size with zoom |     # Calculate effective cell size with zoom | ||||||
| @ -143,6 +144,9 @@ def main(): | |||||||
|     world.add_object(DebugRenderObject(Position(0, 0))) |     world.add_object(DebugRenderObject(Position(0, 0))) | ||||||
|     world.add_object(DebugRenderObject(Position(20, 0))) |     world.add_object(DebugRenderObject(Position(20, 0))) | ||||||
| 
 | 
 | ||||||
|  |     # sets seed to 67 >_< | ||||||
|  |     random.seed(67) | ||||||
|  | 
 | ||||||
|     running = True |     running = True | ||||||
|     while running: |     while running: | ||||||
|         deltatime = clock.get_time() / 1000.0  # Convert milliseconds to seconds |         deltatime = clock.get_time() / 1000.0  # Convert milliseconds to seconds | ||||||
| @ -227,17 +231,17 @@ def main(): | |||||||
|             total_ticks += 1 |             total_ticks += 1 | ||||||
|             # Add your tick-specific logic here |             # Add your tick-specific logic here | ||||||
| 
 | 
 | ||||||
|             print("Tick logic executed") |  | ||||||
|             world.tick_all() |  | ||||||
| 
 |  | ||||||
|             # gets every object in the world and returns amount of FoodObjects |             # gets every object in the world and returns amount of FoodObjects | ||||||
|             objects = world.get_objects() |             objects = world.get_objects() | ||||||
|  |             print(objects) | ||||||
|             food = len([obj for obj in objects if isinstance(obj, FoodObject)]) |             food = len([obj for obj in objects if isinstance(obj, FoodObject)]) | ||||||
|             print(f"Food count: {food}") |             print(f"Food count: {food}") | ||||||
|             if food < 10: |             if food < 10 and FOOD_SPAWNING == True: | ||||||
|                 for i in range(10 - food): |  | ||||||
|                 world.add_object(FoodObject(Position(random.randint(-200, 200), random.randint(-200, 200)))) |                 world.add_object(FoodObject(Position(random.randint(-200, 200), random.randint(-200, 200)))) | ||||||
| 
 | 
 | ||||||
|  |             print("Tick logic executed") | ||||||
|  |             world.tick_all() | ||||||
|  | 
 | ||||||
|         # Calculate TPS every second |         # Calculate TPS every second | ||||||
|         if current_time - last_tps_time >= 1.0: |         if current_time - last_tps_time >= 1.0: | ||||||
|             actual_tps = tick_counter |             actual_tps = tick_counter | ||||||
| @ -308,7 +312,7 @@ def main(): | |||||||
|             for each in selected_objects: |             for each in selected_objects: | ||||||
|                 obj = each |                 obj = each | ||||||
|                 obj_text = font.render( |                 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 = obj_text.get_rect() | ||||||
|                 obj_rect.topleft = (10, 30 + i * 20) |                 obj_rect.topleft = (10, 30 + i * 20) | ||||||
|  | |||||||
| @ -39,17 +39,39 @@ class DebugRenderObject(BaseEntity): | |||||||
| 
 | 
 | ||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         return f"DebugRenderObject({self.position}, neighbors={self.neighbors})" |         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 |         self.decay += 1 | ||||||
| 
 | 
 | ||||||
|         if self.decay > 255: |         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): |     def render(self, camera, screen): | ||||||
|         if camera.is_in_view(*self.position.get_position()): |         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})" | ||||||
| @ -101,6 +101,12 @@ class World: | |||||||
|                     closest_obj = obj |                     closest_obj = obj | ||||||
|         return closest_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): | class BaseEntity(ABC): | ||||||
|     def __init__(self, position: "Position"): |     def __init__(self, position: "Position"): | ||||||
| @ -124,12 +130,8 @@ class BaseEntity(ABC): | |||||||
|     def flag_for_death(self): |     def flag_for_death(self): | ||||||
|         self.flags["death"] = True |         self.flags["death"] = True | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     def get_objects(self): |  | ||||||
|         return self.objects |  | ||||||
| 
 |  | ||||||
| class Position: | class Position: | ||||||
|     def __init__(self, x, y): |     def __init__(self, x: int, y: int): | ||||||
|         self.x = x |         self.x = x | ||||||
|         self.y = y |         self.y = y | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user