Compare commits

...

4 Commits

Author SHA1 Message Date
5eb2079da1 food work :3
All checks were successful
Build Simulation and Test / Run All Tests (push) Successful in 2m18s
2025-06-03 19:25:54 -05:00
da355dbc6c Merge branch 'food'
# Conflicts:
#	main.py
#	world/render_objects.py
2025-06-03 18:46:47 -05:00
fc171cd523 food turns yellow then black as it spoils, checks food amount and adds more to equal 10, randomizes position with seed 2025-06-02 17:01:19 -05:00
56857e24b8 food turns green as it spoils 2025-06-02 00:30:51 -05:00
3 changed files with 67 additions and 6 deletions

22
main.py
View File

@ -1,9 +1,10 @@
import pygame import pygame
import time import time
import sys import sys
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
@ -17,17 +18,19 @@ DARK_GRAY = (64, 64, 64)
GRAY = (128, 128, 128) GRAY = (128, 128, 128)
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
RENDER_BUFFER = 50 RENDER_BUFFER = 50
SPEED = 700 # Pixels per second
# Grid settings # Grid settings
GRID_WIDTH = 20 # Number of cells horizontally 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
@ -141,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
@ -225,6 +231,14 @@ def main():
total_ticks += 1 total_ticks += 1
# Add your tick-specific logic here # 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") print("Tick logic executed")
world.tick_all() world.tick_all()
@ -298,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)

View File

@ -1,6 +1,12 @@
from world.world import Position, BaseEntity from world.world import Position, BaseEntity
import pygame 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): class DebugRenderObject(BaseEntity):
def __init__(self, position: Position, radius=5): def __init__(self, position: Position, radius=5):
@ -33,3 +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(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})"

View File

@ -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,9 +130,8 @@ class BaseEntity(ABC):
def flag_for_death(self): def flag_for_death(self):
self.flags["death"] = True self.flags["death"] = True
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