Compare commits
	
		
			4 Commits
		
	
	
		
			b80a5afc4a
			...
			5eb2079da1
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5eb2079da1 | |||
| da355dbc6c | |||
| fc171cd523 | |||
| 56857e24b8 | 
							
								
								
									
										22
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								main.py
									
									
									
									
									
								
							| @ -1,9 +1,10 @@ | ||||
| import pygame | ||||
| import time | ||||
| 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 | ||||
| @ -17,17 +18,19 @@ DARK_GRAY = (64, 64, 64) | ||||
| GRAY = (128, 128, 128) | ||||
| WHITE = (255, 255, 255) | ||||
| RENDER_BUFFER = 50 | ||||
| SPEED = 700 # Pixels per second | ||||
| 
 | ||||
| # Grid settings | ||||
| 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 | ||||
| @ -141,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 | ||||
| @ -225,6 +231,14 @@ def main(): | ||||
|             total_ticks += 1 | ||||
|             # Add your tick-specific logic here | ||||
| 
 | ||||
|             # 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 and FOOD_SPAWNING == True: | ||||
|                 world.add_object(FoodObject(Position(random.randint(-200, 200), random.randint(-200, 200)))) | ||||
| 
 | ||||
|             print("Tick logic executed") | ||||
|             world.tick_all() | ||||
| 
 | ||||
| @ -298,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) | ||||
|  | ||||
| @ -1,6 +1,12 @@ | ||||
| from world.world import Position, BaseEntity | ||||
| import pygame | ||||
| 
 | ||||
| # returns desired yellow value for food decay | ||||
| def food_decay_yellow(decay): | ||||
|     if decay < 128: | ||||
|         return decay | ||||
|     else: | ||||
|         return 255 - decay | ||||
| 
 | ||||
| class DebugRenderObject(BaseEntity): | ||||
|     def __init__(self, position: Position, radius=5): | ||||
| @ -33,3 +39,39 @@ class DebugRenderObject(BaseEntity): | ||||
| 
 | ||||
|     def __repr__(self): | ||||
|         return f"DebugRenderObject({self.position}, neighbors={self.neighbors})" | ||||
| 
 | ||||
| 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 = 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 | ||||
|             ) | ||||
| 
 | ||||
|     def __repr__(self): | ||||
|         return f"FoodObject({self.position}, decay={self.decay})" | ||||
| @ -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,9 +130,8 @@ class BaseEntity(ABC): | ||||
|     def flag_for_death(self): | ||||
|         self.flags["death"] = True | ||||
| 
 | ||||
| 
 | ||||
| class Position: | ||||
|     def __init__(self, x, y): | ||||
|     def __init__(self, x: int, y: int): | ||||
|         self.x = x | ||||
|         self.y = y | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user