Merge branch 'food'
# Conflicts: # main.py # world/render_objects.py
This commit is contained in:
commit
da355dbc6c
10
main.py
10
main.py
@ -1,6 +1,7 @@
|
||||
import pygame
|
||||
import time
|
||||
import sys
|
||||
import random
|
||||
|
||||
from world.world import World, Position
|
||||
from world.render_objects import DebugRenderObject
|
||||
@ -17,6 +18,7 @@ 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
|
||||
@ -228,6 +230,14 @@ def main():
|
||||
print("Tick logic executed")
|
||||
world.tick_all()
|
||||
|
||||
# gets every object in the world and returns amount of FoodObjects
|
||||
objects = world.get_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))))
|
||||
|
||||
# Calculate TPS every second
|
||||
if current_time - last_tps_time >= 1.0:
|
||||
actual_tps = tick_counter
|
||||
|
||||
@ -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,17 @@ 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):
|
||||
self.decay += 1
|
||||
|
||||
if self.decay > 255:
|
||||
self.decay = 0 # eventually will raise a destroy flag
|
||||
|
||||
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)
|
||||
|
||||
@ -125,6 +125,9 @@ class BaseEntity(ABC):
|
||||
self.flags["death"] = True
|
||||
|
||||
|
||||
def get_objects(self):
|
||||
return self.objects
|
||||
|
||||
class Position:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user